擬合優度(Goodness of Fit) 是指回歸直線對觀測值的擬合程度。度量擬合優度的統計量是可決系數(亦稱確定系數)R²。
一般來說,擬合優度到達 0.8 以上就可以說擬合效果不錯了。
# #################################擬合優度R^2的計算######################################
def __sst(y_no_fitting):
""" 計算SST(total sum of squares) 總平方和 :param y_no_predicted: List[int] or array[int] 待擬合的y :return: 總平方和SST """
y_mean = sum(y_no_fitting) / len(y_no_fitting)
s_list =[(y - y_mean)**2 for y in y_no_fitting]
sst = sum(s_list)
return sst
def __ssr(y_fitting, y_no_fitting):
""" 計算SSR(regression sum of squares) 回歸平方和 :param y_fitting: List[int] or array[int] 擬合好的y值 :param y_no_fitting: List[int] or array[int] 待擬合y值 :return: 回歸平方和SSR """
y_mean = sum(y_no_fitting) / len(y_no_fitting)
s_list =[(y - y_mean)**2 for y in y_fitting]
ssr = sum(s_list)
return ssr
def __sse(y_fitting, y_no_fitting):
""" 計算SSE(error sum of squares) 殘差平方和 :param y_fitting: List[int] or array[int] 擬合好的y值 :param y_no_fitting: List[int] or array[int] 待擬合y值 :return: 殘差平方和SSE """
s_list = [(y_fitting[i] - y_no_fitting[i])**2 for i in range(len(y_fitting))]
sse = sum(s_list)
return sse
def goodness_of_fit(y_fitting, y_no_fitting):
""" 計算擬合優度R^2 :param y_fitting: List[int] or array[int] 擬合好的y值 :param y_no_fitting: List[int] or array[int] 待擬合y值 :return: 擬合優度R^2 """
SSR = __ssr(y_fitting, y_no_fitting)
SST = __sst(y_no_fitting)
rr = SSR /SST
return rr
import random
import matplotlib.pyplot as plt
# 生成待擬合數據
a = np.arange(10)
# 通過添加正態噪聲,創造擬合好的數據
b = a + 0.4 * np.random.normal(size=len(a))
print("原始數據為: ", a)
print("擬合數據為: ", b)
rr = goodness_of_fit(b, a)
print("擬合優度為:", rr)
plt.plot(a, a, color="#72CD28", label='原始數據')
plt.plot(a, b, color="#EBBD43", label='擬合數據')
plt.legend()
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用來正常顯示中文標簽
plt.rcParams['axes.unicode_minus'] = False # 用來正常顯示負號
plt.savefig(r"C:\Users\Yunger_Blue\Desktop\temp.jpg")
plt.show()
結果為:
原始數據為: [0 1 2 3 4 5 6 7 8 9]
擬合數據為: [0.23705933 1.20951491 2.37326542 3.00448608 3.48391211 4.30719527 5.95446175 7.50969723 8.97662945 8.27064816]
擬合優度為: 0.9971013400436336
參考資料
[1] 擬合優度R^2 2019.8
[2] 數學建模方法—【03】擬合優度的計算(python計算) 2020.8