Created
July 28, 2016 21:02
-
-
Save amueller/643f812a275a9e0c75048aab6988a92c to your computer and use it in GitHub Desktop.
binary operators on all estimators
This file contains 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
from sklearn.base import BaseEstimator | |
def piper(self, other): | |
from sklearn.pipeline import make_pipeline, Pipeline | |
if isinstance(self, Pipeline): | |
steps = ([estimator for (name, estimator) in self.steps] + [other]) | |
return make_pipeline(*steps) | |
else: | |
return make_pipeline(self, other) | |
def unionizer(self, other): | |
from sklearn.pipeline import make_union, FeatureUnion | |
if isinstance(self, FeatureUnion): | |
steps = ([estimator for (name, estimator) in self.steps] + [other]) | |
return make_union(*steps) | |
else: | |
return make_union(self, other) | |
BaseEstimator.__rshift__ = piper | |
BaseEstimator.__mul__ = unionizer |
Oh, no! You forgot to add that parameter renaming magic I once wrote so that all double-underscored parameter settings are eradicated forever and replaced with custom incantations. Without parameter renaming magic, est
is far from substitutable with est >> est2
.
@jnothman sorry just saw this. But I'm not following
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On second though, should have called it "make @GaelVaroquaux cry"