【问题标题】:SciKit-Learn: JoblibException When Using Random ForestSciKit-Learn:使用随机森林时的 JoblibException
【发布时间】:2017-12-10 15:11:25
【问题描述】:

我正在尝试做一些机器学习。我正在尝试预测一篇文章的参与时间。我的X 数据集如下:

    _text_word_length   _title_char_length  _title_word_length  _text_char_length
0   1306                53                   7                  8056    
1   1075                62                   11                 6127

而我的目标 Y 值只是代表参与时间的浮点数。

我使用 SciKit-Learn 如下:

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score, KFold
import numpy as np
clf = RandomForestRegressor(n_jobs=-1, n_estimators=250, max_features = 0.8, verbose = 2)
score = cross_val_score(estimator = clf, X = X1, y = Y1, cv = KFold(n_splits = 5, random_state = 100), n_jobs = -1, \
                        scoring = "neg_mean_squared_error")
np.mean([np.sqrt(-x) for x in score])

因为我使用的是详细模式,所以它会输出随机森林的所有树。它几乎穿过了所有的树,然后我得到了这个:

JoblibException: JoblibException
___________________________________________________________________________
Multiprocessing exception:

然后有大量的文字(不会在这里复制,但可以根据要求)。最后,我看到了:

ValueError: I/O operation on closed file

我完全迷失了,因为昨天运行的代码非常相似,所以我不确定我做错了什么。

有什么想法吗?

谢谢!

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    您可以尝试以下方法吗?因此,将所有代码放在 if name =='main' 之后并带有适当的缩进。

    from sklearn.ensemble import RandomForestRegressor
    from sklearn.model_selection import cross_val_score, KFold
    import numpy as np
    
    if __name__ =='main':
    
        clf = RandomForestRegressor(n_estimators=250, max_features = 0.8, verbose = 2)
        score = cross_val_score(estimator = clf, X = X1, y = Y1, cv = KFold(n_splits = 5, random_state = 100), n_jobs = -1,scoring = "neg_mean_squared_error")
        np.mean([np.sqrt(-x) for x in score])
    

    请注意 1)在cross_val_score之前的评分=..应该有一个昏迷而不是切片(/)

    2) 在 cross_val_score 中只使用一次 n_jobs =-1

    【讨论】:

      猜你喜欢
      • 2015-03-28
      • 2015-12-01
      • 2013-04-26
      • 2014-11-12
      • 2015-09-16
      • 2015-04-16
      • 2016-06-22
      • 2012-03-10
      • 2017-03-26
      相关资源
      最近更新 更多