【问题标题】:Error in running Random Forest Classifier运行随机森林分类器时出错
【发布时间】:2020-02-02 10:02:30
【问题描述】:

我正在尝试在 python 中实现随机森林分类器,但它显示了一个值错误。示例代码为:

from sklearn.ensemble import RandomForestClassifier
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

df = pd.read_csv("0.5-1.csv")
df.head()

X = df[['wavelength', 'phase velocity']]
y = df['shear wave velocity']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

print (len(X_train),len(X_test),len(y_train),len(y_test))

rf = RandomForestClassifier(n_estimators=40)
rf.fit(X_train, y_train)
print (rf.score(X_test, y_test))

错误是:

Traceback (most recent call last):
  File "G:\My Drive\ANN\test\0.5-1\0.5-1_tunecode.py", line 23, in <module>
    rf.fit(X_train, y_train)
  File "C:\Users\sadia\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\ensemble\forest.py", line 275, in fit
    y, expanded_class_weight = self._validate_y_class_weight(y)
  File "C:\Users\sadia\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\ensemble\forest.py", line 478, in _validate_y_class_weight
    check_classification_targets(y)
  File "C:\Users\sadia\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\multiclass.py", line 169, in check_classification_targets
    raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'continuous'

失败发生在:

rf.fit(X_train, y_train)

任何帮助将不胜感激。

【问题讨论】:

  • 请分享示例数据文件。
  • 请不要在谷歌电子表格上共享整个 csv 数据。而只是发布带有代码的示例数据。
  • 我只是添加了我的示例数据的图片
  • 理想情况下,您不应该在图像中发布数据。复制该问题会很困难。看看这个:meta.stackoverflow.com/questions/285551/…
  • 好的。抱歉,我对发布数据感到困惑。

标签: python scikit-learn random-forest


【解决方案1】:

发生此错误是因为您将浮点值传递给分类器,该分类器期望分类值作为目标向量。尝试使用回归器算法。 即,您应该使用 RandomForestRegressor 而不是 RandomForestClassifier 作为连续目标向量。

希望这会有所帮助!

【讨论】:

  • 谢谢。有用!但是如果我想实现其他分类器,例如 MLP 分类器、逻辑回归或 SVM,我应该如何安排数据?
  • 对于分类器,将目标向量转换为分类形式。看看这个:scikit-learn.org/stable/modules/generated/…。除此之外,它完全取决于数据和其他测量参数来找出最适合的模型。
猜你喜欢
  • 2020-12-08
  • 2018-02-18
  • 2022-01-25
  • 2013-12-12
  • 2018-05-20
  • 2021-07-01
  • 2018-03-05
  • 1970-01-01
  • 2019-09-05
相关资源
最近更新 更多