多种降维技术与管道和GridSearchCV的应用

我们都知道常见的方法是定义一个包含降维技术的管道,然后是一个用于训练和测试的模型。接着我们可以应用GridSearchCV进行超参数调优。

grid = GridSearchCV(Pipeline([    ('reduce_dim', PCA()),    ('classify', RandomForestClassifier(n_jobs = -1))    ]),param_grid=[    {        'reduce_dim__n_components': range(0.7,0.9,0.1),        'classify__n_estimators': range(10,50,5),        'classify__max_features': ['auto', 0.2],        'classify__min_samples_leaf': [40,50,60],        'classify__criterion': ['gini', 'entropy']    }],cv=5, scoring='f1')grid.fit(X,y)

我能理解上述代码。

今天我在查看文档时,发现了一段有点奇怪的代码。

pipe = Pipeline([    # the reduce_dim stage is populated by the param_grid    ('reduce_dim', 'passthrough'),                        # 这是如何工作的?    ('classify', LinearSVC(dual=False, max_iter=10000))])N_FEATURES_OPTIONS = [2, 4, 8]C_OPTIONS = [1, 10, 100, 1000]param_grid = [    {        'reduce_dim': [PCA(iterated_power=7), NMF()],        'reduce_dim__n_components': N_FEATURES_OPTIONS,   ### 没有使用PCA..?        'classify__C': C_OPTIONS    },    {        'reduce_dim': [SelectKBest(chi2)],        'reduce_dim__k': N_FEATURES_OPTIONS,        'classify__C': C_OPTIONS    },]reducer_labels = ['PCA', 'NMF', 'KBest(chi2)']grid = GridSearchCV(pipe, n_jobs=1, param_grid=param_grid)X, y = load_digits(return_X_y=True)grid.fit(X, y)
  1. 首先,在定义管道时,它使用了一个字符串’passthrough’而不是一个对象。

        ('reduce_dim', 'passthrough'),  ```
  2. 然后,在为网格搜索定义不同的降维技术时,它使用了一种不同的策略。 [PCA(iterated_power=7), NMF()] 这是如何工作的?
            'reduce_dim': [PCA(iterated_power=7), NMF()],        'reduce_dim__n_components': N_FEATURES_OPTIONS,  # 这里 

请有人帮我解释一下这段代码。

已解决 – 一句话来说,顺序是 ['PCA', 'NMF', 'KBest(chi2)']

感谢seralouk(见下面的回答)

参考 如果有人需要更多细节1 2 3


回答:

据我所知,这是等价的。


在文档中,你有以下内容:

pipe = Pipeline([    # the reduce_dim stage is populated by the param_grid    ('reduce_dim', 'passthrough'),    ('classify', LinearSVC(dual=False, max_iter=10000))])N_FEATURES_OPTIONS = [2, 4, 8]C_OPTIONS = [1, 10, 100, 1000]param_grid = [    {        'reduce_dim': [PCA(iterated_power=7), NMF()],        'reduce_dim__n_components': N_FEATURES_OPTIONS,        'classify__C': C_OPTIONS    },    {        'reduce_dim': [SelectKBest(chi2)],        'reduce_dim__k': N_FEATURES_OPTIONS,        'classify__C': C_OPTIONS    },]

最初我们有 ('reduce_dim', 'passthrough'), 然后是 'reduce_dim': [PCA(iterated_power=7), NMF()]

PCA的定义是在第二行完成的。


你可以选择以下替代定义方式:

pipe = Pipeline([    # the reduce_dim stage is populated by the param_grid    ('reduce_dim', PCA(iterated_power=7)),    ('classify', LinearSVC(dual=False, max_iter=10000))])N_FEATURES_OPTIONS = [2, 4, 8]C_OPTIONS = [1, 10, 100, 1000]param_grid = [    {        'reduce_dim__n_components': N_FEATURES_OPTIONS,        'classify__C': C_OPTIONS    },    {        'reduce_dim': [SelectKBest(chi2)],        'reduce_dim__k': N_FEATURES_OPTIONS,        'classify__C': C_OPTIONS    },]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注