Python - 随机森林 - 迭代添加树
Python - Random Forest - Iteratively adding trees
我正在 Python 上做一些机器学习任务。我需要构建 RandomForest,然后构建一个图表来显示训练和测试样本的质量如何取决于随机森林中的树数。是否需要每次用一定数量的树建立一个新的随机森林?或者我可以以某种方式迭代添加树(如果可能的话,你能给出代码示例如何做到这一点)吗?
您可以使用 RandomForestClassifier
的 warm start
参数来做到这一点。
这是一个您可以根据自己的具体需要进行调整的示例:
errors = []
growing_rf = RandomForestClassifier(n_estimators=10, n_jobs=-1,
warm_start=True, random_state=1514)
for i in range(40):
growing_rf.fit(X_train, y_train)
growing_rf.n_estimators += 10
errors.append(log_loss(y_valid, growing_rf.predict_proba(X_valid)))
_ = plt.plot(errors, '-r')
这是我得到的:
我正在 Python 上做一些机器学习任务。我需要构建 RandomForest,然后构建一个图表来显示训练和测试样本的质量如何取决于随机森林中的树数。是否需要每次用一定数量的树建立一个新的随机森林?或者我可以以某种方式迭代添加树(如果可能的话,你能给出代码示例如何做到这一点)吗?
您可以使用 RandomForestClassifier
的 warm start
参数来做到这一点。
这是一个您可以根据自己的具体需要进行调整的示例:
errors = []
growing_rf = RandomForestClassifier(n_estimators=10, n_jobs=-1,
warm_start=True, random_state=1514)
for i in range(40):
growing_rf.fit(X_train, y_train)
growing_rf.n_estimators += 10
errors.append(log_loss(y_valid, growing_rf.predict_proba(X_valid)))
_ = plt.plot(errors, '-r')
这是我得到的: