Created
June 5, 2015 13:20
-
-
Save kingjr/4a3a038c5fa57bb57bcc to your computer and use it in GitHub Desktop.
ad hoc: Scaled Logistic Regression with probabilistic output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ad hoc: Scaled Logistic Regression with probabilistic output | |
| class force_predict(object): | |
| def __init__(self, clf, mode='predict_proba', axis=0): | |
| self._mode = mode | |
| self._axis = axis | |
| self._clf = clf | |
| def fit(self, X, y, **kwargs): | |
| self._clf.fit(X, y, **kwargs) | |
| self._copyattr() | |
| def predict(self, X): | |
| if self._mode == 'predict_proba': | |
| return self._clf.predict_proba(X)[:, self._axis] | |
| elif self._mode == 'decision_function': | |
| distances = self._clf.decision_function(X) | |
| if len(distances.shape) > 1: | |
| return distances[:, self._axis] | |
| else: | |
| return distances | |
| else: | |
| return self._clf.predict(X) | |
| def get_params(self, deep=True): | |
| return dict(clf=self._clf, mode=self._mode, axis=self._axis) | |
| def _copyattr(self): | |
| for key, value in self._clf.__dict__.iteritems(): | |
| self.__setattr__(key, value) | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.pipeline import Pipeline | |
| from sklearn.linear_model import LogisticRegression | |
| scaler = StandardScaler() | |
| clf = force_predict(LogisticRegression(penalty='l2', C=1), axis=1) | |
| pipeline = Pipeline([('scaler', scaler), ('clf', clf)]) | |
| # Area Under the Curve Scorer: | |
| def scorer_auc(y_true, y_pred): | |
| from sklearn.metrics import roc_auc_score | |
| from sklearn.preprocessing import LabelBinarizer | |
| le = LabelBinarizer() | |
| y_true = le.fit_transform(y_true) | |
| return roc_auc_score(y_true, y_pred) | |
| # GAT | |
| from mne.decoding import GeneralizationAcrossTime | |
| gat = GeneralizationAcrossTime(n_jobs=-1, clf=pipeline, scorer=auc_scorer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment