使用python绘制混淆矩阵
# 可视化分类器性能
# load libraries
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import pandas as pd
# load data 加载数据
iris = datasets.load_iris()
# create feature matrix
features = iris.data
# create target vector
target = iris.target
# create list of target class names
class_names = iris.target_names
# split into training and test sets
# 创建 训练集 测试集
features_train, features_test, target_train, target_test = train_test_split(features, target, random_state=1)
# create logistic regression 逻辑回归
classifier = LogisticRegression()
# train model and make predictions 做出预测
target_predicted = classifier.fit(features_train, target_train).predict(features_test)
# create confusion matrix 创建混淆矩阵
matrix = confusion_matrix(target_test, target_predicted)
# create pandas dataframe 创建数据集
dataframe = pd.DataFrame(matrix, index=class_names, columns=class_names)
# create heatmap 绘制热力图
sns.heatmap(dataframe, annot=True, cbar=None, cmap="Blues")
plt.title("Confusion Matrix"), plt.tight_layout()
plt.ylabel("True Class"), plt.xlabel("Predicted Class")
plt.show()
来源:CSDN
作者:御剑归一
链接:https://blog.csdn.net/wj1298250240/article/details/103748272