【问题标题】:Pipeline issues in sklearn [closed]sklearn中的管道问题[关闭]
【发布时间】: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)`

如何解决这个问题?

【问题讨论】:

标签: scikit-learn pipeline


【解决方案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 个特征。
猜你喜欢
  • 2012-12-06
  • 2021-03-06
  • 1970-01-01
  • 2020-04-16
  • 2021-11-30
  • 1970-01-01
  • 2016-07-06
  • 2010-12-29
  • 2017-12-07
相关资源
最近更新 更多