【问题标题】:In GridSearchCV, how do I pass only the default parameters in param_grid?在 GridSearchCV 中,如何只传递 param_grid 中的默认参数?
【发布时间】:2017-12-11 22:54:24
【问题描述】:

我是初学者,下面有以下代码。

from sklearn.naive_bayes import GaussianNB
from sklearn.decomposition import PCA

pca = PCA()
model = GaussianNB()
steps = [('pca', pca), ('model', model)]
pipeline = Pipeline(steps)

cv = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=42)
modelwithpca = GridSearchCV(pipeline, param_grid= ,cv=cv)
modelwithpca.fit(X_train,y_train)

这是一个本地测试,我想要完成的是,

我。对数据集执行 PCA

二。仅使用默认参数使用高斯朴素贝叶斯

三。使用 StratifiedShuffleSplit

所以最后我希望将上述步骤转移到另一个转储分类器、数据集和特征列表的函数中,以测试性能。

dump_classifier_and_data(modelwithpca, dataset, features)  

在 param_grid 部分,我不想测试任何参数列表。如果有意义的话,我只想在高斯朴素贝叶斯中使用默认参数。我要改变什么?

关于我如何实例化分类器对象也应该有任何改变吗?

【问题讨论】:

    标签: python machine-learning scikit-learn sklearn-pandas grid-search


    【解决方案1】:

    GridSearchCV 的目的是针对管道中的至少一件事使用不同的参数进行测试(如果您不想测试不同的参数,则不需要使用GridSearchCV)。 所以,一般来说,如果你想测试不同的PCAn_components。 使用GridSearchCV 的管道格式如下:

    gscv = GridSearchCV(pipeline, param_grid={'{step_name}__{parameter_name}': [possible values]}, cv=cv)
    

    例如:

    # this would perform cv for the 3 different values of n_components for pca
    gscv = GridSearchCV(pipeline, param_grid={'pca__n_components': [3, 6, 10]}, cv=cv)
    

    如果您如上所述使用GridSearchCV 调整PCA,这当然意味着您的模型将具有默认值。

    如果您不需要参数调整,那么GridSearchCV 不是要走的路,因为像这样为 GridSearchCV 使用模型的默认参数,只会产生一个组合的参数网格,所以它就像只表演简历。 这样做没有意义 - 如果我正确理解了你的问题:

    from sklearn.naive_bayes import GaussianNB
    from sklearn.decomposition import PCA
    from sklearn.pipeline import Pipeline
    
    pca = PCA()
    model = GaussianNB()
    steps = [('pca', pca), ('model', model)]
    pipeline = Pipeline(steps)
    
    cv = StratifiedShuffleSplit(n_splits=5, test_size=0.2, random_state=42)
    # get the default parameters of your model and use them as a param_grid
    modelwithpca = GridSearchCV(pipeline, param_grid={'model__' + k: [v] for k, v in model.get_params().items()}, cv=cv)
    
    # will run 5 times as your cv is configured
    modelwithpca.fit(X_train,y_train)
    

    希望这有帮助,祝你好运!

    【讨论】:

      猜你喜欢
      • 2012-08-06
      • 2011-12-24
      • 2013-02-20
      • 2014-04-09
      • 1970-01-01
      • 2016-01-02
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多