随机森林的特定交叉验证

Bro*_*ook 9 scikit-learn

使用随机森林与scikit学习.RF过度拟合数据并且预测结果很糟糕.

过度拟合不依赖于RF的参数:NBtree,Depth_Tree

过度匹配发生在许多不同的参数上(通过grid_search测试).

补救措施:我调整初始数据/下采样一些结果,以影响拟合(手动预处理噪声样本).

Loop on random generation of RF fits, 

Get RF prediction on the  data for prediction
Select the model which best fits the "predicted data" (not the calibration data).
Run Code Online (Sandbox Code Playgroud)

这个蒙特卡洛斯非常消耗,只是想知道是否有其他方法可以对随机森林进行交叉验证?(即不是超参数优化).

EDITED

小智 32

使用scikit-learn中的任何分类器进行交叉验证非常简单:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
import numpy as np

clf = RandomForestClassifier() #Initialize with whatever parameters you want to

# 10-Fold Cross validation
print np.mean(cross_val_score(clf, X_train, y_train, cv=10))
Run Code Online (Sandbox Code Playgroud)

如果您希望运行网格搜索,可以通过GridSearchCV类轻松完成.为了做到这一点,你必须提供一个param_grid,根据文件是

具有参数名称(字符串)作为键的字典和作为值的参数设置列表,或者这些字典的列表,在这种情况下,探索列表中每个字典所跨越的网格.这样可以搜索任何参数设置序列.

也许,你可以定义param_grid如下:

param_grid = {
                 'n_estimators': [5, 10, 15, 20],
                 'max_depth': [2, 5, 7, 9]
             }
Run Code Online (Sandbox Code Playgroud)

然后您可以使用GridSearchCV类,如下所示

from sklearn.model_selection import GridSearchCV

grid_clf = GridSearchCV(clf, param_grid, cv=10)
grid_clf.fit(X_train, y_train)
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用最佳模型grid_clf. best_estimator_和最佳参数grid_clf. best_params_.同样,您可以使用获得网格分数grid_clf.grid_scores_

希望这可以帮助!

  • 它应从sklearn.ensemble导入RandomForestClassifier或从sklearn.ensemble导入RandomForestRegressor (2认同)