Skip to content

Commit 98cb91b

Browse files
go-bearsrth
authored andcommitted
DOC numpydoc validation for VotingClassifier (scikit-learn#15497)
1 parent 54e78b7 commit 98cb91b

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

maint_tools/test_docstrings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
"SGDClassifier.score",
4040
"SGDClassifier.sparsify",
4141
"SGDClassifier.densify",
42+
"VotingClassifier.fit",
43+
"VotingClassifier.transform",
44+
"VotingClassifier.predict",
45+
"VotingClassifier.score",
46+
"VotingClassifier.predict_proba",
47+
"VotingClassifier.set_params",
48+
"VotingClassifier.get_params",
49+
"VotingClassifier.named_estimators",
50+
"VotingClassifier$",
4251
]
4352

4453

sklearn/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
"""Base classes for all estimators."""
1+
"""
2+
Base classes for all estimators.
3+
4+
Used for VotingClassifier
5+
"""
26

37
# Author: Gael Varoquaux <[email protected]>
48
# License: BSD 3 clause
@@ -334,6 +338,7 @@ def _get_tags(self):
334338

335339
class ClassifierMixin:
336340
"""Mixin class for all classifiers in scikit-learn."""
341+
337342
_estimator_type = "classifier"
338343

339344
def score(self, X, y, sample_weight=None):

sklearn/ensemble/_base.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Base class for ensemble-based estimators.
3-
"""
1+
"""Base class for ensemble-based estimators."""
42

53
# Authors: Gilles Louppe
64
# License: BSD 3 clause
@@ -42,14 +40,13 @@ def _parallel_fit_estimator(estimator, X, y, sample_weight=None):
4240

4341

4442
def _set_random_states(estimator, random_state=None):
45-
"""Sets fixed random_state parameters for an estimator
43+
"""Set fixed random_state parameters for an estimator.
4644
4745
Finds all parameters ending ``random_state`` and sets them to integers
4846
derived from ``random_state``.
4947
5048
Parameters
5149
----------
52-
5350
estimator : estimator supporting get/set_params
5451
Estimator with potential randomness managed by random_state
5552
parameters.
@@ -106,6 +103,7 @@ class BaseEnsemble(MetaEstimatorMixin, BaseEstimator, metaclass=ABCMeta):
106103
estimators_ : list of estimators
107104
The collection of fitted base estimators.
108105
"""
106+
109107
# overwrite _required_parameters from MetaEstimatorMixin
110108
_required_parameters = []
111109

