机器学习:性能度量——Precision,Recall,F1_score(Python)

import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report, ConfusionMatrixDisplay, confusion_matrix


digits = datasets.load_digits()
X, y = digits.data, digits.target
X = StandardScaler().fit_transform(X)  # 标准化
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0,
                                                    shuffle=True, stratify=y)

# 采用逻辑回归进行多分类
model_lg = LogisticRegression()  # 逻辑回归模型实例
model_lg.fit(X_train, y_train)  # 训练样本
y_test_pred = model_lg.predict(X_test)  # 测试样本预测

# 单独计算性能指标
test_accuracy = metrics.accuracy_score(y_test, y_test_pred)
macro = metrics.precision_score(y_test, y_test_pred, average='macro')
micro = metrics.recall_score(y_test, y_test_pred, average='micro')
f1_micro = metrics.f1_score(y_test, y_test_pred, average='micro')
f1_weighted = metrics.f1_score(y_test, y_test_pred, average='weighted')
fbeta = metrics.fbeta_score(y_test, y_test_pred, average='macro', beta=1)
print("各性能指标计算:")
print("test_accuracy = %f, macro = %f, micro = %f, \n f1_micro = %f, f1_weighted = %f, fbeta = %f"
      % (test_accuracy, macro, micro, f1_micro, f1_weighted, fbeta))

# 绘制混淆矩阵
fig, ax = plt.subplots(figsize=(10, 8))
target_names = []  # 用来命名类别
for i in range(10):
    target_names.append("n" + str(i))

ConfusionMatrixDisplay.from_estimator(model_lg, X_test, y_test, display_labels=target_names,
                              cmap=plt.cm.Reds, ax = ax)
plt.show()

# 解决报错:from sklearn.metrics import plot_confusion_matrix
# 方法一:
# 降低sklearn版本为1.2以下。
# 方法二:
# 将
# from sklearn.metrics import plot_confusion_matrix
# 修改为:
# from sklearn.metrics import ConfusionMatrixDisplay
# 然后将对应代码:
# plot_confusion_matrix
# 全部修改为:
# ConfusionMatrixDisplay.from_estimator
# 或者
# ConfusionMatrixDisplay.from_predictions

print("-" * 60)
print("混淆矩阵:")
print(confusion_matrix(y_test, y_test_pred))  # 输出混淆矩阵

print("-" * 60)
print(classification_report(y_test, y_test_pred, target_names=target_names))

print("-" * 60)
prfs = metrics.precision_recall_fscore_support(y_test, y_test_pred, beta=1, average=None)
pd.DataFrame(prfs, index=["precision", "recall", "fscore", "support"])


# 自写代码
import numpy as np


cm = confusion_matrix(y_test, y_test_pred)  # 构造混淆矩阵
precision = np.diag(cm) / np.sum(cm, axis=0)  # 查准率
recall = np.diag(cm) / np.sum(cm, axis=1)  # 召回率
f1_score = 2 * precision * recall / (precision + recall)  # F1
support = np.sum(cm, axis=1)  # 各类别支持样本量
support_all = np.sum(support)  # 总样本量
accuracy = np.sum(np.diag(cm)) / np.sum(cm)  # 精度
# 宏查准率,宏召回率,宏-F1
macro_avg = [precision.mean(), recall.mean(),
             2 * precision.mean() * recall.mean() / (precision.mean() + recall.mean())]
# 加权查准率,加权召回率,加权-F1
weight = support / support_all  # 各类别权重系数
weighted_avg = [np.sum(weight * precision), np.sum(weight * recall),
                np.sum(weight * f1_score)]
# np.sum(1 + weight ** 2) * precision * recall / (weight ** 2 * precision + recall)
metrics1 = pd.DataFrame(np.array([precision, recall, f1_score, support]).T,
                        columns=["precision", "recall", "f1_score", "support"])
metrics2 = pd.DataFrame([[' ', ' ', ' ', ' '], [' ', ' ', accuracy, support_all],
                         np.hstack([macro_avg, support_all]),
                         np.hstack([weighted_avg, support_all])],
                        columns=["precision", "recall", "f1_score", "support"])
metrics = pd.concat([metrics1, metrics2], ignore_index=True)
target_names = []  # 用来命名类别
for i in range(10):
    target_names.append("n" + str(i))
target_names.extend(["", "accuracy", "macro_avg", "weighted-avg"])
metrics.index = target_names
print(metrics)


机器学习:性能度量——Precision,Recall,F1_score(Python)_第1张图片

 

机器学习:性能度量——Precision,Recall,F1_score(Python)_第2张图片

 

 

机器学习:性能度量——Precision,Recall,F1_score(Python)_第3张图片

 

 

 

你可能感兴趣的:(机器学习,python,人工智能)