1
- # Adapted from
2
- # https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/pipeline.py
3
-
4
- """
5
- The :mod:`unblanced_dataset.pipeline` module implements utilities to build a composite
6
- estimator, as a chain of transforms, samples and estimators.
1
+ """
2
+ The :mod:`unbalanced_dataset.pipeline` module implements utilities to build
3
+ a composite estimator, as a chain of transforms, samples and estimators.
7
4
"""
8
5
6
+ # Adapted from
7
+ # https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/pipeline.py
8
+
9
9
# Author: Edouard Duchesnay
10
10
# Gael Varoquaux
11
11
# Virgile Fritsch
14
14
# chkoar
15
15
# License: BSD
16
16
17
+ from __future__ import print_function
18
+ from __future__ import division
19
+
17
20
from warnings import warn
18
21
19
22
from sklearn .externals import six
@@ -29,7 +32,7 @@ class Pipeline(pipeline.Pipeline):
29
32
"""Pipeline of transforms and resamples with a final estimator.
30
33
31
34
Sequentially apply a list of transforms, samples and a final estimator.
32
- Intermediate steps of the pipeline must be transformers or resamplers,
35
+ Intermediate steps of the pipeline must be transformers or resamplers,
33
36
that is, they must implement fit, transform and sample methods.
34
37
The final estimator only needs to implement fit.
35
38
@@ -38,50 +41,19 @@ class Pipeline(pipeline.Pipeline):
38
41
For this, it enables setting parameters of the various steps using their
39
42
names and the parameter name separated by a '__', as in the example below.
40
43
41
- Read more in the :ref:`User Guide <pipeline>`.
42
-
43
44
Parameters
44
45
----------
45
46
steps : list
46
- List of (name, transform) tuples (implementing fit/transform/fit_sample) that are
47
- chained, in the order in which they are chained, with the last object
48
- an estimator.
47
+ List of (name, transform) tuples (implementing
48
+ fit/transform/fit_sample) that are chained, in the order in which they
49
+ are chained, with the last object an estimator.
49
50
50
51
Attributes
51
52
----------
52
53
named_steps : dict
53
54
Read-only attribute to access any step parameter by user given name.
54
55
Keys are step names and values are steps parameters.
55
56
56
- Examples
57
- --------
58
- >>> from sklearn import svm
59
- >>> from sklearn.datasets import samples_generator
60
- >>> from sklearn.feature_selection import SelectKBest
61
- >>> from sklearn.feature_selection import f_regression
62
- >>> from sklearn.pipeline import Pipeline
63
- >>> # generate some data to play with
64
- >>> X, y = samples_generator.make_classification(
65
- ... n_informative=5, n_redundant=0, random_state=42)
66
- >>> # ANOVA SVM-C
67
- >>> anova_filter = SelectKBest(f_regression, k=5)
68
- >>> clf = svm.SVC(kernel='linear')
69
- >>> anova_svm = Pipeline([('anova', anova_filter), ('svc', clf)])
70
- >>> # You can set the parameters using the names issued
71
- >>> # For instance, fit using a k of 10 in the SelectKBest
72
- >>> # and a parameter 'C' of the svm
73
- >>> anova_svm.set_params(anova__k=10, svc__C=.1).fit(X, y)
74
- ... # doctest: +ELLIPSIS
75
- Pipeline(steps=[...])
76
- >>> prediction = anova_svm.predict(X)
77
- >>> anova_svm.score(X, y) # doctest: +ELLIPSIS
78
- 0.77...
79
- >>> # getting the selected features chosen by anova_filter
80
- >>> anova_svm.named_steps['anova'].get_support()
81
- ... # doctest: +NORMALIZE_WHITESPACE
82
- array([ True, True, True, False, False, True, False, True, True, True,
83
- False, False, True, False, True, False, False, False, False,
84
- True], dtype=bool)
85
57
"""
86
58
87
59
# BaseEstimator interface
@@ -99,8 +71,8 @@ def __init__(self, steps):
99
71
100
72
for t in transforms :
101
73
if (not (hasattr (t , "fit" ) or hasattr (t , "fit_transform" ) or
102
- hasattr (t , "fit_sample" )) or not ( hasattr ( t , "transform" )
103
- or hasattr (t , "sample" ))):
74
+ hasattr (t , "fit_sample" )) or
75
+ not ( hasattr ( t , "transform" ) or hasattr (t , "sample" ))):
104
76
raise TypeError ("All intermediate steps of the chain should "
105
77
"be transforms and implement fit and transform"
106
78
" '%s' (type %s) doesn't)" % (t , type (t )))
@@ -130,7 +102,7 @@ def _pre_transform(self, X, y=None, **fit_params):
130
102
return Xt , yt , fit_params_steps [self .steps [- 1 ][0 ]]
131
103
132
104
def fit (self , X , y = None , ** fit_params ):
133
- """Fit all the transforms and samples one after the other and transform
105
+ """Fit all the transforms and samples one after the other and transform
134
106
the data, then fit the transformed data using the final estimator.
135
107
136
108
Parameters
@@ -147,8 +119,8 @@ def fit(self, X, y=None, **fit_params):
147
119
return self
148
120
149
121
def fit_transform (self , X , y = None , ** fit_params ):
150
- """Fit all the transforms and samples one after the other and
151
- transform or sample the data, then use fit_transform on
122
+ """Fit all the transforms and samples one after the other and
123
+ transform or sample the data, then use fit_transform on
152
124
transformed data using the final estimator.
153
125
154
126
Parameters
@@ -169,8 +141,8 @@ def fit_transform(self, X, y=None, **fit_params):
169
141
170
142
@if_delegate_has_method (delegate = '_final_estimator' )
171
143
def fit_sample (self , X , y = None , ** fit_params ):
172
- """Fit all the transforms and samples one after the other and
173
- transform or sample the data, then use fit_sample on
144
+ """Fit all the transforms and samples one after the other and
145
+ transform or sample the data, then use fit_sample on
174
146
transformed data using the final estimator.
175
147
176
148
Parameters
@@ -188,8 +160,8 @@ def fit_sample(self, X, y=None, **fit_params):
188
160
189
161
@if_delegate_has_method (delegate = '_final_estimator' )
190
162
def sample (self , X , y ):
191
- """Applies transforms to the data, and the sample method of
192
- the final estimator. Valid only if the final estimator
163
+ """Applies transforms to the data, and the sample method of
164
+ the final estimator. Valid only if the final estimator
193
165
implements predict.
194
166
195
167
Parameters
@@ -199,7 +171,7 @@ def sample(self, X, y):
199
171
of the pipeline.
200
172
"""
201
173
Xt = X
202
- for name , transform in self .steps [:- 1 ]:
174
+ for _ , transform in self .steps [:- 1 ]:
203
175
if hasattr (transform , "fit_sample" ):
204
176
pass
205
177
else :
@@ -208,8 +180,8 @@ def sample(self, X, y):
208
180
209
181
@if_delegate_has_method (delegate = '_final_estimator' )
210
182
def predict (self , X ):
211
- """Applies transforms to the data, and the predict method of
212
- the final estimator. Valid only if the final estimator
183
+ """Applies transforms to the data, and the predict method of
184
+ the final estimator. Valid only if the final estimator
213
185
implements predict.
214
186
215
187
Parameters
@@ -219,7 +191,7 @@ def predict(self, X):
219
191
of the pipeline.
220
192
"""
221
193
Xt = X
222
- for name , transform in self .steps [:- 1 ]:
194
+ for _ , transform in self .steps [:- 1 ]:
223
195
if hasattr (transform , "fit_sample" ):
224
196
pass
225
197
else :
@@ -231,8 +203,8 @@ def fit_predict(self, X, y=None, **fit_params):
231
203
"""Applies fit_predict of last step in pipeline after transforms
232
204
and samples.
233
205
234
- Applies fit_transforms or fit_samples of a pipeline to the data,
235
- followed by the fit_predict method of the final estimator in the
206
+ Applies fit_transforms or fit_samples of a pipeline to the data,
207
+ followed by the fit_predict method of the final estimator in the
236
208
pipeline. Valid only if the final estimator implements fit_predict.
237
209
238
210
Parameters
@@ -260,7 +232,7 @@ def predict_proba(self, X):
260
232
of the pipeline.
261
233
"""
262
234
Xt = X
263
- for name , transform in self .steps [:- 1 ]:
235
+ for _ , transform in self .steps [:- 1 ]:
264
236
if hasattr (transform , "fit_sample" ):
265
237
pass
266
238
else :
@@ -280,7 +252,7 @@ def decision_function(self, X):
280
252
of the pipeline.
281
253
"""
282
254
Xt = X
283
- for name , transform in self .steps [:- 1 ]:
255
+ for _ , transform in self .steps [:- 1 ]:
284
256
if hasattr (transform , "fit_sample" ):
285
257
pass
286
258
else :
@@ -300,7 +272,7 @@ def predict_log_proba(self, X):
300
272
of the pipeline.
301
273
"""
302
274
Xt = X
303
- for name , transform in self .steps [:- 1 ]:
275
+ for _ , transform in self .steps [:- 1 ]:
304
276
if hasattr (transform , "fit_sample" ):
305
277
pass
306
278
else :
@@ -320,7 +292,7 @@ def transform(self, X):
320
292
of the pipeline.
321
293
"""
322
294
Xt = X
323
- for name , transform in self .steps :
295
+ for _ , transform in self .steps :
324
296
if hasattr (transform , "fit_sample" ):
325
297
pass
326
298
else :
@@ -345,8 +317,8 @@ def inverse_transform(self, X):
345
317
" pipeline.inverse_transform any more." , FutureWarning )
346
318
X = X [None , :]
347
319
Xt = X
348
- for name , step in self .steps [::- 1 ]:
349
- if hasattr (transform , "fit_sample" ):
320
+ for _ , step in self .steps [::- 1 ]:
321
+ if hasattr (step , "fit_sample" ):
350
322
pass
351
323
else :
352
324
Xt = step .inverse_transform (Xt )
@@ -369,13 +341,11 @@ def score(self, X, y=None):
369
341
steps of the pipeline.
370
342
"""
371
343
Xt = X
372
- for name , transform in self .steps [:- 1 ]:
344
+ for _ , transform in self .steps [:- 1 ]:
373
345
if hasattr (transform , "fit_sample" ):
374
346
pass
375
347
else :
376
- print Xt .shape
377
348
Xt = transform .transform (Xt )
378
- print Xt .shape
379
349
return self .steps [- 1 ][- 1 ].score (Xt , y )
380
350
381
351
@@ -386,15 +356,6 @@ def make_pipeline(*steps):
386
356
does not permit, naming the estimators. Instead, their names will be set
387
357
to the lowercase of their types automatically.
388
358
389
- Examples
390
- --------
391
- >>> from sklearn.naive_bayes import GaussianNB
392
- >>> from sklearn.preprocessing import StandardScaler
393
- >>> make_pipeline(StandardScaler(), GaussianNB(priors=None)) # doctest: +NORMALIZE_WHITESPACE
394
- Pipeline(steps=[('standardscaler',
395
- StandardScaler(copy=True, with_mean=True, with_std=True)),
396
- ('gaussiannb', GaussianNB(priors=None))])
397
-
398
359
Returns
399
360
-------
400
361
p : Pipeline
0 commit comments