我正在尝试确定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))
对于“随机森林”,precision_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的情况下precision_score给出此Value错误?
ValueError:无法处理多标签指示符和连续多输出的precision_score()的混合
任何建议,不胜感激!
最佳答案
回归适用于连续目标变量。分类是针对分类目标变量的。您需要使用RandomForestClassifier来解决分类问题。
关于python - ValueError:无法处理多标签指示符和连续多输出的precision_score()的混合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41724680/