python评价回归模型指标:决定系数R2,相关系数R,均方误差MSE,均方根误差RMSE
程序员文章站
2023-12-27 08:20:21
python实现回归相关系数计算的几种方法#计算回归相关系数的方法 确定Ok#第一种def calc_corr(a,b): a_avg = sum(a)/len(a) b_avg = sum(b)/len(b) cov_ab = sum([(x - a_avg)*(y - b_avg) for x,y in zip(a, b)]) sq = math.sqrt(sum([(x - a_avg)**2 for x in a])*sum([(x - b_avg)**2...
#计算回归相关系数的方法 确定Ok
#相关系数第一种
def calc_corr(a,b):
a_avg = sum(a)/len(a)
b_avg = sum(b)/len(b)
cov_ab = sum([(x - a_avg)*(y - b_avg) for x,y in zip(a, b)])
sq = math.sqrt(sum([(x - a_avg)**2 for x in a])*sum([(x - b_avg)**2 for x in b]))
corr_factor = cov_ab/sq
return corr_factor
#相关系数第二种
import numpy as np
from astropy.units import Ybarn
import math
def computeCorrelation(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)
return SSR / SST
#决定系数
from sklearn.metrics import r2_score
r2_score(y_true,y_pred)
#均方误差、均方根误差
from sklearn.merics import mean_squared_error
mes = mean_squared_error(y_true,y_pred)
rmse = np.sqrt(mse)
本文地址:https://blog.csdn.net/Ms__zhao/article/details/107352580