sklearn中的管道问题[关闭]
Posted
技术标签:
【中文标题】sklearn中的管道问题[关闭]【英文标题】:Pipeline issues in sklearn [closed] 【发布时间】:2022-01-16 23:26:56 【问题描述】:我有一个神经网络模型,我希望它适合我的训练数据。当我编译下面的代码行时
history = pipeline.fit(inputs[train], targets[train], epochs=epochs, batchsize=batchsize)
我收到以下错误消息:
Pipeline.fit does not accept the epochs parameter. You can pass parameters to specific
steps of your pipeline using the stepname__parameter format, e.g.
`Pipeline.fit(X, y, logisticregression__sample_weight=sample_weight)`
如何解决这个问题?
【问题讨论】:
请发minimal reproducible example;通过提供的信息(未),不可能知道epochs
参数在此处如何涉及。
【参考方案1】:
我不认为epochs
参数是为MLPClassifier
定义的,你应该改用max_iter
参数。
那么如果你想在Pipeline
中指定超参数,你可以这样做:
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.impute import SimpleImputer
from sklearn.datasets import make_classification
X, y = make_classification()
model = make_pipeline(SimpleImputer(), StandardScaler(), MLPClassifier())
params =
'mlpclassifier__max_iter' : 10,
'mlpclassifier__batch_size' : 20
model.set_params(**params)
model.fit(X, y)
我建议使用这种表示法,因为您可以轻松地重复使用它来执行GridSearchCV
。
【讨论】:
谢谢你,安托万。有没有办法在管道中包含“估算”?我有一个包含几个 NaN 值的数据集,所以我必须通过插补来消除这些值。 我在示例中添加了SimpleImputer
。
嗨 Antoine,我试过你的例子。但是,不知何故,它不起作用。我无法解码以下错误消息。 “X 有 14 个特征,但 SimpleImputer 预计有 20 个特征作为输入。”输入 X 在我的数据集中有 14 个特征,但我无法理解为什么 SimpleImputer 需要 20 个特征。以上是关于sklearn中的管道问题[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何从管道中的 sklearn TFIDF Vectorizer 返回数据帧?