@@ -122,8 +120,10 @@ def __init__(self, base_estimator, n_estimators=10,
122120
# self.estimators_ needs to be filled by the derived classes in fit.
123121

124122
def _validate_estimator(self, default=None):
125-
"""Check the estimator and the n_estimator attribute, set the
126-
`base_estimator_` attribute."""
123+
"""Check the estimator and the n_estimator attribute.
124+
125+
Sets the base_estimator_` attributes.
126+
"""
127127
if not isinstance(self.n_estimators, numbers.Integral):
128128
raise ValueError("n_estimators must be an integer, "
129129
"got {0}.".format(type(self.n_estimators)))
@@ -159,15 +159,15 @@ def _make_estimator(self, append=True, random_state=None):
159159
return estimator
160160

161161
def __len__(self):
162-
"""Returns the number of estimators in the ensemble."""
162+
"""Return the number of estimators in the ensemble."""
163163
return len(self.estimators_)
164164

165165
def __getitem__(self, index):
166-
"""Returns the index'th estimator in the ensemble."""
166+
"""Return the index'th estimator in the ensemble."""
167167
return self.estimators_[index]
168168

169169
def __iter__(self):
170-
"""Returns iterator over estimators in the ensemble."""
170+
"""Return iterator over estimators in the ensemble."""
171171
return iter(self.estimators_)
172172

173173

@@ -204,6 +204,7 @@ class _BaseHeterogeneousEnsemble(MetaEstimatorMixin, _BaseComposition,
204204
training data. If an estimator has been set to `'drop'`, it will not
205205
appear in `estimators_`.
206206
"""
207+
207208
_required_parameters = ['estimators']
208209

209210
@property

sklearn/ensemble/_voting.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,19 @@ class _BaseVoting(TransformerMixin, _BaseHeterogeneousEnsemble):
4141

4242
@property
4343
def _weights_not_none(self):
44-
"""Get the weights of not `None` estimators"""
44+
"""Get the weights of not `None` estimators."""
4545
if self.weights is None:
4646
return None
4747
return [w for est, w in zip(self.estimators, self.weights)
4848
if est[1] not in (None, 'drop')]
4949

5050
def _predict(self, X):
51-
"""Collect results from clf.predict calls. """
51+
"""Collect results from clf.predict calls."""
5252
return np.asarray([est.predict(X) for est in self.estimators_]).T
5353

5454
@abstractmethod
5555
def fit(self, X, y, sample_weight=None):
56-
"""
57-
common fit operations.
58-
"""
56+
"""Get common fit operations."""
5957
names, clfs = self._validate_estimators()
6058

6159
if (self.weights is not None and
@@ -90,7 +88,7 @@ class VotingClassifier(ClassifierMixin, _BaseVoting):
9088
9189
Parameters
9290
----------
93-
estimators : list of (string, estimator) tuples
91+
estimators : list of (str, estimator) tuples
9492
Invoking the ``fit`` method on the ``VotingClassifier`` will fit clones
9593
of those original estimators that will be stored in the class attribute
9694
``self.estimators_``. An estimator can be set to ``'drop'``
@@ -138,6 +136,10 @@ class VotingClassifier(ClassifierMixin, _BaseVoting):
138136
classes_ : array-like, shape (n_predictions,)
139137
The classes labels.
140138
139+
See Also
140+
--------
141+
VotingRegressor: Prediction voting regressor.
142+
141143
Examples
142144
--------
143145
>>> import numpy as np
@@ -172,10 +174,6 @@ class VotingClassifier(ClassifierMixin, _BaseVoting):
172174
[1 1 1 2 2 2]
173175
>>> print(eclf3.transform(X).shape)
174176
(6, 6)
175-
176-
See also
177-
--------
178-
VotingRegressor: Prediction voting regressor.
179177
"""
180178

181179
def __init__(self, estimators, voting='hard', weights=None, n_jobs=None,
@@ -187,7 +185,7 @@ def __init__(self, estimators, voting='hard', weights=None, n_jobs=None,
187185
self.flatten_transform = flatten_transform
188186

189187
def fit(self, X, y, sample_weight=None):
190-
""" Fit the estimators.
188+
"""Fit the estimators.
191189
192190
Parameters
193191
----------
@@ -206,6 +204,7 @@ def fit(self, X, y, sample_weight=None):
206204
Returns
207205
-------
208206
self : object
207+
209208
"""
210209
check_classification_targets(y)
211210
if isinstance(y, np.ndarray) and len(y.shape) > 1 and y.shape[1] > 1:
@@ -223,7 +222,7 @@ def fit(self, X, y, sample_weight=None):
223222
return super().fit(X, transformed_y, sample_weight)
224223

225224
def predict(self, X):
226-
""" Predict class labels for X.
225+
"""Predict class labels for X.
227226
228227
Parameters
229228
----------
@@ -235,7 +234,6 @@ def predict(self, X):
235234
maj : array-like, shape (n_samples,)
236235
Predicted class labels.
237236
"""
238-
239237
check_is_fitted(self)
240238
if self.voting == 'soft':
241239
maj = np.argmax(self.predict_proba(X), axis=1)
@@ -252,11 +250,11 @@ def predict(self, X):
252250
return maj
253251

254252
def _collect_probas(self, X):
255-
"""Collect results from clf.predict calls. """
253+
"""Collect results from clf.predict calls."""
256254
return np.asarray([clf.predict_proba(X) for clf in self.estimators_])
257255

258256
def _predict_proba(self, X):
259-
"""Predict class probabilities for X in 'soft' voting """
257+
"""Predict class probabilities for X in 'soft' voting."""
260258
check_is_fitted(self)
261259
avg = np.average(self._collect_probas(X), axis=0,
262260
weights=self._weights_not_none)

0 commit comments

Comments
 (0)