10000 DOC - Ensures that GridSearchCV passes numpydoc validation by EricEllwanger · Pull Request #21003 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

DOC - Ensures that GridSearchCV passes numpydoc validation #21003

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 17 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
1f446d6
(#20308) Ensure GridSearchCV-none passes numpydoc validation - Correc…
frellwan Sep 9, 2021
5ebddc6
(#20308) Ensure GridSearchCV-classes_ passes numpydoc validation - ad…
frellwan Sep 10, 2021
f60864f
(#20308) Ensure GridSearchCV-fit passes numpydoc validation - added R…
frellwan Sep 10, 2021
9a4f876
(#20308) Ensure GridSearchCV-decision_function passes numpydoc valida…
frellwan Sep 10, 2021
accb5ba
(#20308) Ensure GridSearchCV-inverse_transform passes numpydoc valida…
frellwan Sep 10, 2021
507d143
(#20308) Ensure GridSearchCV-n_features_in_ passes numpydoc validatio…
frellwan Sep 10, 2021
3ba5d1d
(#20308) Ensure GridSearchCV-predict passes numpydoc validation - add…
frellwan Sep 10, 2021
9a21a01
(#20308) Ensure GridSearchCV-predict_log_proba passes numpydoc valida…
frellwan Sep 10, 2021
1779148 10000
(#20308) Ensure GridSearchCV-predict_proba passes numpydoc validation…
frellwan Sep 10, 2021
624f76f
(#20308) Ensure GridSearchCV-score passes numpydoc validation - added…
frellwan Sep 10, 2021
e6d72e3
(#20308) Ensure GridSearchCV-score_samples passes numpydoc validation…
frellwan Sep 10, 2021
21c4a6b
(#20308) Ensure GridSearchCV-transform passes numpydoc validation - a…
frellwan Sep 10, 2021
6e081b0
(#20308) Ensure GridSearchCV-score passes numpydoc validation - Chang…
frellwan Sep 10, 2021
37b7a14
(#20308) Remove GridSearchCV from DOCSTRING_IGNORE_LIST
frellwan Sep 10, 2021
2c268b7
Merge branch 'doc-fixes' of https://github.com/EricEllwanger/scikit-l…
frellwan Sep 10, 2021
839fbf1
Merge branch 'main' into doc-fixes
EricEllwanger Sep 10, 2021
29d9b22
Correct indentation
glemaitre Sep 13, 2021
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 @@ -9,7 +9,6 @@

# List of modules ignored when checking for numpydoc validation.
DOCSTRING_IGNORE_LIST = [
"GridSearchCV",
"HalvingGridSearchCV", 10000
"HalvingRandomSearchCV",
"HashingVectorizer",
Expand Down
91 changes: 69 additions & 22 deletions sklearn/model_selection/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def _pairwise(self):
return getattr(self.estimator, "_pairwise", False)

def score(self, X, y=None):
"""Returns the score on the given data, if the estimator has been refit.
"""Return the score on the given data, if the estimator has been refit.

This uses the score defined by ``scoring`` where provided, and the
``best_estimator_.score`` method otherwise.
Expand All @@ -451,6 +451,8 @@ def score(self, X, y=None):
Returns
-------
score : float
The score defined by ``scoring`` if provided, and the
``best_estimator_.score`` method otherwise.
"""
_check_refit(self, "score")
check_is_fitted(self)
Expand Down Expand Up @@ -491,6 +493,7 @@ def score_samples(self, X):
Returns
-------
y_score : ndarray of shape (n_samples,)
The ``best_estimator_.score_samples`` method.
"""
check_is_fitted(self)
return self.best_estimator_.score_samples(X)
Expand All @@ -508,6 +511,11 @@ def predict(self, X):
Must fulfill the input assumptions of the
underlying estimator.

Returns
-------
y_pred : ndarray of shape (n_samples,)
The predicted labels or values for `X` based on the estimator with
the best found parameters.
"""
check_is_fitted(self)
return self.best_estimator_.predict(X)
Expand All @@ -525,6 +533,12 @@ def predict_proba(self, X):
Must fulfill the input assumptions of the
underlying estimator.

Returns
-------
y_pred : ndarray of shape (n_samples,) or (n_samples, n_classes)
Predicted class probabilities for `X` based on the estimator with
the best found parameters. The order of the classes corresponds
to that in the fitted attribute :term:`classes_`.
"""
check_is_fitted(self)
return self.best_estimator_.predict_proba(X)
Expand All @@ -542,6 +556,12 @@ def predict_log_proba(self, X):
Must fulfill the input assumptions of the
underlying estimator.

Returns
-------
y_pred : ndarray of shape (n_samples,) or (n_samples, n_classes)
Predicted class log-probabilities for `X` based on the estimator
with the best found parameters. The order of the classes
corresponds to that in the fitted attribute :term:`classes_`.
"""
check_is_fitted(self)
return self.best_estimator_.predict_log_proba(X)
Expand All @@ -559,6 +579,12 @@ def decision_function(self, X):
Must fulfill the input assumptions of the
underlying estimator.

Returns
-------
y_score : ndarray of shape (n_samples,) or (n_samples, n_classes) \
or (n_samples, n_classes * (n_classes-1) / 2)
Result of the decision function for `X` based on the estimator with
the best found parameters.
"""
check_is_fitted(self)
return self.best_estimator_.decision_function(X)
Expand All @@ -576,6 +602,11 @@ def transform(self, X):
Must fulfill the input assumptions of the
underlying estimator.

Returns
-------
Xt : {ndarray, sparse matrix} of shape (n_samples, n_features)
`X` transformed in the new space based on the estimator with
the best found parameters.
"""
check_is_fitted(self)
return self.best_estimator_.transform(X)
Expand All @@ -593,12 +624,21 @@ def inverse_transform(self, Xt):
Must fulfill the input assumptions of the
underlying estimator.

Returns
-------
X : {ndarray, sparse matrix} of shape (n_samples, n_features)
Result of the `inverse_transform` function for `Xt` based on the
estimator with the best found parameters.
"""
check_is_fitted(self)
return self.best_estimator_.inverse_transform(Xt)

@property
def n_features_in_(self):
"""Number of features seen during :term:`fit`.

Only available when `refit=True`.
"""
# For consistency with other estimators we raise a AttributeError so
# that hasattr() fails if the search estimator isn't fitted.
try:
Expand All @@ -614,6 +654,10 @@ def n_features_in_(self):

@property
def classes_(self):
"""Class labels.

Only available when `refit=True` and the estimator is a classifier.
"""
_estimator_has("classes_")(self)
return self.best_estimator_.classes_

Expand Down Expand Up @@ -733,7 +777,12 @@ def fit(self, X, y=None, *, groups=None, **fit_params):
instance (e.g., :class:`~sklearn.model_selection.GroupKFold`).

**fit_params : dict of str -> object
Parameters passed to the ``fit`` method of the estimator
Parameters passed to the ``fit`` method of the estimator.

Returns
-------
self : object
Instance of fitted estimator.
"""
estimator = self.estimator
refit_metric = "score"
Expand Down Expand Up @@ -1002,7 +1051,7 @@ class GridSearchCV(BaseSearchCV):

Parameters
----------
estimator : estimator object.
estimator : estimator object
This is assumed to implement the scikit-learn estimator interface.
Either estimator needs to provide a ``score`` function,
or ``scoring`` must be passed.
Expand Down Expand Up @@ -1137,25 +1186,6 @@ class GridSearchCV(BaseSearchCV):
.. versionchanged:: 0.21
Default value was changed from ``True`` to ``False``


Examples
--------
>>> from sklearn import svm, datasets
>>> from sklearn.model_selection import GridSearchCV
>>> iris = datasets.load_iris()
>>> parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
>>> svc = svm.SVC()
>>> clf = GridSearchCV(svc, parameters)
>>> clf.fit(iris.data, iris.target)
GridSearchCV(estimator=SVC(),
param_grid={'C': [1, 10], 'kernel': ('linear', 'rbf')})
>>> sorted(clf.cv_results_.keys())
['mean_fit_time', 'mean_score_time', 'mean_test_score',...
'param_C', 'param_kernel', 'params',...
'rank_test_score', 'split0_test_score',...
'split2_test_score', ...
'std_fit_time', 'std_score_time', 'std_test_score']

Attributes
----------
cv_results_ : dict of numpy (masked) ndarrays
Expand Down Expand Up @@ -1308,6 +1338,23 @@ class GridSearchCV(BaseSearchCV):
sklearn.metrics.make_scorer : Make a scorer from a performance metric or
loss function.

Examples
--------
>>> from sklearn import svm, datasets
>>> from sklearn.model_selection import GridSearchCV
>>> iris = datasets.load_iris()
>>> parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
>>> svc = svm.SVC()
>>> clf = GridSearchCV(svc, parameters)
>>> clf.fit(iris.data, iris.target)
GridSearchCV(estimator=SVC(),
param_grid={'C': [1, 10], 'kernel': ('linear', 'rbf')})
>>> sorted(clf.cv_results_.keys())
['mean_fit_time', 'mean_score_time', 'mean_test_score',...
'param_C', 'param_kernel', 'params',...
'rank_test_score', 'split0_test_score',...
'split2_test_score', ...
'std_fit_time', 'std_score_time', 'std_test_score']
"""

_required_parameters = ["estimator", "param_grid"]
Expand Down
0