BaggingRegressor 无限循环/不停止运行的问题

当我使用BaggingRegressor来控制过拟合时,我遇到了程序循环运行,而且完全占用CPU的问题,打开任务管理器一看,发现还真是个并行程序。无意间居然牵涉到了并行,这是我敲完那行代码之后没有想到的。

使用os.system("pause"),让程序暂停后发现python还是给了一些提示:

ImportError: [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

其实我本来是另外一个Python文件运行导入这个BaggingRegressor的python文件的,看到这个提示发现没戏了,因为"if __name__ == '__main__'"不允许其他地方导入。

解决的方案也就是如上所述,在前面加上"if __name__ == '__main__'"就可以了,下面举了个例子

if __name__ == 'main':

    '''your code here...'''
    
    llr = linear_model.LogisticRegression(C = 1.0, penalty = 'l1', tol = 1e-6)

    bagging_llr = BaggingRegressor(llr, n_estimators = 20, max_samples = 0.8, max_features = 1.0, bootstrap = True, bootstrap_features = False, n_jobs = -1)
    
    bagging_llr.fit(X, y)
    
    '''your code here...'''

猜你喜欢

转载自blog.csdn.net/yyhhlancelot/article/details/82354001