From 0f49f085700502cb52d769da4cbf675fa2cb3a73 Mon Sep 17 00:00:00 2001 From: James Bourbeau Date: Fri, 2 Nov 2018 10:56:28 -0500 Subject: [PATCH] Add classes_ to classifier attributes --- sklearn/calibration.py | 4 ++-- sklearn/discriminant_analysis.py | 3 +++ sklearn/ensemble/gradient_boosting.py | 3 +++ sklearn/linear_model/logistic.py | 6 ++++++ sklearn/linear_model/passive_aggressive.py | 3 +++ sklearn/linear_model/perceptron.py | 3 +++ sklearn/linear_model/ridge.py | 6 ++++++ sklearn/linear_model/stochastic_gradient.py | 3 +++ sklearn/naive_bayes.py | 12 ++++++++++++ sklearn/neighbors/classification.py | 10 ++++++++++ sklearn/neighbors/nearest_centroid.py | 3 +++ sklearn/semi_supervised/label_propagation.py | 4 ++-- sklearn/svm/classes.py | 9 +++++++++ sklearn/tests/test_docstring_parameters.py | 13 +++++++++++++ sklearn/tree/tree.py | 5 +++++ 15 files changed, 83 insertions(+), 4 deletions(-) diff --git a/sklearn/calibration.py b/sklearn/calibration.py index ed80523880cfd..c2c0a8229e284 100644 --- a/sklearn/calibration.py +++ b/sklearn/calibration.py @@ -83,8 +83,8 @@ class CalibratedClassifierCV(BaseEstimator, ClassifierMixin): Attributes ---------- - classes_ : array, shape (n_classes) - The class labels. + classes_ : array, shape = (n_classes,) + Class labels. calibrated_classifiers_ : list (len() equal to cv or 1 if cv == "prefit") The list of calibrated classifiers, one for each crossvalidation fold, diff --git a/sklearn/discriminant_analysis.py b/sklearn/discriminant_analysis.py index a2919c7ff7a69..c0b8dfdc8d3e2 100644 --- a/sklearn/discriminant_analysis.py +++ b/sklearn/discriminant_analysis.py @@ -594,6 +594,9 @@ class QuadraticDiscriminantAnalysis(BaseEstimator, ClassifierMixin): of the Gaussian distributions along its principal axes, i.e. the variance in the rotated coordinate system. + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis diff --git a/sklearn/ensemble/gradient_boosting.py b/sklearn/ensemble/gradient_boosting.py index 39da7de21a166..2726076c95d60 100644 --- a/sklearn/ensemble/gradient_boosting.py +++ b/sklearn/ensemble/gradient_boosting.py @@ -1905,6 +1905,9 @@ class GradientBoostingClassifier(BaseGradientBoosting, ClassifierMixin): The collection of fitted sub-estimators. ``loss_.K`` is 1 for binary classification, otherwise n_classes. + classes_ : array, shape = (n_classes,) + Class labels. + Notes ----- The features are always randomly permuted at each split. Therefore, diff --git a/sklearn/linear_model/logistic.py b/sklearn/linear_model/logistic.py index 8391284817fad..4173cd9cf9785 100644 --- a/sklearn/linear_model/logistic.py +++ b/sklearn/linear_model/logistic.py @@ -1167,6 +1167,9 @@ class LogisticRegression(BaseEstimator, LinearClassifierMixin, In SciPy <= 1.0.0 the number of lbfgs iterations may exceed ``max_iter``. ``n_iter_`` will now report at most ``max_iter``. + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> from sklearn.datasets import load_iris @@ -1638,6 +1641,9 @@ class LogisticRegressionCV(LogisticRegression, BaseEstimator, Actual number of iterations for all classes, folds and Cs. In the binary or multinomial cases, the first dimension is equal to 1. + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> from sklearn.datasets import load_iris diff --git a/sklearn/linear_model/passive_aggressive.py b/sklearn/linear_model/passive_aggressive.py index 8070ef5185587..8c4c40dd23e9a 100644 --- a/sklearn/linear_model/passive_aggressive.py +++ b/sklearn/linear_model/passive_aggressive.py @@ -133,6 +133,9 @@ class PassiveAggressiveClassifier(BaseSGDClassifier): The actual number of iterations to reach the stopping criterion. For multiclass fits, it is the maximum over every binary fit. + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> from sklearn.linear_model import PassiveAggressiveClassifier diff --git a/sklearn/linear_model/perceptron.py b/sklearn/linear_model/perceptron.py index 1bc06f4f17276..90675c1f94c2f 100644 --- a/sklearn/linear_model/perceptron.py +++ b/sklearn/linear_model/perceptron.py @@ -117,6 +117,9 @@ class Perceptron(BaseSGDClassifier): The actual number of iterations to reach the stopping criterion. For multiclass fits, it is the maximum over every binary fit. + classes_ : array, shape = (n_classes,) + Class labels. + Notes ----- diff --git a/sklearn/linear_model/ridge.py b/sklearn/linear_model/ridge.py index 606cdecc17e66..912ef66042e1e 100644 --- a/sklearn/linear_model/ridge.py +++ b/sklearn/linear_model/ridge.py @@ -775,6 +775,9 @@ class RidgeClassifier(LinearClassifierMixin, _BaseRidge): Independent term in decision function. Set to 0.0 if ``fit_intercept = False``. + classes_ : array, shape = (n_classes,) + Class labels. + n_iter_ : array or None, shape (n_targets,) Actual number of iterations for each target. Available only for sag and lsqr solvers. Other solvers will return None. @@ -1358,6 +1361,9 @@ class RidgeClassifierCV(LinearClassifierMixin, _BaseRidgeCV): Independent term in decision function. Set to 0.0 if ``fit_intercept = False``. + classes_ : array, shape = (n_classes,) + Class labels. + alpha_ : float Estimated regularization parameter diff --git a/sklearn/linear_model/stochastic_gradient.py b/sklearn/linear_model/stochastic_gradient.py index 146d9623f22e7..8795eb7f6c9d2 100644 --- a/sklearn/linear_model/stochastic_gradient.py +++ b/sklearn/linear_model/stochastic_gradient.py @@ -938,6 +938,9 @@ class SGDClassifier(BaseSGDClassifier): intercept_ : array, shape (1,) if n_classes == 2 else (n_classes,) Constants in decision function. + classes_ : array, shape = (n_classes,) + Class labels. + n_iter_ : int The actual number of iterations to reach the stopping criterion. For multiclass fits, it is the maximum over every binary fit. diff --git a/sklearn/naive_bayes.py b/sklearn/naive_bayes.py index dced4fbdb3dd2..92dfe425e254f 100644 --- a/sklearn/naive_bayes.py +++ b/sklearn/naive_bayes.py @@ -143,6 +143,9 @@ class GaussianNB(BaseNB): epsilon_ : float absolute additive value to variances + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> import numpy as np @@ -676,6 +679,9 @@ class MultinomialNB(BaseDiscreteNB): during fitting. This value is weighted by the sample weight when provided. + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> import numpy as np @@ -777,6 +783,9 @@ class ComplementNB(BaseDiscreteNB): Number of samples encountered for each feature during fitting. This value is weighted by the sample weight when provided. + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> import numpy as np @@ -878,6 +887,9 @@ class BernoulliNB(BaseDiscreteNB): during fitting. This value is weighted by the sample weight when provided. + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> import numpy as np diff --git a/sklearn/neighbors/classification.py b/sklearn/neighbors/classification.py index e84f9751d0089..348e30d03a46e 100644 --- a/sklearn/neighbors/classification.py +++ b/sklearn/neighbors/classification.py @@ -82,6 +82,11 @@ class KNeighborsClassifier(NeighborsBase, KNeighborsMixin, for more details. Doesn't affect :meth:`fit` method. + Attributes + ---------- + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> X = [[0], [1], [2], [3]] @@ -296,6 +301,11 @@ class RadiusNeighborsClassifier(NeighborsBase, RadiusNeighborsMixin, ``-1`` means using all processors. See :term:`Glossary ` for more details. + Attributes + ---------- + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> X = [[0], [1], [2], [3]] diff --git a/sklearn/neighbors/nearest_centroid.py b/sklearn/neighbors/nearest_centroid.py index 316e933c78843..fb6aedb852d52 100644 --- a/sklearn/neighbors/nearest_centroid.py +++ b/sklearn/neighbors/nearest_centroid.py @@ -48,6 +48,9 @@ class NearestCentroid(BaseEstimator, ClassifierMixin): centroids_ : array-like, shape = [n_classes, n_features] Centroid of each class + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> from sklearn.neighbors.nearest_centroid import NearestCentroid diff --git a/sklearn/semi_supervised/label_propagation.py b/sklearn/semi_supervised/label_propagation.py index 081e54fbb0dfb..ae4ca11a54042 100644 --- a/sklearn/semi_supervised/label_propagation.py +++ b/sklearn/semi_supervised/label_propagation.py @@ -340,7 +340,7 @@ class LabelPropagation(BaseLabelPropagation): X_ : array, shape = [n_samples, n_features] Input array. - classes_ : array, shape = [n_classes] + classes_ : array, shape = (n_classes,) The distinct labels used in classifying instances. label_distributions_ : array, shape = [n_samples, n_classes] @@ -454,7 +454,7 @@ class LabelSpreading(BaseLabelPropagation): X_ : array, shape = [n_samples, n_features] Input array. - classes_ : array, shape = [n_classes] + classes_ : array, shape = (n_classes,) The distinct labels used in classifying instances. label_distributions_ : array, shape = [n_samples, n_classes] diff --git a/sklearn/svm/classes.py b/sklearn/svm/classes.py index b26ba34c358c1..3cbc7248e58b7 100644 --- a/sklearn/svm/classes.py +++ b/sklearn/svm/classes.py @@ -111,6 +111,9 @@ class LinearSVC(BaseEstimator, LinearClassifierMixin, intercept_ : array, shape = [1] if n_classes == 2 else [n_classes] Constants in decision function. + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> from sklearn.svm import LinearSVC @@ -556,6 +559,9 @@ class SVC(BaseSVC): fit_status_ : int 0 if correctly fitted, 1 otherwise (will raise warning) + classes_ : array, shape = (n_classes,) + Class labels. + probA_ : array, shape = [n_class * (n_class-1) / 2] probB_ : array, shape = [n_class * (n_class-1) / 2] If probability=True, the parameters learned in Platt scaling to @@ -737,6 +743,9 @@ class NuSVC(BaseSVC): intercept_ : array, shape = [n_class * (n_class-1) / 2] Constants in decision function. + classes_ : array, shape = (n_classes,) + Class labels. + Examples -------- >>> import numpy as np diff --git a/sklearn/tests/test_docstring_parameters.py b/sklearn/tests/test_docstring_parameters.py index b4a831e571c4a..19e99bd8f2dc2 100644 --- a/sklearn/tests/test_docstring_parameters.py +++ b/sklearn/tests/test_docstring_parameters.py @@ -17,6 +17,7 @@ from sklearn.utils.testing import check_docstring_parameters from sklearn.utils.testing import _get_func_name from sklearn.utils.testing import ignore_warnings +from sklearn.utils.testing import all_estimators from sklearn.utils.deprecation import _is_deprecated import pytest @@ -144,3 +145,15 @@ def test_tabs(): assert '\t' not in source, ('"%s" has tabs, please remove them ', 'or add it to theignore list' % modname) + + +@pytest.mark.parametrize('name, Classifier', + all_estimators(type_filter='classifier')) +def test_classifier_docstring_attributes(name, Classifier): + pytest.importorskip('numpydoc') + from numpydoc import docscrape + + doc = docscrape.ClassDoc(Classifier) + attributes = doc['Attributes'] + assert attributes + assert any(['classes_' in att[0] for att in attributes]) diff --git a/sklearn/tree/tree.py b/sklearn/tree/tree.py index 2357ce3f508e4..2d6e5809edadd 100644 --- a/sklearn/tree/tree.py +++ b/sklearn/tree/tree.py @@ -1291,6 +1291,11 @@ class ExtraTreeClassifier(DecisionTreeClassifier): Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. + Attributes + ---------- + classes_ : array, shape = (n_classes,) + Class labels. + See also -------- ExtraTreeRegressor, sklearn.ensemble.ExtraTreesClassifier,