# 【回歸模型評估: MAE、MAPE、SSE、MSE、RMSE、R²】 :::info - MAE (Mean Absolute Error),平均絕對誤差 - MAPE (Mean Absolute Percentage Error),平均絕對百分誤差 - SSE (Sum of Squared Errors), 殘差平方和 - MSE (Mean Squared Error) ,均方誤差 - RMSE (Root Mean Squared Error),均方根誤差 - R²,决定系数 - 練習:Python 回歸評估、繪圖 ::: <br/> 迴歸分析中,通常有兩種主要類型的變數: 自變數(Independent Variables): 自變數也叫解釋變數或特徵,主要用來預測因變數。在迴歸分析中,我們會收集一些自變數的數據,例如房屋的大小、廣告的投放金額等 因變數(Dependent Variable): 因變量也較響應變量,是要預測或解釋的變量。在迴歸分析中,建立一個模型,用自變數的值來預測因變數的值,例如房屋的售價、產品銷售等 迴歸分析的目標是找到自變數和因變數之間的關係,並建立一個適當的數學模型來描述這種關係。最簡單的情況是一元線性迴歸,其中只有一個自變數和一個因變數之間的關係可以用一條直線來表示,更進階的還有多元線性迴歸和非線性迴歸 但我們要如何知道這條線是好是壞呢? 可以用每個點與這條線的直線距離,來衡量實際值與預測值的誤差。誤差越小,模型越準確 <br/> ```= 假設有組數字 實際值為 [3.0, -0.5, 2.0, 7.0] 回歸預測值為 [2.5, 0.0, 2.0, 8.0] ``` <br/> ### MAE (Mean Absolute Error),平均絕對誤差 預測值和實際值之間的平均平方誤差,|實際值-預測值|,相加後平均 能夠反映模型在預測中的整體準確性,值越小,模型的預測精度越高 ![螢幕擷取畫面 2024-05-28 223330](https://hackmd.io/_uploads/BkXpjv74R.png) ```= # MAE (∣3.0−2.5∣+∣−0.5−0.0∣+∣2.0−2.0∣+∣7.0−8.0∣) / 4 = 2.0 / 4 = 0.5 ``` <br/> ### MAPE (Mean Absolute Percentage Error),平均絕對百分誤差 MAE 有個盲點,當數值範圍較大時,MAE也會較大,因此會使用MAPE 網路上找的圖 ![螢幕擷取畫面 2024-05-28 234605](https://hackmd.io/_uploads/H1183OQ4C.png) ![螢幕擷取畫面 2024-05-28 234720](https://hackmd.io/_uploads/SJxo3uX4A.png) ```= # MAPE (100/4) * (∣3.0−2.5∣/3.0 + ∣−0.5−0.0∣/(-0.5) + ∣2.0−2.0∣/2.0 + ∣7.0−8.0∣/7.0) = (100/4) * (0.1667+1.0+0.0+0.1429) = 25 * 1.3096 = 32.74% ``` <br/> ### SSE (Sum of Squared Errors), 殘差平方和 (實際值-預測值)的平方,值越小,模型的預測精度越高 ![螢幕擷取畫面 2024-05-28 225433](https://hackmd.io/_uploads/rkbBld740.png) ```= # SSE (3.0−2.5)^2+(−0.5−0.0)^2+(2.0−2.0)^2+(7.0−8.0)^2 = 1.5 ``` <br/> ### MSE (Mean Squared Error) ,均方誤差 (實際值-預測值)的平方,相加後平均。對大誤差較敏感 >P.S >样本越多,SSE越大 >MSE 是 SSE 的平均值,不受樣本數影響 ![螢幕擷取畫面 2024-05-28 223320](https://hackmd.io/_uploads/S183ivXNC.png) ```= # MSE ((3.0−2.5)^2+(−0.5−0.0)^2+(2.0−2.0)^2+(7.0−8.0)^2) / 4 = 1.5/4 = 0.375 ``` <br/> ### RMSE (Root Mean Squared Error),均方根誤差 MSE開根號。更接近實際值,更容易理解 ![螢幕擷取畫面 2024-05-28 223510](https://hackmd.io/_uploads/rytTsvm4C.png) ```= # RMSE √0.375 = 0.612 ``` <br/> ### R²,决定系数 取值範圍是 [0, 1],越接近 1 模型對資料的適合程度越高 >P.S >RSS(Residual Sum of Squares) = SSE (Sum of Squared Errors), 殘差平方和 >TSS(Total Sum of Squares) = SST (Total Sum of Squares),總和平方和 ![螢幕擷取畫面 2024-05-28 224135](https://hackmd.io/_uploads/rykEpDX40.png) ![螢幕擷取畫面 2024-05-28 224405](https://hackmd.io/_uploads/rkFaavmE0.png) ![螢幕擷取畫面 2024-05-28 232214](https://hackmd.io/_uploads/SyqnLuQEA.png) ```= # R² y_mean = (3.0+(-0.5)+2.0+7.0)/4 = 11.5/4 = 2.875 # RSS ((3.0−2.5)^2+(−0.5−0.0)^2+(2.0−2.0)^2+(7.0−8.0)^2) = 1.5 # TSS ((3.0−2.875)^2+(−0.5−2.875)^2+(2.0−2.875)^2+(7.0−2.875)^2) = 23.0625 # R² 1-(RSS/TSS) = 1-(1.5/23.0625) = 1-0.065051 = 0.934949 ``` <br/> ### 練習:Python 回歸評估、繪圖 ```= import numpy as np from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score # 實際值和預測值 y_true = np.array([3.0, -0.5, 2.0, 7.0]) y_pred = np.array([2.5, 0.0, 2.0, 8.0]) # MAE mae = mean_absolute_error(y_true, y_pred) # MAPE mape = np.mean(np.abs((y_true - y_pred) / y_true)) * 100 # SSE sse = np.sum((y_true - y_pred) ** 2) # MSE mse = mean_squared_error(y_true, y_pred) # RMSE rmse = np.sqrt(mse) # R-squared r_squared = r2_score(y_true, y_pred) print("Mean Absolute Error (MAE):", mae) print("Mean Absolute Percentage Error (MAPE):", mape) print("Sum of Squared Errors (SSE):", sse) print("Mean Squared Error (MSE):", mse) print("Root Mean Squared Error (RMSE):", rmse) print("R-squared (R^2):", r_squared) ``` ![螢幕擷取畫面 2024-05-28 235232](https://hackmd.io/_uploads/ByQApuQ4A.png) >預測值與真實值之間的平均差異為0.5 >模型的平均預測誤差約為真實值的 32.74% >預測值與真實值之間的總體差異為1.5 >預測值與真實值之間的平方差異的平均值為0.375 >預測值與真實值之間的平均差異約為 0.612,包含平方的步驟,對較大偏差較敏感 >R-squared表示模型可以解釋因變數 94.9% 的方差,說明模型的擬合效果很好 > >儘管 R^2 指標顯示模型的擬合效果很好,但 MAPE 的值較高可能表示模型在某些情況下的預測能力仍有提升空間 <br/> ```= import numpy as np import matplotlib.pyplot as plt # y_true = np.array([3.0, -0.5, 2.0, 7.0]) y_pred = np.array([2.5, 0.0, 2.0, 8.0]) # plt.scatter(y_true, y_pred, color='blue', label='True vs. Predicted') # 繪製完美預測直線 max_val = max(np.max(y_true), np.max(y_pred)) plt.plot([0, max_val], [0, max_val], color='red', linestyle='--', label='Perfect Prediction') # plt.title('Regression Plot') plt.xlabel('True Values') plt.ylabel('Predicted Values') plt.legend() plt.grid(True) plt.show() ``` ![螢幕擷取畫面 2024-05-28 233359](https://hackmd.io/_uploads/BJ3_YdXVR.png)