python - 绘制混淆矩阵多类和多标签指标目标错误
问题描述
我试图将混淆矩阵绘制到模型但得到以下错误:
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”来指示解码的各种资源。但我似乎无法将其应用于我的场景。
我尝试了一切,但不断收到相同的错误。
有人可以帮忙吗?
解决方案
推荐阅读
- python - 可以使用空的 MultiIndex 对 DataFrame 进行切片吗?
- r - 在 R 包的描述文件中使用连字符
- reactjs - 如何升级到 babel 7
- powershell - 尝试使用 Invoke-SQLCmd 和 -ErrorAction SilentlyContinue 进行 Catch
- css - ASP.NET Core razor pages 应用程序中左侧带有关闭和打开按钮的导航菜单
- angular - 动态角度组件
- curl - 指定 --with-ssl 时,cURL 会寻找什么样的文件?
- spring - 关于跨站脚本伪造
- spring-mvc - Tomcat8 Spring MVC4 maven jsp源码显示而不是内容
- reactjs - 使用阿波罗客户端创建反应应用程序没有得到任何响应