【发布时间】:2020-07-01 05:27:29
【问题描述】:
我正在 python 中构建一个随机森林二元分类器,该数据集包含 4898 个实例、60-40 个分层拆分率和 78% 的数据属于一个目标标签,其余数据属于另一个。为了实现最实用/最好的随机森林分类器模型,我应该选择什么值的 n_estimators?我使用下面的代码 sn-p 绘制了准确度与 n_estimators 曲线。 x_trai 和 y_train 分别是训练集中的特征和目标标签,x_test 和 y_test 分别是测试集中的特征和目标标签。
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
scores =[]
for k in range(1, 200):
rfc = RandomForestClassifier(n_estimators=k)
rfc.fit(x_train, y_train)
y_pred = rfc.predict(x_test)
scores.append(accuracy_score(y_test, y_pred))
import matplotlib.pyplot as plt
%matplotlib inline
# plot the relationship between K and testing accuracy
# plt.plot(x_axis, y_axis)
plt.plot(range(1, 200), scores)
plt.xlabel('Value of n_estimators for Random Forest Classifier')
plt.ylabel('Testing Accuracy')
在这里,可以看出 n_estimators 的高值会给出一个很好的准确度分数,但即使对于附近的 n_estimators 值,它也会在曲线中随机波动,所以我不能精确地选择最好的。我只想知道n_estimators超参数的调优,应该怎么选,请帮忙。我应该使用 ROC 曲线还是 CAP 曲线而不是 accuracy_score?谢谢。
【问题讨论】:
-
您应该在性能开始稳定在曲线上的那一刻选择一个值。您不应该尝试选择特定值,n_estimator 的两个接近值之间的性能差异来自随机性导致的可变性,不会复制到新数据中
-
逐步细化是提高效率的一种方法。尝试使用 GridSearch 和交叉折叠来找到最佳参数
标签: python classification random-forest hyperparameters