您好,登錄后才能下訂單哦!
我就廢話不多說啦,直接上代碼吧!
target = [1.5, 2.1, 3.3, -4.7, -2.3, 0.75] prediction = [0.5, 1.5, 2.1, -2.2, 0.1, -0.5] error = [] for i in range(len(target)): error.append(target[i] - prediction[i]) print("Errors: ", error) print(error) squaredError = [] absError = [] for val in error: squaredError.append(val * val)#target-prediction之差平方 absError.append(abs(val))#誤差絕對值 print("Square Error: ", squaredError) print("Absolute Value of Error: ", absError) print("MSE = ", sum(squaredError) / len(squaredError))#均方誤差MSE from math import sqrt print("RMSE = ", sqrt(sum(squaredError) / len(squaredError)))#均方根誤差RMSE print("MAE = ", sum(absError) / len(absError))#平均絕對誤差MAE targetDeviation = [] targetMean = sum(target) / len(target)#target平均值 for val in target: targetDeviation.append((val - targetMean) * (val - targetMean)) print("Target Variance = ", sum(targetDeviation) / len(targetDeviation))#方差 print("Target Standard Deviation = ", sqrt(sum(targetDeviation) / len(targetDeviation)))#標(biāo)準(zhǔn)差
補充拓展:回歸模型指標(biāo):MSE 、 RMSE、 MAE、R2
sklearn調(diào)用
# 測試集標(biāo)簽預(yù)測 y_predict = lin_reg.predict(X_test) # 衡量線性回歸的MSE 、 RMSE、 MAE、r2 from math import sqrt from sklearn.metrics import mean_absolute_error from sklearn.metrics import mean_squared_error from sklearn.metrics import r2_score print("mean_absolute_error:", mean_absolute_error(y_test, y_predict)) print("mean_squared_error:", mean_squared_error(y_test, y_predict)) print("rmse:", sqrt(mean_squared_error(y_test, y_predict))) print("r2 score:", r2_score(y_test, y_predict))
原生實現(xiàn)
# 測試集標(biāo)簽預(yù)測 y_predict = lin_reg.predict(X_test) # 衡量線性回歸的MSE 、 RMSE、 MAE mse = np.sum((y_test - y_predict) ** 2) / len(y_test) rmse = sqrt(mse) mae = np.sum(np.absolute(y_test - y_predict)) / len(y_test) r2 = 1-mse/ np.var(y_test) print("mse:",mse," rmse:",rmse," mae:",mae," r2:",r2)
相關(guān)公式
MSE
RMSE
MAE
R2
以上這篇python之MSE、MAE、RMSE的使用就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。