Skip to content

Commit 166fe21

Browse files
committed
add predict proba estimator
1 parent ee19b40 commit 166fe21

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

stlearn/stacking.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ class StackingClassifier(BaseEstimator, ClassifierMixin, TransformerMixin):
115115
features_indices : list of indexers
116116
Index epxressions to be applied on the columns of X_stacked.
117117
Can be slices, lists of intgers or bool.
118+
119+
n_jobs : int (default: 1)
120+
The number of jobs to run in parallel (across estimators).
118121
"""
119122

120123
def __init__(self, estimators,
@@ -260,6 +263,27 @@ def predict_estimators(self, X):
260263
for x, clf in zip(X_list, self.estimators))
261264
return np.array(predictions_).T
262265

266+
def predict_proba_estimators(self, X):
267+
"""Predict class label probability for samples in X for each estimators.
268+
269+
Parameters
270+
----------
271+
X : {array-like, sparse matrix}, shape = (n_samples, n_features)
272+
The multi-input samples.
273+
274+
Returns
275+
-------
276+
C : array, shape = (n_samples, n_classes, n_estimators)
277+
Predicted class label per sample and estimators.
278+
"""
279+
_check_Xy(self, X)
280+
X_list = _split_features(X, self.feature_indices)
281+
predictions_ = Parallel(n_jobs=self.n_jobs)(
282+
delayed(_predict_proba_estimator)(clf, x)
283+
for x, clf in zip(X_list, self.estimators))
284+
return np.transpose(np.array(predictions_),
285+
(1, 2, 0))
286+
263287
def score_estimators(self, X, y):
264288
"""Returns the mean accuracy for each estimators.
265289

stlearn/tests/test_stacking.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def test_stacking_essentials():
7070
proba = stacking.predict_proba(X_stacked)
7171
assert_array_equal(proba.sum(1), np.ones_like(proba[:, 1]))
7272

73+
proba_estimators = stacking.predict_proba_estimators(X_stacked)
74+
for proba in np.transpose(proba_estimators, (2, 0, 1)):
75+
assert_array_equal(proba.sum(1),
76+
np.ones_like(proba[:, 1]))
77+
7378
score = stacking.score(X_stacked, y)
7479
assert_true(np.isscalar(score))
7580

0 commit comments

Comments
 (0)