![](/img/trans.png)
[英]ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets error
[英]ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets sklearn
我使用随机森林分类器算法来预测我的样本属于不同的类别(5 个不同的类别)。 但是,在做出预测后,由于类别不同,我无法准确评估我的 model。 我在另一篇文章中看到有必要使用 np.argmax(y_pred, axis=1) 但我并不真正了解该元素的用途以及如何使用该元素,即使在我的情况下也不需要它。 你能帮我么?
import numpy as np
import pandas as pd
from sklearn import metrics
from keras.utils import to_categorical
import sklearn as sk
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
X = pd.read_csv('/Users/lottie/desktop/1.csv', header=None)
Y = pd.read_csv('/Users/lottie/desktop/2.csv', header=None)
X.drop([0,0], inplace=True)
Y.drop([0,0], inplace=True)
del X[0]
del Y[0]
Y_encoded = list()
for i in Y.loc[0:,1] :
if i == 'BRCA' : Y_encoded.append(0)
if i == 'KIRC' : Y_encoded.append(1)
if i == 'COAD' : Y_encoded.append(2)
if i == 'LUAD' : Y_encoded.append(3)
if i == 'PRAD' : Y_encoded.append(4)
Y_bis = to_categorical(Y_encoded)
X_train, X_test, y_train, y_test = train_test_split(X, Y_bis, test_size=0.30, random_state=42)
regressor = RandomForestRegressor(n_estimators=20, random_state=0)
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
print(confusion_matrix(y_test,y_pred))
print(classification_report(y_test,y_pred))
print(accuracy_score(y_test, y_pred))
您正在使用RandomForestRegressor
。 这个 model 是用于连续变量(比如房子的价格),如果你有类,你的 output 是不连续的。
如果您有课程,则必须使用RandomForestClassifier
。 显然,您必须将 output 编码为数字。 每个不同的 class 对应一个编号。 然后,当您预测时,您将获得 class 的编号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.