使用随机森林分类器进行多类分类时出现未知标签类型:’continuous’

我的代码:

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")

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注