200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > python中回归分析的算法_Python实现的简单线性回归算法实例分析

python中回归分析的算法_Python实现的简单线性回归算法实例分析

时间:2023-12-05 18:23:49

相关推荐

python中回归分析的算法_Python实现的简单线性回归算法实例分析

本文实例讲述了Python实现的简单线性回归算法。分享给大家供大家参考,具体如下:

用python实现R的线性模型(lm)中一元线性回归的简单方法,使用R的women示例数据,R的运行结果:> summary(fit)

Call:

lm(formula = weight ~ height, data = women)

Residuals:

Min 1Q Median 3Q Max

-1.7333 -1.1333 -0.3833 0.7417 3.1167

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) -87.51667 5.93694 -14.74 1.71e-09 ***

height 3.45000 0.09114 37.85 1.09e-14 ***

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ‘ 1

Residual standard error: 1.525 on 13 degrees of freedom

Multiple R-squared: 0.991, Adjusted R-squared: 0.9903

F-statistic: 1433 on 1 and 13 DF, p-value: 1.091e-14

python实现的功能包括:

计算pearson相关系数

使用最小二乘法计算回归系数

计算拟合优度判定系数R2R2

计算估计标准误差Se

计算显著性检验的F和P值

import numpy as np

import scipy.stats as ss

class Lm:

"""简单一元线性模型,计算回归系数、拟合优度的判定系数和

估计标准误差,显著性水平"""

def __init__(self, data_source, separator):

self.beta = np.matrix(np.zeros(2))

self.yhat = np.matrix(np.zeros(2))

self.r2 = 0.0

self.se = 0.0

self.f = 0.0

self.msr = 0.0

self.mse = 0.0

self.p = 0.0

data_mat = np.genfromtxt(data_source, delimiter=separator)

self.xarr = data_mat[:, :-1] self.yarr = data_mat[:, -1] self.ybar = np.mean(self.yarr)

self.dfd = len(self.yarr) - 2 # 自由度n-2

return

# 计算协方差

@staticmethod

def cov_custom(x, y):

result = sum((x - np.mean(x)) * (y - np.mean(y))) / (len(x) - 1)

return result

# 计算相关系数

@staticmethod

def corr_custom(x, y):

return Lm.cov_custom(x, y) / (np.std(x, ddof=1) * np.std(y, ddof=1))

# 计算回归系数

def simple_regression(self):

xmat = np.mat(self.xarr)

ymat = np.mat(self.yarr).T

xtx = xmat.T * xmat

if np.linalg.det(xtx) == 0.0:

print('Can not resolve the problem')

return

self.beta = np.linalg.solve(xtx, xmat.T * ymat) # xtx.I * (xmat.T * ymat)

self.yhat = (xmat * self.beta).flatten().A[0] return

# 计算拟合优度的判定系数R方,即相关系数corr的平方

def r_square(self):

y = np.mat(self.yarr)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。