您的位置:首页 > 编程语言 > Python开发

python中实现交叉验证时出现

2018-03-30 15:31 513 查看
#查看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)))


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: