【问题标题】:RandomForest, how to choose the optimal n_estimator parameterRandomForest,如何选择最优的 n_estimator 参数
【发布时间】:2019-03-01 23:33:52
【问题描述】:

我想训练我的模型并选择最佳数量的树。代码在这里

from sklearn.ensemble import RandomForestClassifier

tree_dep = [3,5,6]
tree_n = [2,5,7]

avg_rf_f1 = []
search = []

for x in tree_dep:
  for y in tree_n:
    search.append((a,b))
    rf_model = RandomForestClassifier(n_estimators=tree_n, max_depth=tree_dep, random_state=42)
    rf_scores = cross_val_score(rf_model, X_train, y_train, cv=10, scoring='f1_macro')

    avg_rf_f1.append(np.mean(rf_scores))

best_tree_dep, best_n = search[np.argmax(avg_rf_f1)]

错误在这一行

rf_scores = cross_val_score(rf_model, X_train, y_train, cv=10, scoring='f1_macro')

ValueError: n_estimators must be an integer, got <class 'list'>.

想知道如何解决它。谢谢!!!

【问题讨论】:

  • 看看你的错误信息。你还没有通过int,你已经通过了整个listrf_model = RandomForestClassifier(n_estimators=y)
  • 是的,我明白了。但老实说,我真的是 python 的初学者。不知道怎么改。
  • 我在上面的评论中添加了答案。或查看 Piotrek 的回答
  • 我应该单独训练树的数量吗?像 rf_model = RandomForestClassifier(n_estimators=2) ; rf_model = RandomForestClassifier(n_estimators=5); rf_model = RandomForestClassifier(n_estimators=7) ?
  • 啊啊啊!!我知道了。我怎么没看到!!谢谢!

标签: python machine-learning scikit-learn random-forest cross-validation


【解决方案1】:

您在循环中遍历列表的元素,但不在循环中使用它们。您无需提供列表中的元素n_estimatorsmax_depth,而是提供整个列表。这应该可以解决它,现在在每次迭代中,您从两个列表中获取不同的元素组合:

from sklearn.ensemble import RandomForestClassifier

tree_dep = [3,5,6]
tree_n = [2,5,7]

avg_rf_f1 = []
search = []

for x in tree_dep:
  for y in tree_n:
    search.append((a,b))
    rf_model = RandomForestClassifier(n_estimators=y, max_depth=x, random_state=42)
    rf_scores = cross_val_score(rf_model, X_train, y_train, cv=10, scoring='f1_macro')

    avg_rf_f1.append(np.mean(rf_scores))

best_tree_dep, best_n = search[np.argmax(avg_rf_f1)]

【讨论】:

    【解决方案2】:

    在 scikit-learn 中有一个名为 GridSearchCV 的辅助函数可以做到这一点。它获取您要测试的参数值列表,并使用所有可能的参数集训练分类器以返回最佳参数集。
    我建议它比您正在实现的嵌套循环方法更清洁和更快。它很容易扩展到其他参数(只需将所需的参数添加到您的网格中)并且可以并行化。

    from sklearn.ensemble import RandomForestClassifier
    from sklearn.model_selection import GridSearchCV
    
    params_to_test = {
        'n_estimators':[2,5,7],
        'max_depth':[3,5,6]
    }
    
    #here you can put any parameter you want at every run, like random_state or verbosity
    rf_model = RandomForestClassifier(random_state=42)
    #here you specify the CV parameters, number of folds, numberof cores to use...
    grid_search = GridSearchCV(rf_model, param_grid=params_to_test, cv=10, scoring='f1_macro', n_jobs=4)
    
    grid_search.fit(X_train, y_train)
    
    best_params = grid_search.best_params_ 
    
    #best_params is a dict you can pass directly to train a model with optimal settings 
    best_model = RandomForestClassifier(**best_params)
    

    正如 cmets 中所指出的,最佳模型存储在 grid_search 对象中,因此不要使用以下方法创建新模型:

    best_model = RandomForestClassifier(**best_params)
    

    我们可以使用grid_search中的那个:

    best_model = grid_search.best_estimator_
    

    【讨论】:

    • n_cores 在 GridSearchCV 中应该是 n_jobs。此外,best_model == grid_search.best_estimator_ 比传递 best_params 更方便,尤其是在您使用管道等时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 2017-08-17
    • 2017-06-13
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多