随机森林算法的参数调优(一)
在构建模型时我们会设置一些参数值:
model = RandomForestClassifier(max_depth=2, min_samples_leaf=20,n_estimators=20,random_state=1)
model.fit(X_train, y_train)
其中:
max_features: 允许单个决策树使用特征的最大数量
min_sample_leaf:最小样本叶片大小
n_estimators:建立子树的数量,较多的子树可以让模型有更好的性能,但同时让你的代码变慢。
这些参数的值会对模型的拟合程度产生影响,所以我通过参数调优来确定模型的最佳参数,从而使模型预测的准确率更高。
参数调优的代码段:
from sklearn.model_selection import GridSearchCV
parameters = {'n_estimators':[5, 10, 20,25,30], 'max_depth':[2, 3, 4, 5,6,7,8,9,10], 'min_samples_leaf':[5, 10, 20, 30,40,50]}
new_model = RandomForestClassifier(random_state=1)
grid_search = GridSearchCV(new_model, parameters, cv=6, scoring='accuracy')
grid_search.fit(X_train, y_train)
grid_search.best_params_
print('最优模型参数: ' + str(grid_search.best_params_))