【问题标题】:pipeline and cross validation in python using scikit learn使用 scikit learn 在 python 中进行管道和交叉验证
【发布时间】:2020-10-28 00:59:28
【问题描述】:

我对交叉验证有一个普遍的疑问。

在模块 2 的笔记本中提到应该使用管道进行交叉验证,以防止数据泄漏。我明白为什么,但是对管道功能有疑问:

如果我想在管道中使用三个函数:MinMaxScaler()PolynomialFeatures(用于多个度数)和最后一个 Ridge(用于多个 alpha 值)。由于我想在使用多个参数值后找到最佳模型,因此我将使用 GridSearchCV() 函数进行交叉验证并给出最佳模型分数。

但是,在我用三个函数初始化管道对象并将其插入GridSearchCV() 函数后,如何在GridSearchCV() 函数的params 参数中插入多个度数和aplha 值。我是按照在管道对象中定义函数的顺序将参数作为两个列表的列表插入,还是发送两个列表的字典,其中键是管道中函数的对象名称? ???

【问题讨论】:

  • 你能把你的代码添加到这个问题中吗?这将使您更容易理解您遇到的问题

标签: python scikit-learn pipeline cross-validation polynomials


【解决方案1】:

您只需将其作为字典输入即可。

试试这个例子:

from sklearn.preprocessing import MinMaxScaler, PolynomialFeatures
from sklearn.linear_model import Ridge
from sklearn.pipeline import make_pipeline
from sklearn.datasets import make_regression
from sklearn.model_selection import GridSearchCV

X, y = make_regression(random_state=42)

pipe = make_pipeline(MinMaxScaler(), PolynomialFeatures(),  Ridge())

pipe
# Pipeline(steps=[('minmaxscaler', MinMaxScaler()),
#                 ('polynomialfeatures', PolynomialFeatures()),
#                 ('ridge', Ridge())])

gs = GridSearchCV(pipe, param_grid={'polynomialfeatures__degree': [2,4],
                                    'ridge__alpha': [1,10]}).fit(X, y)

# gs.best_params_
# {'polynomialfeatures__degree': 2, 'ridge__alpha': 1}

【讨论】:

    猜你喜欢
    • 2015-06-22
    • 2012-10-14
    • 2017-04-12
    • 2021-10-25
    • 2015-11-13
    • 2016-04-25
    • 2018-03-30
    • 2017-09-02
    • 2020-03-07
    相关资源
    最近更新 更多