【问题标题】:Issues with Pipeline and GridSearchCV管道和 GridSearchCV 的问题
【发布时间】:2020-09-20 08:02:30
【问题描述】:

我在使用 Pipeline Tool 和 GridSearchCV 时遇到了一些问题。 我收到以下错误消息:“TypeError:Pipeline 的最后一步应该实现 fit 或者是字符串 'passthrough'。'1' (type) 没有”。

你知道我的错误在哪里吗?

这是我的代码:

from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline


X = wage['age'][:, np.newaxis]
y = wage['wage'][:, np.newaxis]

degree = 2
model = Pipeline(steps=[('poly', PolynomialFeatures(degree)), ('linear', LinearRegression(fit_intercept=False))])

param_grid = {'poly': [1, 2, 3, 4, 5]}
cv_model = GridSearchCV(model, param_grid, scoring='r2', cv=5, n_jobs=1)
cv_model.fit(X, y)

【问题讨论】:

标签: python scikit-learn pipeline grid-search


【解决方案1】:

问题可能来自我没有在 param_grid 中的“poly”之后使用“__”表示法。

使用这种表示法,它似乎可以工作:

param_grid = {'poly__degree': [1, 2, 3, 4, 5]}

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2018-07-05
    • 2021-11-26
    • 2014-01-29
    • 2018-06-12
    • 2013-07-18
    • 2020-11-05
    • 2020-09-24
    相关资源
    最近更新 更多