前人已写,所以不重复造轮子了,顾粘上相关链接:
https://www.freesion.com/article/5668431209/#METRICS_7
解释一下代码中用到的color = colors[0],需要自定义相关颜色的列表,这里我们可以定义为:
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
同时,源代码里面在显示出现的时候少写了一行代码:
def plot_metrics(history):
metrics = ['loss', 'auc', 'precision', 'recall']
for n, metric in enumerate(metrics):
name = metric
plt.subplot(2,2,n+1)
plt.plot(history.epoch, history.history[metric], color=colors[0], label='Train')
plt.plot(history.epoch, history.history['val_'+metric],
color=colors[0], linestyle="--", label='Val')
plt.xlabel('Epoch')
plt.ylabel(name)
if metric == 'loss':
plt.ylim([0, plt.ylim()[1]])
elif metric == 'auc':
plt.ylim([0.8,1])
else:
plt.ylim([0,1])
plt.legend()
plot_metrics(baseline_history)
应该在for循环的结尾处加上一句:
plt.show()
才能够正确显示所绘制的图像。