我的代码:
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之间,用于多类分类,每个类别对应一个整数值。
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")