from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC from sklearn.pipeline import Pipeline, make_pipeline class FactoryEstimator(object): def __repr__(self): return "ConstructedEstimator(" + str(self.params) + ")" # for "is_classifier" ugh @property def steps(self): return self.constructor(self.params).steps def __init__(self, **params): self.params = params self.constructor = params['constructor'] def get_params(self, deep=None): return self.params def set_params(self, **params): self.params.update(params) return self def fit(self, X, y): self.estimator_ = self.constructor(self.params) self.estimator_.fit(X, y) return self def predict(self, X): return self.estimator_.predict(X) func = lambda x: make_pipeline(StandardScaler(with_mean=x['with_mean']), SVC(gamma=x['gamma'], C=x['C'])) est = FactoryEstimator(constructor=func, with_mean=True, C=1, gamma=.1) grid = GridSearchCV(est, param_grid={'C': [.1, 1, 10], 'gamma': [0.001, 0.1, 1, 0]}, scoring='accuracy') grid.fit(X, y) print(grid.best_score_) est = ConstructedEstimator(constructor=func, with_mean=True, C=1, gamma=.1) from sklearn.datasets import load_iris iris = load_iris() X, y = iris.data, iris.target est.fit(X, y) est.predict(X) from sklearn.grid_search import GridSearchCV grid.fit(X, y) grid.best_estimator_ grid.best_estimator_ grid.predict(X) from sklearn.cross_validation import cross_val_score est = ConstructedEstimator(constructor=func, with_mean=True, C=1, gamma=.1) cross_val_score(est, X, y, scoring="accuracy") est.fit(X, y) bla = func({'C':1, 'with_mean': True, 'gamma': 0.1}) bla.fit(X, y) bla.score(X, y) cross_val_score(bla, X, y, scoring="accuracy") est.steps_