首页 > 解决方案 > 绘制混淆矩阵多类和多标签指标目标错误

问题描述

我试图将混淆矩阵绘制到模型但得到以下错误:

Classification metrics can't handle a mix of multiclass and multilabel-indicator targets

首先我拆分数据(训练/测试):

# Shuffing Dataset / frac=1 is to shuffle 100% of dataset, if for ex 0.5 then 50%
df_shuffled = df_shuffled.sample(frac=1)
# Splitting in to X,y
X = df_shuffled.drop(['Target'], axis=1)
y = np.array(df_shuffled['Target'].values.tolist())
#Splitting in to train and test.  The test split is 20% and the training split is 80%. 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)

然后我缩放数据

# Scaling the Data Using StandardScaler
scaler = preprocessing.StandardScaler()
x_scaled = scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
X_test

然后我运行模型:

# Random Forest
rf = RandomForestClassifier(random_state=1)
model = MultiOutputClassifier(rf, n_jobs=-1)
model.fit(X_train, y_train)
m = model.fit(X_train, y_train)



y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy for Random Forest: ",accuracy)
"Done"

水库=Accuracy for Random Forest: 0.9390134529147982

然后我想绘制混淆矩阵:

from sklearn.metrics import plot_confusion_matrix
plot_confusion_matrix(estimator=m, X = X_test, y_true= y_test)

但是当我这样做时,我收到以下错误:

Classification metrics can't handle a mix of multiclass and multilabel-indicator targets

argmax我已经看到通过调用“test_labels”和“predictions”来指示解码的各种资源。但我似乎无法将其应用于我的场景。

我尝试了一切,但不断收到相同的错误。

有人可以帮忙吗?

标签: pythonplotmodelconfusion-matrix

解决方案


推荐阅读