|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Prediction stacking API |
| 4 | +""" |
| 5 | +# Author: Mehdi Rahim <rahim.mehdi@gmail.com> |
| 6 | +# |
| 7 | +# License: BSD 3 clause |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +from sklearn.base import BaseEstimator, TransformerMixin, ClassifierMixin |
| 11 | +from sklearn.metrics import accuracy_score |
| 12 | +from sklearn.externals.joblib import Memory, Parallel, delayed |
| 13 | + |
| 14 | + |
| 15 | +def fit_estimator(clf, X, y): |
| 16 | + return clf.fit(X, y) |
| 17 | + |
| 18 | + |
| 19 | +def predict_estimator(clf, X): |
| 20 | + return clf.predict(X) |
| 21 | + |
| 22 | + |
| 23 | +def predict_proba_estimator(clf, X): |
| 24 | + # try predict_proba |
| 25 | + predict_proba = getattr(clf, "predict_proba", None) |
| 26 | + if callable(predict_proba): |
| 27 | + return clf.predict_proba(X)[:, 0] |
| 28 | + |
| 29 | + # or decision_function |
| 30 | + decision_function = getattr(clf, "decision_function", None) |
| 31 | + if callable(decision_function): |
| 32 | + return clf.decision_function(X) |
| 33 | + |
| 34 | + raise NotImplementedError("predict_proba not supported") |
| 35 | + |
| 36 | + |
| 37 | +class StackingClassifier(BaseEstimator, ClassifierMixin, TransformerMixin): |
| 38 | + """Meta-classifier of 3D X matrix with labels |
| 39 | + """ |
| 40 | + |
| 41 | + def __init__(self, estimators=None, |
| 42 | + stacking_estimator=None, |
| 43 | + memory=Memory(cachedir=None), memory_level=0, |
| 44 | + n_jobs=1): |
| 45 | + """ initialization |
| 46 | + """ |
| 47 | + self.estimators = estimators |
| 48 | + self.stacking_estimator = stacking_estimator |
| 49 | + self.memory = memory |
| 50 | + self.memory_level = memory_level |
| 51 | + self.n_jobs = n_jobs |
| 52 | + |
| 53 | + def fit(self, X, y): |
| 54 | + """ stacking model fitting |
| 55 | + X is 3D matrix |
| 56 | + """ |
| 57 | + |
| 58 | + self.estimators = Parallel(n_jobs=self.n_jobs)( |
| 59 | + delayed(fit_estimator)(clf, x, y) |
| 60 | + for x, clf in zip(X, self.estimators)) |
| 61 | + |
| 62 | + predictions_ = Parallel(n_jobs=self.n_jobs)( |
| 63 | + delayed(predict_proba_estimator)(clf, x) |
| 64 | + for x, clf in zip(X, self.estimators)) |
| 65 | + predictions_ = np.array(predictions_).T |
| 66 | + |
| 67 | + self.stacking_estimator.fit(predictions_, y) |
| 68 | + return self |
| 69 | + |
| 70 | + def predict(self, X): |
| 71 | + """ stacking model prediction |
| 72 | + X is 3D matrix |
| 73 | + """ |
| 74 | + |
| 75 | + predictions_ = Parallel(n_jobs=self.n_jobs)( |
| 76 | + delayed(predict_proba_estimator)(clf, x) |
| 77 | + for x, clf in zip(X, self.estimators)) |
| 78 | + predictions_ = np.array(predictions_).T |
| 79 | + |
| 80 | + return self.stacking_estimator.predict(predictions_) |
| 81 | + |
| 82 | + def score(self, X, y): |
| 83 | + """ stacking model accuracy |
| 84 | + """ |
| 85 | + return accuracy_score(y, self.predict(X)) |
| 86 | + |
| 87 | + def predict_estimators(self, X): |
| 88 | + """ prediction from separate estimators |
| 89 | + """ |
| 90 | + predictions_ = Parallel(n_jobs=self.n_jobs)( |
| 91 | + delayed(predict_estimator)(clf, x) |
| 92 | + for x, clf in zip(X, self.estimators)) |
| 93 | + return np.array(predictions_).T |
| 94 | + |
| 95 | + def score_estimators(self, X, y): |
| 96 | + """ accuracy from separate estimators |
| 97 | + """ |
| 98 | + predictions_ = self.predict_estimators(X) |
| 99 | + return np.array([accuracy_score(y, p) for p in predictions_.T]) |
0 commit comments