分类任务绘制混淆矩阵

该代码片段导入了numpy和matplotlib库,用以读取CSV文件中的混淆矩阵数据并生成蓝调热力图。它设置了字体大小和类型,定义了类标签,然后创建了一个14x12大小的图像,并以lower为原点显示了热力图。此外,还添加了颜色条,轴标签,标题以及每个单元格的值,并反转了y轴。
摘要由CSDN通过智能技术生成
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

# Set font size and family
mpl.rcParams['font.size'] =12
mpl.rcParams['font.family'] = 'Times New Roman'

# Load confusion matrix from CSV file
confusion_matrix = np.loadtxt('../confusion_matrix.csv', delimiter=',')

# Define class labels
labels = ['Airport', 'BareLand', 'BaseballField', 'Beach', 'Bridge', 'Center', 'Church', 'Commercial', 'DenseResidential', 'Desert', 'Farmland', 'Forest', 'Industrial', 'Meadow', 'MediumResidential', 'Mountain', 'Park', 'Parking', 'Playground', 'Pond', 'Port', 'RailwayStation', 'Resort', 'River', 'School', 'SparseResidential', 'Square', 'Stadium', 'StorageTanks', 'Viaduct']

# Set figure size
fig, ax = plt.subplots(figsize=(14,12))

# Plot heatmap with origin set to 'lower'
im = ax.imshow(confusion_matrix, cmap='Blues', extent=[0, len(labels), 0, len(labels)], origin='lower')

# Set ticks at the middle of each cell
ticks = np.arange(len(labels))
plt.xticks(ticks+0.5, labels, rotation=90, fontsize=12)
plt.yticks(ticks+0.5, labels, fontsize=12)
# Hide tick marks
plt.tick_params(axis='both', which='both', length=0)

# Add color bar
cbar = ax.figure.colorbar(im, ax=ax, shrink=0.8)




# Set axis labels and title
plt.xlabel('Predicted Labels', fontsize=16)
plt.ylabel('True Labels', fontsize=16)
plt.title('Confusion Matrix',fontsize=17)



# Add text labels to each cell
for i in range(len(labels)):
    for j in range(len(labels)):
        if confusion_matrix[i, j] != 0:
            text = ax.text(j + 0.5, i + 0.5, '{:.1f}'.format(confusion_matrix[i, j]), ha='center', va='center', color='black', fontsize=10)

# Invert y-axis
ax.invert_yaxis()

# Show plot
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python与遥感

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值