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)))
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)))
相关文章推荐
- 机器学习-CrossValidation交叉验证Python实现
- 什么是留一交叉验证以及如何用python编码实现
- 机器学习-CrossValidation交叉验证Python实现
- 机器学习:交叉验证和模型选择与Python代码实现
- python交叉验证train_test_split(old:cross validation)实现
- 机器学习-CrossValidation交叉验证Python实现
- python中sklearn实现交叉验证
- python中sklearn实现交叉验证
- 机器学习(6)-交叉验证代码实现原理和图像均值化预处理
- 1_python小程序之实现用户的注册登陆验证功能
- python实现字符串只出现一次的字符
- python+pyqt实现12306图片验证效果
- python 实现非验证发送邮件
- 数据标准化+网格搜索+交叉验证+预测(Python)
- Python实现随机生成手机号及正则验证手机号的方法
- CrossValidation十字交叉验证的Python实现
- 【人脸识别】人脸验证算法Joint Bayesian详解及实现(Python版)
- python实现12306验证和登录
- Python通过Django实现用户注册和邮箱验证功能代码
- Python登录注册验证功能实现