学习测试代码
"""
# 模型对比实验
# 实现不同模型的准确率对比图绘制
"""
import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams['pdf.fonttype'] = 42
plt.rcParams['ps.fonttype'] = 42
plt.rc('font', family='Times New Roman', size=12)
# 1. 读取模型的训练过程数据文件
AlexNet = pd.read_csv('../result/AlexNet.csv')
VGG16 = pd.read_csv('../result/VGG16.csv')
GoogleNetActual = pd.read_csv('../result/GoogleNet.csv')
BiTCN_BiLSTM_Attention = pd.read_csv('../result/BiTCN-BiLSTM-Attention.csv')
# 2. 将数据转换为列表文件
epoch_list = list(range(1, len(BiTCN_BiLSTM_Attention) + 1)) # CSV文件有30行数据,因为进行了30轮次的训练
# CSV文件中的列名为 'accuracy' 和 'loss' 分别代表准确率和损失值
BiTCN_BiLSTM_Attention_accuracy = BiTCN_BiLSTM_Attention['accuracy'].tolist()
BiTCN_BiLSTM_Attention_loss = BiTCN_BiLSTM_Attention['loss'].tolist()
# 获取对比模型的准确率和损失值
AlexNet_accuracy = AlexNet['accuracy'].tolist()
AlexNet_loss = AlexNet['loss'].tolist()
VGG16_accuracy = VGG16['accuracy'].tolist()
VGG16_loss = VGG16['loss'].tolist()
GoogleNetActual_accuracy = GoogleNetActual['accuracy'].tolist()
GoogleNetActual_loss = GoogleNetActual['loss'].tolist()
# # 3. 绘制准确率对比图
# plt.figure(figsize=(8, 5))
# plt.xlabel("Epoch")
# plt.ylabel("Accuracy")
#
# # 下面开始添加准确率对比图中需要增加的曲线
# plt.plot(epoch_list, AlexNet_accuracy, linewidth='2', color='aqua', label='AlexNet')
# plt.plot(epoch_list, VGG16_accuracy, linewidth='2', color='#CC0066', label='VGG16')
# plt.plot(epoch_list, GoogleNetActual_accuracy, linewidth='2', color='green', label='GoogleNet')
# plt.plot(epoch_list, BiTCN_BiLSTM_Attention_accuracy, linewidth='2', color='blue', label='BiTCN-BiLSTM-Attention')
#
# plt.rcParams.update({'font.size': 12})
# plt.title('Model Accuracy Comparison')
# plt.legend()
# plt.show()
#
# # 4. 绘制损失值对比图
# plt.figure(figsize=(8, 5))
# plt.xlabel("Epoch")
# plt.ylabel("Loss")
#
# plt.plot(epoch_list, AlexNet_loss, linewidth=2, color='aqua', label='AlexNet')
# plt.plot(epoch_list, VGG16_loss, linewidth=2, color='#CC0066', label='VGG16')
# plt.plot(epoch_list, GoogleNetActual_loss, linewidth=2, color='green', label='GoogleNet')
# plt.plot(epoch_list, BiTCN_BiLSTM_Attention_loss, linewidth=2, color='blue', label='BiTCN-BiLSTM-Attention')
#
# plt.title('Model Loss Comparison')
# plt.legend()
# plt.show()
# 绘制准确率对比图
plt.figure(figsize=(8, 5))
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.grid(True) # 添加网格线
# 使用不同的颜色和线型,确保在黑白打印时也能区分
plt.plot(epoch_list, AlexNet_accuracy, '-o', color='aqua', label='AlexNet')
plt.plot(epoch_list, VGG16_accuracy, '-s', color='magenta', label='VGG16')
plt.plot(epoch_list, GoogleNetActual_accuracy, '-^', color='green', label='GoogleNet')
plt.plot(epoch_list, BiTCN_BiLSTM_Attention_accuracy, '-d', color='blue', label='BiTCN-BiLSTM-Attention')
plt.title('Model Accuracy Comparison')
plt.legend()
plt.tight_layout() # 调整整体布局
plt.show()
# 绘制损失值对比图
plt.figure(figsize=(8, 5))
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.grid(True) # 添加网格线
plt.plot(epoch_list, AlexNet_loss, '-o', color='aqua', label='AlexNet')
plt.plot(epoch_list, VGG16_loss, '-s', color='magenta', label='VGG16')
plt.plot(epoch_list, GoogleNetActual_loss, '-^', color='green', label='GoogleNet')
plt.plot(epoch_list, BiTCN_BiLSTM_Attention_loss, '-d', color='blue', label='BiTCN-BiLSTM-Attention')
plt.title('Model Loss Comparison')
plt.legend()
plt.tight_layout() # 调整整体布局
plt.show()
过程思路学习
为了使图表看起来更加专业和美观,您可以考虑以下几个方面进行修改:
字体和大小调整:您已经使用了Times New Roman字体,这是一个很好的选择。确保图表中的所有文字(包括图例和轴标签)都是易读的。
颜色和线型:选择不同的颜色和线型,以便在黑白打印时也能容易区分。
添加网格线:网格线可以帮助读者更精确地判断数值。
适当的轴范围和标签:确保轴标签清晰,并考虑轴的范围是否合适。
添加标题和图例:图例应清晰表明每条线代表什么,并且位置不应遮挡图表数据。
精简和清晰的图表设计:避免图表过于拥挤,确保数据点和线清晰可见。