Skip to content

Instantly share code, notes, and snippets.

@amueller
Created July 28, 2016 21:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amueller/643f812a275a9e0c75048aab6988a92c to your computer and use it in GitHub Desktop.
Save amueller/643f812a275a9e0c75048aab6988a92c to your computer and use it in GitHub Desktop.
binary operators on all estimators
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
@amueller
Copy link
Author

On second though, should have called it "make @GaelVaroquaux cry"

@jnothman
Copy link

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.

@amueller
Copy link
Author

@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