【问题标题】:Class label not present sklearn.ensemble.RandomForestClassifier for class_weight类标签不存在 sklearn.ensemble.RandomForestClassifier for class_weight
【发布时间】:2019-11-08 19:17:39
【问题描述】:

我正在使用来自sklearn.ensembleRandomForestClassifier。当我在没有class_weight 的情况下使用它时它可以工作,但是当我添加class_weight 时它会给出这个错误。

lr = RandomForestClassifier(n_estimators = 22,criterion =
                           'entropy',max_depth=5,class_weight={'Sex':2.})

lr.fit(X_train.values[:,1:],Y_train)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-248-411a1c135d08> in <module>
      1 print(X_train)
----> 2 lr.fit(X_train.values[:,1:],Y_train)

/opt/conda/lib/python3.6/site-packages/sklearn/ensemble/forest.py in fit(self, X, y, sample_weight)
    273         self.n_outputs_ = y.shape[1]
    274 
--> 275         y, expanded_class_weight = self._validate_y_class_weight(y)
    276 
    277         if getattr(y, "dtype", None) != DOUBLE or not y.flags.contiguous:

/opt/conda/lib/python3.6/site-packages/sklearn/ensemble/forest.py in _validate_y_class_weight(self, y)
    519                     class_weight = self.class_weight
    520                 expanded_class_weight = compute_sample_weight(class_weight,
--> 521                                                               y_original)
    522 
    523         return y, expanded_class_weight

/opt/conda/lib/python3.6/site-packages/sklearn/utils/class_weight.py in compute_sample_weight(class_weight, y, indices)
    161             weight_k = compute_class_weight(class_weight_k,
    162                                             classes_full,
--> 163                                             y_full)
    164 
    165         weight_k = weight_k[np.searchsorted(classes_full, y_full)]

/opt/conda/lib/python3.6/site-packages/sklearn/utils/class_weight.py in compute_class_weight(class_weight, classes, y)
     63             i = np.searchsorted(classes, c)
     64             if i >= len(classes) or classes[i] != c:
---> 65                 raise ValueError("Class label {} not present.".format(c))
     66             else:
     67                 weight[i] = class_weight[c]

ValueError: Class label Sex not present. 

这是我的 X_train:

PassengerId Pclass Sex ... Ticket Fare Embarked

【问题讨论】:

    标签: python-3.x scikit-learn random-forest


    【解决方案1】:

    Y_train你有多少班?

    class_weightY_train 相关,即标签。

    示例:

    class_weight={0:1,1:2}

    表示权重 1 到 0 类,权重 2 到 1 类。

    使用class_weight={'Sex':2.}是错误的,它指的是X_train

    【讨论】:

    • @RavikantSingh 如果您的Y_train 中没有至少两个类,则您无法对任何内容进行分类
    • 这是真的。您不能只用一个类来定义分类问题。这根本没有意义。也许您应该寻找回归(标量值预测)模型。
    • @RavikantSingh “它有效”是什么意思?由于没有什么可分类的,模型到底在做什么?
    • 可能OP意味着没有错误弹出。但是,当n_classes &lt; 2 时,分类仍然没有意义
    • @RavikantSingh 告诉我,你的目标是什么?
    猜你喜欢
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多