# 【回归算法 评价指标】MAE,MSE,RMSE,R-square计算函数

# -*- coding: utf-8 -*-

import math
import numpy as np
import pandas as pd

# 回归评估指标计算，平均绝对误差，均方误差，均方根误差
def compute_mae_mse_rmse(target,prediction):
error = []
for i in range(len(target)):
error.append(target[i] - prediction[i])
squaredError = []
absError = []
for val in error:
squaredError.append(val * val)  # target-prediction之差平方
absError.append(abs(val))  # 误差绝对值
mae=sum(absError)/len(absError)  # 平均绝对误差MAE
mse=sum(squaredError)/len(squaredError)  # 均方误差MSE
RMSE=sum(absError)/len(absError)
return mae,mse,RMSE

def compute_rsquared(X, Y):
xBar = np.mean(X)
yBar = np.mean(Y)
SSR = 0
varX = 0
varY = 0
for i in range(0, len(X)):
diffXXBar = X[i] - xBar
diffYYBar = Y[i] - yBar
SSR += (diffXXBar * diffYYBar)
varX += diffXXBar ** 2
varY += diffYYBar ** 2

SST = math.sqrt(varX * varY)
r2=round((SSR / SST) ** 2,3)
return r2

if __name__ == '__main__':
testX = [174.5, 171.2, 172.9, 161.6, 123.6, 112.1, 107.1, 98.6, 98.7, 97.5, 95.8, 93.5, 91.1, 85.2, 75.6, 72.7,
68.6,
69.1, 63.8, 60.1, 65.2, 71, 75.8, 77.8]
testY = [88.3, 87.1, 88.7, 85.8, 89.4, 88, 83.7, 73.2, 71.6, 71, 71.2, 70.5, 69.2, 65.1, 54.8, 56.7, 62, 68.2, 71.1,
76.1, 79.8, 80.9, 83.7, 85.8]

mae,mse,rmse=compute_mae_mse_rmse(testX,testY)
print("MAE:%.4f " %mae)
print("MSE:%.4f " %mse)
print("RMSE:%.4f "%rmse)

r2=compute_rsquared(testX,testY)
print("R2：%.4f " %r2)


MAE:28.6917
MSE:1467.3342
RMSE:28.6917
R2：0.3710


07-16 1万+

07-23 2757

01-24 1160

01-19 10万+

07-26 2万+

06-22 2383

11-22 2万+

02-26 3万+

10-08 122

08-30 10万+

06-18 1643

12-29 20万+

06-25 519

03-15 80

02-21 381

09-18 4万+

10-25

02-03 8万+

06-03 220

07-19 5560

01-05 27万+

04-14 58万+

09-23 1823

03-19 81万+

04-05 444

06-15 1万+

11-19 2467

08-26 2万+

06-02 4497

07-10 149

03-15 2889

12-24 1万+

10-25 1万+

01-24 1万+

02-13 29万+

07-14 8514

05-23 612

03-22 644

02-25 38万+

03-13 14万+

03-01 13万+

03-04 13万+

03-08 4万+

03-08 7万+

04-25 6万+

03-10 12万+

03-12 11万+

03-13 11万+

03-14 1万+

03-18 9198

03-19 3万+

03-19 8万+

03-19 3万+

03-22 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-27 1万+

03-29 21万+

03-29 10万+

03-30 16万+

05-28 5547

05-28 1万+

04-02 3499

04-02 4万+

04-06 7万+

04-09 8万+

04-09 2万+

05-17 7899

04-11 3万+

04-15 6万+

04-18 4万+

04-20 4万+

04-24 3万+

04-29 6734

04-30 9709

04-30 9244

#### 在北京，我遇上每天必须挣1000元的出租车司机！

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客