gpt4 book ai didi

python - 值错误: Can't handle mix of multilabel-indicator and continuous-multioutput accuracy_score()

转载 作者:行者123 更新时间:2023-12-01 03:23:13 29 4
gpt4 key购买 nike

我正在尝试确定 KNN 和随机森林之间的预测模型的准确度分数,但 precision_score 方法给出了主题行中给出的错误。我的代码如下:

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(x_train,y_train)
knn.predict(x_test)
#Accuracy of prediction
y_pred = knn.predict(x_test)
#predictions = [round(value) for value in y_pred]
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: %.2f%%" % (accuracy * 100.0))

上述精度打印语句的输出为:

Accuracy: 80.04%

现在是第二种方法:

from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor(n_estimators=100, min_samples_leaf=10,
random_state=1)
model.fit(x_train, y_train)
print(model.score)
#Accuracy of prediction
y_pred = model.predict(x_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: %.2f%%" % (accuracy * 100.0))

在随机森林的情况下,accuracy_score 函数给出以下错误

accuracy = accuracy_score(y_test, y_pred)
Traceback (most recent call last):

File "<ipython-input-263-232dbb7449ea>", line 1, in <module>
accuracy = accuracy_score(y_test, y_pred)

File "C:\Anaconda3\lib\site-packages\sklearn\metrics\classification.py",
line 172, in accuracy_score
y_type, y_true, y_pred = _check_targets(y_true, y_pred)

File "C:\Anaconda3\lib\site-packages\sklearn\metrics\classification.py",
line 82, in _check_targets
"".format(type_true, type_pred))
ValueError: Can't handle mix of multilabel-indicator and continuous-
multioutput

为什么在第二个分类器 RandomForest 的情况下,accuracy_score 会出现此值错误?ValueError:无法处理多标签指示器和连续多输出 precision_score() 的混合

非常感谢任何建议!

最佳答案

回归适用于连续目标变量。分类是针对分类目标变量。您需要使用 RandomForestClassifier 来解决分类问题。

关于python - 值错误: Can't handle mix of multilabel-indicator and continuous-multioutput accuracy_score(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41724680/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com