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()

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM