8000 DOC Ensures that NearestCentroid passes numpydoc validation by jmloyola · Pull Request #21084 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

DOC Ensures that NearestCentroid passes numpydoc validation #21084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion maint_tools/test_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"MultiTaskElasticNetCV",
"MultiTaskLasso",
"MultiTaskLassoCV",
"NearestCentroid",
"OrthogonalMatchingPursuit",
"OrthogonalMatchingPursuitCV",
"PLSCanonical",
Expand Down
60 changes: 33 additions & 27 deletions sklearn/neighbors/_nearest_centroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ class NearestCentroid(ClassifierMixin, BaseEstimator):
metric : str or callable
The metric to use when calculating distance between instances in a
feature array. If metric is a string or callable, it must be one of
the options allowed by metrics.pairwise.pairwise_distances for its
metric parameter.
The centroids for the samples corresponding to each class is the point
from which the sum of the distances (according to the metric) of all
samples that belong to that particular class are minimized.
If the "manhattan" metric is provided, this centroid is the median and
for all other metrics, the centroid is now set to be the mean.
the options allowed by
:func:`~sklearn.metrics.pairwise_distances` for its metric
parameter. The centroids for the samples corresponding to each class is
the point from which the sum of the distances (according to the metric)
of all samples that belong to that particular class are minimized.
If the `"manhattan"` metric is provided, this centroid is the median
and for all other metrics, the centroid is now set to be the mean.

.. versionchanged:: 0.19
``metric='precomputed'`` was deprecated and now raises an error
`metric='precomputed'` was deprecated and now raises an error

shrink_threshold : float, default=None
Threshold for shrinking centroids to remove features.
Expand All @@ -66,18 +66,6 @@ class NearestCentroid(ClassifierMixin, BaseEstimator):

.. versionadded:: 1.0

Examples
--------
>>> from sklearn.neighbors import NearestCentroid
>>> import numpy as np
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> y = np.array([1, 1, 1, 2, 2, 2])
>>> clf = NearestCentroid()
>>> clf.fit(X, y)
NearestCentroid()
>>> print(clf.predict([[-0.8, -1]]))
[1]

See Also
--------
KNeighborsClassifier : Nearest neighbors classifier.
Expand All @@ -94,6 +82,17 @@ class NearestCentroid(ClassifierMixin, BaseEstimator):
of the National Academy of Sciences of the United States of America,
99(10), 6567-6572. The National Academy of Sciences.

Examples
--------
>>> from sklearn.neighbors import NearestCentroid
>>> import numpy as np
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> y = np.array([1, 1, 1, 2, 2, 2])
>>> clf = NearestCentroid()
>>> clf.fit(X, y)
NearestCentroid()
>>> print(clf.predict([[-0.8, -1]]))
[1]
"""

def __init__(self, metric="euclidean", *, shrink_threshold=None):
Expand All @@ -111,7 +110,12 @@ def fit(self, X, y):
`n_features` is the number of features.
Note that centroid shrinking cannot be used with sparse matrices.
y : array-like of shape (n_samples,)
Target values (integers)
Target values.

Returns
-------
self : object
Fitted estimator.
"""
if self.metric == "precomputed":
raise ValueError("Precomputed is not supported.")
Expand Down Expand Up @@ -191,23 +195,25 @@ def fit(self, X, y):
return self

def predict(self, X):
"""Perform classification on an array of test vectors X.
"""Perform classification on an array of test vectors `X`.

The predicted class C for each sample in X is returned.
The predicted class `C` for each sample in `X` is returned.

Parameters
----------
X : array-like of shape (n_samples, n_features)
X : {array-like, sparse matrix} of shape (n_samples, n_features)
Test samples.

Returns
-------
C : ndarray of shape (n_samples,)
The predicted classes.

Notes
-----
If the metric constructor parameter is "precomputed", X is assumed to
be the distance matrix between the data to be predicted and
``self.centroids_``.
If the metric constructor parameter is `"precomputed"`, `X` is assumed
to be the distance matrix between the data to be predicted and
`self.centroids_`.
"""
check_is_fitted(self)

Expand Down
0