为实现scikit学习估计器接口的任何估计器处理嵌套交叉验证的通用包。
nested-cv的Python项目详细描述
嵌套交叉验证
这个存储库实现了一个通用的嵌套交叉验证函数。可与任何实现scikit学习估计器接口的估计器一起使用。
安装包装:
您可以在pypi*上找到该包,并使用以下命令通过pip安装它:
pip install nested-cv
也可以从Releases页上的控制盘文件安装它。
*我们会逐步推送更新,如果您需要绝对最新的更改,请从github中提取此主控文件。
用法
注意NestedCv的可用选项。一些交叉验证选项在字典cv_options
中定义。
此软件包针对实现scikit学习包装的任何估计器进行了优化,例如xgboost、lightgbm、kerasregressor、kerasclassifier等。
单一算法
下面是一个使用随机林的示例
#import the package fromnested_cvimportNestedCV# Define a parameters gridparam_grid={'max_depth':[3,None],'n_estimators':[100,200,300,400,500,600,700,800,900,1000],'max_features':[50,100,150,200]# Note: You might not have that many features}# Define parameters for function# Default scoring: RMSEnested_CV_search=NestedCV(model=RandomForestRegressor(),params_grid=param_grid,outer_kfolds=5,inner_kfolds=5,cv_options={'sqrt_of_score':True,'randomized_search_iter':30})nested_CV_search.fit(X=X,y=y)grid_nested_cv.score_vs_variance_plot()print('\nCumulated best parameter grid was:\n{0}'.format(nested_CV_search.best_params))
多种算法
下面是一个使用Random Forest、XGBoost和LightGBM的示例
models_to_run=[RandomForestRegressor(),xgb.XGBRegressor(),lgb.LGBMRegressor()]models_param_grid=[{# 1st param grid, corresponding to RandomForestRegressor'max_depth':[3,None],'n_estimators':[100,200,300,400,500,600,700,800,900,1000],'max_features':[50,100,150,200]},{# 2nd param grid, corresponding to XGBRegressor'learning_rate':[0.05],'colsample_bytree':np.linspace(0.3,0.5),'n_estimators':[100,200,300,400,500,600,700,800,900,1000],'reg_alpha':(1,1.2),'reg_lambda':(1,1.2,1.4)},{# 3rd param grid, corresponding to LGBMRegressor'learning_rate':[0.05],'n_estimators':[100,200,300,400,500,600,700,800,900,1000],'reg_alpha':(1,1.2),'reg_lambda':(1,1.2,1.4)}]fori,modelinenumerate(models_to_run):nested_CV_search=NestedCV(model=model,params_grid=models_param_grid[i],outer_kfolds=5,inner_kfolds=5,cv_options={'sqrt_of_score':True,'randomized_search_iter':30})nested_CV_search.fit(X=X,y=y)model_param_grid=nested_CV_search.best_paramsprint('\nCumulated best parameter grid was:\n{0}'.format(model_param_grid))
嵌套Cv参数
Name | type | description |
---|---|---|
model | estimator | The estimator implements scikit-learn estimator interface. |
params_grid | dictionary "dict" | The dict contains hyperparameters for model. |
outer_kfolds | int | Number of outer K-partitions in KFold |
inner_kfolds | int | Number of inner K-partitions in KFold |
cv_options | dictionary "dict" | Next section |
n_jobs | int | Number of jobs for joblib to run (multiprocessing) |
cv_options
值选项
metric
:可从sklearn.metrics调用,默认值=平均值平方误差
用于对每个模型进行评分的评分标准
metric_score_indicator_lower
:布尔型,默认值为true
选择较低的分数是更好的度量计算还是较高的分数是更好的
sqrt_of_score
:布尔型,默认值为false
_________
randomized_search
:布尔型,默认值为true
__
randomized_search_iter
:int,默认值=10
随机搜索的迭代次数
recursive_feature_elimination
:布尔型,默认值为False
是否进行特征消除
predict_proba
:布尔型,默认值为false
_
multiclass_average
:字符串,默认值为“二进制”
_______对于具有多类预测的某些分类度量,需要指定 “二进制”以外的平均值
返回
variance
:按numpy.var()计算的模型方差
outer_scores
:来自外部交叉验证的外部分数列表
best_inner_score_list
:每个外循环的最佳内部分数列表
best_params
:每个内环的所有最佳参数累积在一个dict中
best_inner_params_list
:作为字典数组的每个外部循环的最佳内部参数
如何使用输出?
我们建议看看最好的超参数和每个外环的得分。看看模型在嵌套交叉验证设置中的稳定性如果外部分数变化很大,则可能表示模型不稳定。在这种情况下,从制作新模型开始。
嵌套交叉验证之后?
如果嵌套交叉验证的结果稳定:使用与嵌套交叉验证中相同的过程运行普通交叉验证,即如果在嵌套交叉验证中使用了特征选择,则也应在普通交叉验证中执行该操作。
限制
- XGBoost实现了一个不能在此实现中使用的
early_stopping_rounds
。其他类似的参数可能无法与此实现结合使用必须采用该函数才能使用类似的特殊参数。
我们学到了什么?
- 使用Scikit-Learn将导致更快的实现,因为scikit学习社区已经实现了许多功能,它们完成了很多工作。
- 我们已经在我们的主要项目中学习并应用了这个包。
为什么使用嵌套交叉验证?
控制偏差-方差权衡是机器学习中的一项重要任务,由[Cawley and Talbot, 2010]表示许多文章指出,这是可能的,使用嵌套交叉验证,其中一个由Varma and Simon, 2006嵌套交叉验证的其他有趣文献是[Varoquaox et al., 2017]和[Krstajic et al., 2014]