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()
版权申明:欢迎转载,但请注明出处
一些博文中有一些参考内容因时间久远找不到来源了没有注明,如果侵权请联系我删除。
在校每年国奖、每年专业第一,加拿大留学,先后工作于华东师范大学和香港教育大学。
2025-04-30:宅加太忙,特此在网上找女朋友,坐标上海,非诚勿扰,vx:fan404006308
AI交流资料群:753014672