matplotlib库曲线平滑
matplotlib库曲线平滑
一、总结
一句话总结:
上一个节点*0.8+当前节点*0.2:smoothed_points.append(previous * factor + point * (1 - factor))
def smooth_curve(points, factor=0.8): smoothed_points = [] for point in points: if smoothed_points: previous = smoothed_points[-1] # 上一个节点*0.8+当前节点*0.2 smoothed_points.append(previous * factor + point * (1 - factor)) else: # 添加point smoothed_points.append(point) return smoothed_points
二、matplotlib库曲线平滑
博客对应课程的视频位置:
平滑前:
In [14]:
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'b--', label='Training acc')
plt.plot(epochs, val_acc, 'r-', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'b--', label='Training loss')
plt.plot(epochs, val_loss, 'r-', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
平滑后:
In [ ]:
def smooth_curve(points, factor=0.8): smoothed_points = [] for point in points: if smoothed_points: previous = smoothed_points[-1] # 上一个节点*0.8+当前节点*0.2 smoothed_points.append(previous * factor + point * (1 - factor)) else: # 添加point smoothed_points.append(point) return smoothed_points plt.plot(epochs,smooth_curve(acc), 'b--', label='Smoothed training acc') plt.plot(epochs,smooth_curve(val_acc), 'r-', label='Smoothed validation acc') plt.title('Training and validation accuracy') plt.legend() plt.figure() plt.plot(epochs,smooth_curve(loss), 'b--', label='Smoothed training loss') plt.plot(epochs,smooth_curve(val_loss), 'r-', label='Smoothed validation loss') plt.title('Training and validation loss') plt.legend() plt.show()