python - Scikit 学习多核 : "AttributeError: StdIn instance has no attribute ' close'"

标签 python scikit-learn pycharm

我正在使用 PyCharm 从 Scikit-learn 运行 RandomForestRegressor。这在执行整个脚本时工作正常:

clf = ensemble.RandomForestClassifier(n_estimators=100, n_jobs = 4, verbose=1, oob_score=True)
clf = clf.fit(x_train, y_train)

当我尝试从控制台运行该行时,我反复收到此错误:

AttributeError: StdIn instance has no attribute 'close'
  File "/usr/local/Cellar/python/2.7.6_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 249, in _bootstrap
    sys.stdin.close()

如果我将 n_jobs 参数更改为 n_jobs=1 我不会收到错误,但它无法利用多线程。

我假设这与 PyCharm 控制台正在重定向标准输入这一事实有关,但我不确定如何让它工作。任何想法表示赞赏 - 谢谢!

最佳答案

来晚了一点,但我自己刚才也遇到了同样的问题。

另一个答案中建议的解决方法是避免使用 PyCharm 的交互式解释器。这当然可行,但并不令我满意,因为我非常喜欢使用交互式解释器。

我找到了解决问题的不同解决方法并且允许使用交互式解释器。在通过multiprocessing.Process()每次创建新进程之前,必须应用以下代码片段:

if not hasattr(sys.stdin, 'close'):
    def dummy_close():
        pass
    sys.stdin.close = dummy_close

现在,multiprocessing 可以在 sys.stdin 上调用 close() 就好了,一切都顺利进行。

请注意,在启动阶段仅附加一次虚拟 close() 是不够的。我发现有必要在每次调用 multiprocessing.Process() 之前执行此操作;不过,我没有尝试使用进程池。

关于python - Scikit 学习多核 : "AttributeError: StdIn instance has no attribute ' close'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24171725/

相关文章:

python - 从 CSV 中删除新行\n 读取

python - 无法在 Django 1.9 中发送电子邮件

python - 为什么在Pillow-python中调整图像大小会删除Image.format?

python - 在不使用 "internal"API 的情况下获取 scikit-learn PCA 上的组件

python-3.x - 无效的 base64 编码字符串 : number of data characters (217) cannot be 1 more than a multiple of 4

python - 通过 QT 设计者创建的 Qtoolbar 上的单击操作来调用函数

python - 对具有相同类别的多列进行标签编码

intellij-idea - 如何增加 IntelliJ JetBrains PyCharm、PhpStorm、RubyMine 中的菜单和选项卡字体大小

python - PyCharm docker 调试错误

python - RuntimeWarning : numpy. dtype 大小已更改,可能表示二进制不兼容