@@ -59,10 +59,12 @@ def _predict_estimator(clf, X):
59
59
60
60
def _predict_proba_estimator (clf , X ):
61
61
"""Helper to get prediction method"""
62
+
63
+ # XXX this is not safe. Maybe add explicit 1st level scoring param.
62
64
# try predict_proba
63
65
predict_proba = getattr (clf , "predict_proba" , None )
64
66
if callable (predict_proba ):
65
- return clf .predict_proba (X )[:, 0 ]
67
+ return clf .predict_proba (X )
66
68
67
69
# or decision_function
68
70
decision_function = getattr (clf , "decision_function" , None )
@@ -131,6 +133,9 @@ def __init__(self, estimators,
131
133
self .feature_indices = feature_indices
132
134
self .n_jobs = n_jobs
133
135
136
+ def _disambiguate_probability (self , x ):
137
+ return x [:, - 1 ] if np .ndim (x ) > 1 else x
138
+
134
139
def fit (self , X , y ):
135
140
"""Fit all estimators according to the given training data.
136
141
@@ -154,6 +159,8 @@ def fit(self, X, y):
154
159
predictions_ = Parallel (n_jobs = self .n_jobs )(
155
160
delayed (_predict_proba_estimator )(clf , x )
156
161
for x , clf in zip (X_list , self .estimators ))
162
+ predictions_ = [self ._disambiguate_probability (x )
163
+ for x in predictions_ ]
157
164
predictions_ = np .array (predictions_ ).T
158
165
159
166
self .stacking_estimator .fit (predictions_ , y )
@@ -177,6 +184,8 @@ def predict(self, X):
177
184
predictions_ = Parallel (n_jobs = self .n_jobs )(
178
185
delayed (_predict_proba_estimator )(clf , x )
179
186
for x , clf in zip (X_list , self .estimators ))
187
+ predictions_ = [self ._disambiguate_probability (x )
188
+ for x in predictions_ ]
180
189
predictions_ = np .array (predictions_ ).T
181
190
182
191
return self .stacking_estimator .predict (predictions_ )
@@ -199,12 +208,14 @@ def predict_proba(self, X):
199
208
predictions_ = Parallel (n_jobs = self .n_jobs )(
200
209
delayed (_predict_proba_estimator )(clf , x )
201
210
for x , clf in zip (X_list , self .estimators ))
211
+ predictions_ = [self ._disambiguate_probability (x )
212
+ for x in predictions_ ]
202
213
predictions_ = np .array (predictions_ ).T
203
214
204
215
return _predict_proba_estimator (self .stacking_estimator , predictions_ )
205
216
206
217
def decision_function (self , X ):
207
- return self .predict_proba (X )
218
+ return self ._disambiguate_probability ( self . predict_proba (X ) )
208
219
209
220
def score (self , X , y ):
210
221
"""Returns the mean accuracy on the given test data and labels.
0 commit comments