@@ -432,7 +432,7 @@ def _pairwise(self):
432
432
return getattr (self .estimator , "_pairwise" , False )
433
433
434
434
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.
436
436
437
437
This uses the score defined by ``scoring`` where provided, and the
438
438
``best_estimator_.score`` method otherwise.
@@ -451,6 +451,8 @@ def score(self, X, y=None):
451
451
Returns
452
452
-------
453
453
score : float
454
+ The score defined by ``scoring`` if provided, and the
455
+ ``best_estimator_.score`` method otherwise.
454
456
"""
455
457
_check_refit (self , "score" )
456
458
check_is_fitted (self )
@@ -491,6 +493,7 @@ def score_samples(self, X):
491
493
Returns
492
494
-------
493
495
y_score : ndarray of shape (n_samples,)
496
+ The ``best_estimator_.score_samples`` method.
494
497
"""
495
498
check_is_fitted (self )
496
499
return self .best_estimator_ .score_samples (X )
@@ -508,6 +511,11 @@ def predict(self, X):
508
511
Must fulfill the input assumptions of the
509
512
underlying estimator.
510
513
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.
511
519
"""
512
520
check_is_fitted (self )
513
521
return self .best_estimator_ .predict (X )
@@ -525,6 +533,12 @@ def predict_proba(self, X):
525
533
Must fulfill the input assumptions of the
526
534
underlying estimator.
527
535
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_`.
528
542
"""
529
543
check_is_fitted (self )
530
544
return self .best_estimator_ .predict_proba (X )
@@ -542,6 +556,12 @@ def predict_log_proba(self, X):
542
556
Must fulfill the input assumptions of the
543
557
underlying estimator.
544
558
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_`.
545
565
"""
546
566
check_is_fitted (self )
547
567
return self .best_estimator_ .predict_log_proba (X )
@@ -559,6 +579,12 @@ def decision_function(self, X):
559
579
Must fulfill the input assumptions of the
560
580
underlying estimator.
561
581
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.
562
588
"""
563
589
check_is_fitted (self )
564
590
return self .best_estimator_ .decision_function (X )
@@ -576,6 +602,11 @@ def transform(self, X):
576
602
Must fulfill the input assumptions of the
577
603
underlying estimator.
578
604
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.
579
610
"""
580
611
check_is_fitted (self )
581
612
return self .best_estimator_ .transform (X )
@@ -593,12 +624,21 @@ def inverse_transform(self, Xt):
593
624
Must fulfill the input assumptions of the
594
625
underlying estimator.
595
626
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.
596
632
"""
597
633
check_is_fitted (self )
598
634
return self .best_estimator_ .inverse_transform (Xt )
599
635
600
636
@property
601
637
def n_features_in_ (self ):
638
+ """Number of features seen during :term:`fit`.
639
+
640
+ Only available when `refit=True`.
641
+ """
602
642
# For consistency with other estimators we raise a AttributeError so
603
643
# that hasattr() fails if the search estimator isn't fitted.
604
644
try :
@@ -614,6 +654,10 @@ def n_features_in_(self):
614
654
615
655
@property
616
656
def classes_ (self ):
657
+ """Class labels.
658
+
659
+ Only available when `refit=True` and the estimator is a classifier.
660
+ """
617
661
_estimator_has ("classes_" )(self )
618
662
return self .best_estimator_ .classes_
619
663
@@ -733,7 +777,12 @@ def fit(self, X, y=None, *, groups=None, **fit_params):
733
777
instance (e.g., :class:`~sklearn.model_selection.GroupKFold`).
734
778
735
779
**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.
737
786
"""
738
787
estimator = self .estimator
739
788
refit_metric = "score"
@@ -1002,7 +1051,7 @@ class GridSearchCV(BaseSearchCV):
1002
1051
1003
1052
Parameters
1004
1053
----------
1005
- estimator : estimator object.
1054
+ estimator : estimator object
1006
1055
This is assumed to implement the scikit-learn estimator interface.
1007
1056
Either estimator needs to provide a ``score`` function,
1008
1057
or ``scoring`` must be passed.
@@ -1137,25 +1186,6 @@ class GridSearchCV(BaseSearchCV):
1137
1186
.. versionchanged:: 0.21
1138
1187
Default value was changed from ``True`` to ``False``
1139
1188
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
-
1159
1189
Attributes
1160
1190
----------
1161
1191
cv_results_ : dict of numpy (masked) ndarrays
@@ -1308,6 +1338,23 @@ class GridSearchCV(BaseSearchCV):
1308
1338
sklearn.metrics.make_scorer : Make a scorer from a performance metric or
1309
1339
loss function.
1310
1340
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']
1311
1358
"""
1312
1359
1313
1360
_required_parameters = ["estimator" , "param_grid" ]
0 commit comments