【发布时间】: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