Python 未知标签类型:';连续';而随机森林分类器在多类分类问题上的应用

Python 未知标签类型:';连续';而随机森林分类器在多类分类问题上的应用,python,machine-learning,scikit-learn,random-forest,multiclass-classification,Python,Machine Learning,Scikit Learn,Random Forest,Multiclass Classification,我的代码: 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> i

我的代码:

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'
ValueError回溯(最近一次调用)
在里面
1个rf_分类器=RandomForestClassifier(n_估计器=600,最小样本数=25)
---->2个rf_分类器。配合(组合_x_序列、y_序列)
3.
~\AppData\Roaming\Python\Python39\site packages\sklearn\employee\\u forest.py合适(self,X,y,sample\u weight)
329 self.n_输出u=y.shape[1]
330
-->331 y,扩展类重量=自身。验证类重量(y)
332
333如果getattr(y,“数据类型”,无)!=双重或非y.flags.Continental:
~\AppData\Roaming\Python\Python39\site packages\sklearn\employee\u forest.py in\u validate\u y\u class\u weight(self,y)
557
558 def_验证_等级_重量(自身,y):
-->559检查分类目标(y)
560
561 y=np.拷贝(y)
~\AppData\Roaming\Python\Python39\site packages\sklearn\utils\multiclass.py in check\u classification\u targets(y)
181如果y_类型不在['binary'、'multiclass'、'multiclass multioutput'中,
182“多标签指示器”、“多标签序列”]:
-->183提升值错误(“未知标签类型:%r”%y\u类型)
184
185
ValueError:未知的标签类型:“连续”
y_列是一个数值介于0和5之间的NumPy数组,用于多类分类,每个类对应一个整数

y_列车的类型为int32


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

当y_序列不是输入到任何分类器模型中的类型时,可能会出现此问题。在这种情况下,y_列车属于系列类型。当我将类型更改为NumPy数组时,它工作得很好。 代码如下:

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

你能确认你的目标是你真正想要的吗?未知标签类型:装配时y_列出现问题时会发生错误。检查标签中是否有浮点数。