【问题标题】:Grid search ValueError: Invalid parameter classifier for estimator网格搜索 ValueError:估计器的参数分类器无效
【发布时间】:2021-05-27 03:30:25
【问题描述】:

我正在尝试将随机森林与网格搜索一起使用,但出现此错误

ValueError: Invalid parameter classifier for estimator Pipeline(steps=[('tfidf_vectorizer', TfidfVectorizer()),
                ('rf_classifier', RandomForestClassifier())]). 
Check the list of available parameters with `estimator.get_params().keys()`.
import numpy as np # linear algebra
import pandas as pd
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn import pipeline,ensemble,preprocessing,feature_extraction,metrics
train=pd.read_json('cleaned_data1')
#split dataset into X , Y
X=train.iloc[:,0]
Y=train.iloc[:,2]

estimators=pipeline.Pipeline([
        ('tfidf_vectorizer', feature_extraction.text.TfidfVectorizer(lowercase=True)),
        ('rf_classifier', ensemble.RandomForestClassifier())
    ])

print(estimators.get_params().keys())

params = {"classifier__max_depth": [3, None],
              "classifier__max_features": [1, 3, 10],
              "classifier__min_samples_split": [1, 3, 10],
              "classifier__min_samples_leaf": [1, 3, 10],
              # "bootstrap": [True, False],
              "classifier__criterion": ["gini", "entropy"]}

X_train,X_test,y_train,y_test=train_test_split(X,Y, test_size=0.2)

rf_classifier=GridSearchCV(estimators,params, cv=10 , n_jobs=-1 ,scoring='accuracy',iid=True)

rf_classifier.fit(X_train,y_train)

y_pred=rf_classifier.predict(X_test)

metrics.confusion_matrix(y_test,y_pred)
print(metrics.accuracy_score(y_test,y_pred))

我已经尝试添加这些参数

param_grid = {
    'n_estimators': [200, 500],
    'max_features': ['auto', 'sqrt', 'log2'],
    'max_depth' : [4,5,6,7,8],
    'criterion' :['gini', 'entropy']
}

但还是同样的错误

【问题讨论】:

  • 您的管道中没有任何classifier - 有一个rf_classifier
  • 您可以尝试使用 AutoML 训练 RF github.com/mljar/mljar-supervised 您可以仅使用 RF 调用 AutoML 进行优化:AutoML(algorithms=['Random Forest'], mode='Compete') 并且适合 AutoML 进行超参数搜索:automl.fit(X, y)

标签: python machine-learning scikit-learn random-forest grid-search


【解决方案1】:

请确保当您在管道中引用某些内容时,在初始化参数网格时使用相同的命名约定。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV


# Define a pipeline to search for the best combination of PCA truncation
# and classifier regularization.
pca = PCA()
# set the tolerance to a large value to make the example faster
logistic = LogisticRegression(max_iter=10000, tol=0.1)
pipe = Pipeline(steps=[('pca', pca), ('logistic', logistic)])

X_digits, y_digits = datasets.load_digits(return_X_y=True)

# Parameters of pipelines can be set using ‘__’ separated parameter names:
param_grid = {
    'pca__n_components': [5, 15, 30, 45, 64],
    'logistic__C': np.logspace(-4, 4, 4),
}
search = GridSearchCV(pipe, param_grid, n_jobs=-1)
search.fit(X_digits, y_digits)
print("Best parameter (CV score=%0.3f):" % search.best_score_)
print(search.best_params_)

在本例中,我们将 LogisticRegression 模型称为“logistic”。另外请注意,对于 RandomForestClassifiers,min_samples_split = 1 的值是不可能的,并且会导致错误。

This is from the sklearn documentation

【讨论】:

    【解决方案2】:

    您在管道中调用了随机森林集合“rf_classifier”,您应该将其重命名为“分类器”,这应该可以解决问题。

    参数在管道中查找名为“分类器”的东西,以便它们可以应用自己,但是目前没有任何名称,因此会引发此错误。 如果您愿意(我不确定这是否可行但值得测试),您可以将参数列表中的“classifier__”更改为“rf_classifier__”,以查看参数是否会识别传递的分类器。

    【讨论】:

    • 我将分类器名称更改为“分类器”,但仍然遇到同样的问题。
    猜你喜欢
    • 2020-05-19
    • 2018-06-28
    • 2021-08-13
    • 2016-12-20
    • 2021-02-26
    • 2020-08-11
    • 2020-07-12
    • 2016-04-07
    • 2016-01-03
    相关资源
    最近更新 更多