python中实现交叉验证时出现

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 01:33   18   0

#查看cv评分

rf_test = RandomForestRegressor(max_depth=30, n_estimators=500, max_features = 100, oob_score=True, random_state=1234)
cv_score = cross_val_score(rf_test, train_d.drop('SalePrice', axis = 1), train_d['SalePrice'], cv = 5, n_jobs = -1)
print('CV Score is: '+ str(np.mean(cv_score)))


如上方,使用python进行交叉验证时,报错显示:

[joblib] Attempting to do parallel computing without protecting your import on a system that does not support forking. To use parallel-computing in a script, you must protect your main loop using "if __name__ == '__main__'". Please see the joblib documentation on Parallel for more information


查看stackoverflow 有人碰到同样的问题,总的来说是因为Windows没有fork()。由于这个限制,Windows需要在它生成的所有子进程中重新导入您的主模块,以便在子进程中重新创建父进程。这意味着,如果您有在模块级生成新进程的代码,那么它将在所有子进程中递归地执行。if名称==“main”用于防止模块范围内的代码在子进程中被重新执行。这在Linux上是不必要的,因为它有fork(),它允许它派生一个维护相同状态的子进程,而不需要重新导入主模块。

解决办法

在代码中进行以下更改

#查看cv评分
rf_test = RandomForestRegressor(max_depth=30, n_estimators=500, max_features = 100, oob_score=True, random_state=1234)

if __name__=='__main__':#加入此行 此后的cv函数在main里面运行
    cv_score = cross_val_score(rf_test, train_d.drop('SalePrice', axis = 1), train_d['SalePrice'], cv = 5, n_jobs = -1)
    print('CV Score is: '+ str(np.mean(cv_score)))






分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP