未知标签类型:'continuous' 在多重 class class 化问题上使用随机森林 classifier

Unknown label type: 'continuous' while using random forest classifier on a multi class classification problem

我的代码:

rf_classifier = RandomForestClassifier(n_estimators=600, min_samples_split=25)
rf_classifier.fit(combined_x_train, y_train)

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-55-3f817939cbaa> in <module>
      1 rf_classifier = RandomForestClassifier(n_estimators=600, min_samples_split=25)
----> 2 rf_classifier.fit(combined_x_train, y_train)
      3 

~\AppData\Roaming\Python\Python39\site-packages\sklearn\ensemble\_forest.py in fit(self, X, y, sample_weight)
    329         self.n_outputs_ = y.shape[1]
    330 
--> 331         y, expanded_class_weight = self._validate_y_class_weight(y)
    332 
    333         if getattr(y, "dtype", None) != DOUBLE or not y.flags.contiguous:

~\AppData\Roaming\Python\Python39\site-packages\sklearn\ensemble\_forest.py in _validate_y_class_weight(self, y)
    557 
    558     def _validate_y_class_weight(self, y):
--> 559         check_classification_targets(y)
    560 
    561         y = np.copy(y)

~\AppData\Roaming\Python\Python39\site-packages\sklearn\utils\multiclass.py in check_classification_targets(y)
    181     if y_type not in ['binary', 'multiclass', 'multiclass-multioutput',
    182                       'multilabel-indicator', 'multilabel-sequences']:
--> 183         raise ValueError("Unknown label type: %r" % y_type)
    184 
    185 

ValueError: Unknown label type: 'continuous'

y_train 是一个 NumPy 数组,其值介于 0 和 5 之间,用于多重class class化,每个 class 对应一个整数。

y_train的类型是int32。

我不明白为什么会出现此错误。

当 y_train 不是输入到任何分类器模型中的类型时,可能会出现此问题。在本例中,y_train 属于 Series 类型。当我将类型更改为 NumPy 数组时,它工作正常。 这是代码:

y_train = y_train.to_numpy(dtype="int")
y_test = y_test.to_numpy(dtype="int")