【发布时间】:2019-06-11 11:42:30
【问题描述】:
我用fit 和transform 方法编写了一个自定义估算器类。我能够创建模型,使用该模型进行训练和预测。
但是,在进行交叉验证时,我遇到了这个错误:TypeError: cannot deepcopy this pattern object。
这就是CustomEstimator 的样子:
class DefaultEstimator(BaseEstimator, TransformerMixin):
def __init__(self, preprocessor, pipelines):
self.preprocessor = preprocessor
self.pipelines = pipelines
def fit(self, X, y=None):
for each_pipeline in self.pipelines:
each_pipeline.fit(self.preprocessor.apply(X), y)
return self
def transform(self, X):
transformed_data = []
for each_pipeline in self.pipelines:
transformed_data.append(each_pipeline.transform(self.preprocessor.apply(X)))
return sp.hstack(transformed_data)
有人对解决这个问题有想法吗?
【问题讨论】:
-
你能分享代码吗?从
BaseEstimator继承可以完美运行(参见github.com/luispedro/BuildingMachineLearningSystemsWithPython/… 示例)。 -
@MatthieuBrucher:添加了
CustomEstimator的代码 -
可能管道和预处理器不能深拷贝?你能检查一下吗?
-
@MatthieuBrucher 是的,这就是问题所在。预处理器不能是深拷贝。
标签: python machine-learning scikit-learn