其他分享
首页 > 其他分享> > sklearn学习之:sklearn实现混淆矩阵

sklearn学习之:sklearn实现混淆矩阵

作者:互联网

文章目录

import pandas as pd
import numpy as np
import os
from imblearn.over_sampling import SMOTE
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.ensemble import *
os.chdir("../../数据/")
def preprocessing():
    .....
    .....
    return std_data,label

得到特征和标签

data, label = preprocessing()

训练自己的模型

x_train,x_test,y_train,y_test = train_test_split(data,label)
forest = RandomForestClassifier()
forest.fit(x_train,y_train)
score = forest.score(x_test,y_test)

score
0.7647058823529411

构造混淆矩阵

from sklearn.metrics import confusion_matrix
import seaborn as sns
y_true = y_test
y_pred = forest.predict(x_test)
cm = confusion_matrix(y_true,y_pred)
sns.heatmap(cm,cmap="YlGnBu_r",fmt="d",annot=True)

在这里插入图片描述

加上合适的标签

cm = pd.DataFrame(cm,columns=["cat","dog","lion"],index=["cat","dog","lion"])
sns.heatmap(cm,cmap="YlGnBu_r",fmt="d",annot=True)

在这里插入图片描述

混淆矩阵传递出的信息

标签:混淆,标签,矩阵,dog,lion,test,import,sklearn
来源: https://blog.csdn.net/qq_42902997/article/details/121688840