8000 DOC - Ensures that GridSearchCV passes numpydoc validation (#21003) · scikit-learn/scikit-learn@2fcbf40 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2fcbf40

Browse files
EricEllwangeradrinjalali
authored andcommitted
DOC - Ensures that GridSearchCV passes numpydoc validation (#21003)
1 parent 554d60e commit 2fcbf40

File tree

2 files changed

+69
-23
lines changed

2 files changed

+69
-23
lines changed

maint_tools/test_docstrings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
# List of modules ignored when checking for numpydoc validation.
1111
DOCSTRING_IGNORE_LIST = [
12-
"GridSearchCV",
1312
"HalvingGridSearchCV",
1413
"HalvingRandomSearchCV",
1514
"HashingVectorizer",

sklearn/model_selection/_search.py

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def _pairwise(self):
432432
return getattr(self.estimator, "_pairwise", False)
433433

434434
def score(self, X, y=None):
435-
"""Returns the score on the given data, if the estimator has been refit.
435+
"""Return the score on the given data, if the estimator has been refit.
436436
437437
This uses the score defined by ``scoring`` where provided, and the
438438
``best_estimator_.score`` method otherwise.
@@ -451,6 +451,8 @@ def score(self, X, y=None):
451451
Returns
452452
-------
453453
score : float
454+
The score defined by ``scoring`` if provided, and the
455+
``best_estimator_.score`` method otherwise.
454456
"""
455457
_check_refit(self, "score")
456458
check_is_fitted(self)
@@ -491,6 +493,7 @@ def score_samples(self, X):
491493
Returns
492494
-------
493495
y_score : ndarray of shape (n_samples,)
496+
The ``best_estimator_.score_samples`` method.
494497
"""
495498
check_is_fitted(self)
496499
return self.best_estimator_.score_samples(X)
@@ -508,6 +511,11 @@ def predict(self, X):
508511
Must fulfill the input assumptions of the
509512
underlying estimator.
510513
514+
Returns
515+
-------
516+
y_pred : ndarray of shape (n_samples,)
517+
The predicted labels or values for `X` based on the estimator with
518+
the best found parameters.
511519
"""
512520
check_is_fitted(self)
513521
return self.best_estimator_.predict(X)
@@ -525,6 +533,12 @@ def predict_proba(self, X):
525533
Must fulfill the input assumptions of the
526534
underlying estimator.
527535
536+
Returns
537+
-------
538+
y_pred : ndarray of shape (n_samples,) or (n_samples, n_classes)
539+
Predicted class probabilities for `X` based on the estimator with
540+
the best found parameters. The order of the classes corresponds
541+
to that in the fitted attribute :term:`classes_`.
528542
"""
529543
check_is_fitted(self)
530544
return self.best_estimator_.predict_proba(X)
@@ -542,6 +556,12 @@ def predict_log_proba(self, X):
542556
Must fulfill the input assumptions of the
543557
underlying estimator.
544558
559+
Returns
560+
-------
561+
y_pred : ndarray of shape (n_samples,) or (n_samples, n_classes)
562+
Predicted class log-probabilities for `X` based on the estimator
563+
with the best found parameters. The order of the classes
564+
corresponds to that in the fitted attribute :term:`classes_`.
545565
"""
546566
check_is_fitted(self)
547567
return self.best_estimator_.predict_log_proba(X)
@@ -559,6 +579,12 @@ def decision_function(self, X):
559579
Must fulfill the input assumptions of the
560580
underlying estimator.
561581
582+
Returns
583+
-------
584+
y_score : ndarray of shape (n_samples,) or (n_samples, n_classes) \
585+
or (n_samples, n_classes * (n_classes-1) / 2)
586+
Result of the decision function for `X` based on the estimator with
587+
the best found parameters.
562588
"""
563589
check_is_fitted(self)
564590
return self.best_estimator_.decision_function(X)
@@ -576,6 +602,11 @@ def transform(self, X):
576602
Must fulfill the input assumptions of the
577603
underlying estimator.
578604
605+
Returns
606+
-------
607+
Xt : {ndarray, sparse matrix} of shape (n_samples, n_features)
608+
`X` transformed in the new space based on the estimator with
609+
the best found parameters.
579610
"""
580611
check_is_fitted(self)
581612
return self.best_estimator_.transform(X)
@@ -593,12 +624,21 @@ def inverse_transform(self, Xt):
593624
Must fulfill the input assumptions of the
594625
underlying estimator.
595626
627+
Returns
628+
-------
629+
X : {ndarray, sparse matrix} of shape (n_samples, n_features)
630+
Result of the `inverse_transform` function for `Xt` based on the
631+
estimator with the best found parameters.
596632
"""
597633
check_is_fitted(self)
598634
return self.best_estimator_.inverse_transform(Xt)
599635

600636
@property
601637
def n_features_in_(self):
638+
"""Number of features seen during :term:`fit`.
639+
640+
Only available when `refit=True`.
641+
"""
602642
# For consistency with other estimators we raise a AttributeError so
603643
# that hasattr() fails if the search estimator isn't fitted.
604644
try:
@@ -614,6 +654,10 @@ def n_features_in_(self):
614654

615655
@property
616656
def classes_(self):
657+
"""Class labels.
658+
659+
Only available when `refit=True` and the estimator is a classifier.
660+
"""
617661
_estimator_has("classes_")(self)
618662
return self.best_estimator_.classes_
619663

@@ -733,7 +777,12 @@ def fit(self, X, y=None, *, groups=None, **fit_params):
733777
instance (e.g., :class:`~sklearn.model_selection.GroupKFold`).
734778
735779
**fit_params : dict of str -> object
736-
Parameters passed to the ``fit`` method of the estimator
780+
Parameters passed to the ``fit`` method of the estimator.
781+
782+
Returns
783+
-------
784+
self : object
785+
Instance of fitted estimator.
737786
"""
738787
estimator = self.estimator
739788
refit_metric = "score"
@@ -1002,7 +1051,7 @@ class GridSearchCV(BaseSearchCV):
10021051
10031052
Parameters
10041053
----------
1005-
estimator : estimator object.
1054+
estimator : estimator object
10061055
This is assumed to implement the scikit-learn estimator interface.
10071056
Either estimator needs to provide a ``score`` function,
10081057
or ``scoring`` must be passed.
@@ -1137,25 +1186,6 @@ class GridSearchCV(BaseSearchCV):
11371186
.. versionchanged:: 0.21
11381187
Default value was changed from ``True`` to ``False``
11391188
1140-
1141-
Examples
1142-
--------
1143-
>>> from sklearn import svm, datasets
1144-
>>> from sklearn.model_selection import GridSearchCV
1145-
>>> iris = datasets.load_iris()
1146-
>>> parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
1147-
>>> svc = svm.SVC()
1148-
>>> clf = GridSearchCV(svc, parameters)
1149-
>>> clf.fit(iris.data, iris.target)
1150-
GridSearchCV(estimator=SVC(),
1151-
param_grid={'C': [1, 10], 'kernel': ('linear', 'rbf')})
1152-
>>> sorted(clf.cv_results_.keys())
1153-
['mean_fit_time', 'mean_score_time', 'mean_test_score',...
1154-
'param_C', 'param_kernel', 'params',...
1155-
'rank_test_score', 'split0_test_score',...
1156-
'split2_test_score', ...
1157-
'std_fit_time', 'std_score_time', 'std_test_score']
1158-
11591189
Attributes
11601190
----------
11611191
cv_results_ : dict of numpy (masked) ndarrays
@@ -1308,6 +1338,23 @@ class GridSearchCV(BaseSearchCV):
13081338
sklearn.metrics.make_scorer : Make a scorer from a performance metric or
13091339
loss function.
13101340
1341+
Examples
1342+
--------
1343+
>>> from sklearn import svm, datasets
1344+
>>> from sklearn.model_selection import GridSearchCV
1345+
>>> iris = datasets.load_iris()
1346+
>>> parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
1347+
>>> svc = svm.SVC()
1348+
>>> clf = GridSearchCV(svc, parameters)
1349+
>>> clf.fit(iris.data, iris.target)
1350+
GridSearchCV(estimator=SVC(),
1351+
param_grid={'C': [1, 10], 'kernel': ('linear', 'rbf')})
1352+
>>> sorted(clf.cv_results_.keys())
1353+
['mean_fit_time', 'mean_score_time', 'mean_test_score',...
1354+
'param_C', 'param_kernel', 'params',...
1355+
'rank_test_score', 'split0_test_score',...
1356+
'split2_test_score', ...
1357+
'std_fit_time', 'std_score_time', 'std_test_score']
13111358
"""
13121359

13131360
_required_parameters = ["estimator", "param_grid"]

0 commit comments

Comments
 (0)
0