python - 如何调整管道内随机森林分类器的参数?

标签 python scikit-learn random-forest gridsearchcv

我试图通过使用管道并调整其中的参数来应用 RandomForestClassifier()。这是正在使用的数据集:https://www.kaggle.com/gbonesso/enem-2016

这是代码

from sklearn.ensemble import RandomForestClassifier

imputer = SimpleImputer(strategy="median")
scaler = StandardScaler()
rf = RandomForestClassifier()

features = [
    "NU_IDADE",
    "TP_ESTADO_CIVIL",
    "NU_NOTA_CN",
    "NU_NOTA_CH",
    "NU_NOTA_LC",
    "NU_NOTA_MT",
    "NU_NOTA_COMP1",
    "NU_NOTA_COMP2",
    "NU_NOTA_COMP3",
    "NU_NOTA_COMP4",
    "NU_NOTA_COMP5",
    "NU_NOTA_REDACAO",
]

X = enem[features]
y = enem[["IN_TREINEIRO"]]

X_train, X_test, y_train, y_test = train_test_split(
    X, y, train_size=0.8, random_state=42
)

pipeline = make_pipeline(imputer, scaler, rf)

pipe_params = {
    "randomforestregressor__n_estimators": [100, 500, 1000],
    "randomforestregressor__max_depth": [1, 5, 10, 25],
    "randomforestregressor__max_features": [*np.arange(0.1, 1.1, 0.1)],
}

gridsearch = GridSearchCV(
    pipeline, param_grid=pipe_params, cv=3, n_jobs=-1, verbose=1000
)

gridsearch.fit(X_train, y_train)

它似乎适用于一些参数,但随后我收到此错误消息:

ValueError: Invalid parameter randomforestregressor for estimator Pipeline(steps=[('simpleimputer', SimpleImputer(strategy='median')),
            ('standardscaler', StandardScaler()),
            ('randomforestclassifier', RandomForestClassifier())]). Check the list of available parameters with `estimator.get_params().keys()`.

此外,还有一个问题是我似乎无法获得简历结果。我尝试运行以下代码:

results = pd.DataFrame(gridsearch.cv_results_)
results.sort_values("rank_test_score").head()
score = pipeline.score(X_test, y_test)
score

但是我收到了这个错误:

AttributeError: 'GridSearchCV' object has no attribute 'cv_results_'

关于如何修复这些错误有什么想法吗?

最佳答案

您的问题可能是这本字典:

pipe_params = {
    "randomforestregressor__n_estimators": [100, 500, 1000],
    "randomforestregressor__max_depth": [1, 5, 10, 25],
    "randomforestregressor__max_features": [*np.arange(0.1, 1.1, 0.1)],
}

您的管道没有 randomforestregressor 参数,如您的错误所示。由于您使用的是 RandomForestClassifier,因此应该是:

pipe_params = {
    "randomforestclassifier__n_estimators": [100, 500, 1000],
    "randomforestclassifier__max_depth": [1, 5, 10, 25],
    "randomforestclassifier__max_features": [*np.arange(0.1, 1.1, 0.1)],
}

如果您运行错误消息中的建议,您将看到管道的可用选项 (pipeline.get_params().keys())。

关于python - 如何调整管道内随机森林分类器的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64089009/

相关文章:

python - PyQt- 哪一列获得了右键单击?

Python (Twisted) - 从 fifo 读取并将读取的数据发送到多个协议(protocol)

python - 有没有办法在 Python 的 re.sub() 中的替换字符串中使用正则表达式?

python - 如何使用sklearn计算具有二进制相关性的NDCG?

r - 使用 step_naomit 进行预测并使用 tidymodels 保留 ID

python - 优化对象显示(gamedev)

python - 使用交叉验证评估逻辑回归

python - FutureWarning get_params 来自 scikit-learn

python - 如何在随机森林分类器中找到最大深度?

python - 如何从 Sklearn 管道中提取特征重要性