【问题标题】:Getting error while passing Class_weight parameter in Random Forest在随机森林中传递 Class_weight 参数时出错
【发布时间】:2018-05-27 17:50:08
【问题描述】:

我正在做二进制分类器。由于我的数据不平衡,我正在使用班级权重。我在传递值时遇到错误如何解决这个问题。

错误:ValueError: class_weight must be dict, 'balanced', or None, got: [{0: 0.4, 1: 0.6}]"

代码

rf=RandomForestClassifier(n_estimators=1000,oob_score=True,min_samples_leaf=500,class_weight=[{0:.4, 1:.6}])
    fit_rf=rf.fit(X_train_res,y_train_res)

错误

\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\class_weight.py in compute_class_weight(class_weight, classes, y)
     60         if not isinstance(class_weight, dict):
     61             raise ValueError("class_weight must be dict, 'balanced', or None,"
---> 62                              " got: %r" % class_weight)
     63         for c in class_weight:
     64             i = np.searchsorted(classes, c)

ValueError: class_weight must be dict, 'balanced', or None, got: [{0: 0.4, 1: 0.6}]

如何解决这个问题。

【问题讨论】:

  • 因为你只有一个类权重的字典,试着去掉括号,只传递字典。 class_weight={0:0.4, 1:0.6}.
  • @ScottBoston,谢谢它的工作,你能简要解释一下类权重是如何工作的。我的目标基本上是减少 1 类被错误分类为 0 类。我的数据高度不平衡,80% 来自 0 类,20% 来自 1 类

标签: pandas scikit-learn random-forest


【解决方案1】:

根据documentation

class_weight : 字典,字典列表,“平衡”,

因此,class_weight 参数接受字典、字典列表或字符串“balanced”。您收到的错误消息表明它需要一本字典,并且由于您只有一本字典,因此不需要列表。

那么,让我们试试吧:

rf=RandomForestClassifier(n_estimators=1000,
                          oob_score=True,
                          min_samples_leaf=500,
                          class_weight={0:.4, 1:.6})

fit_rf=rf.fit(X_train_res,y_train_res)

【讨论】:

    猜你喜欢
    • 2015-08-28
    • 2018-04-10
    • 2018-05-20
    • 2020-02-05
    • 2017-09-10
    • 2021-02-01
    • 2018-09-08
    • 2013-06-18
    • 2018-03-11
    相关资源
    最近更新 更多