国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現的簡單線性回歸算法實例分析

2020-01-04 13:41:53
字體:
來源:轉載
供稿:網友

本文實例講述了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實現的功能包括:

  1. 計算pearson相關系數
  2. 使用最小二乘法計算回歸系數
  3. 計算擬合優度判定系數R2R2
  4. 計算估計標準誤差Se
  5. 計算顯著性檢驗的F和P值
import numpy as npimport scipy.stats as ssclass 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)    ybar = np.mean(y)    self.r2 = np.sum((self.yhat - ybar) ** 2) / np.sum((y.A - ybar) ** 2)    return  # 計算估計標準誤差  def estimate_deviation(self):    y = np.array(self.yarr)    self.se = np.sqrt(np.sum((y - self.yhat) ** 2) / self.dfd)    return  # 顯著性檢驗F  def sig_test(self):    ybar = np.mean(self.yarr)    self.msr = np.sum((self.yhat - ybar) ** 2)    self.mse = np.sum((self.yarr - self.yhat) ** 2) / self.dfd    self.f = self.msr / self.mse    self.p = ss.f.sf(self.f, 1, self.dfd)    return  def summary(self):    self.simple_regression()    corr_coe = Lm.corr_custom(self.xarr[:, -1], self.yarr)    self.r_square()    self.estimate_deviation()    self.sig_test()    print('The Pearson/'s correlation coefficient: %.3f' % corr_coe)    print('The Regression Coefficient: %s' % self.beta.flatten().A[0])    print('R square: %.3f' % self.r2)    print('The standard error of estimate: %.3f' % self.se)    print('F-statistic: %d on %s and %s DF, p-value: %.3e' % (self.f, 1, self.dfd, self.p))

python執行結果:

The Regression Coefficient: [-87.51666667   3.45      ]
R square: 0.991
The standard error of estimate: 1.525
F-statistic:  1433 on 1 and 13 DF,  p-value: 1.091e-14

其中求回歸系數時用矩陣轉置求逆再用numpy內置的解線性方程組的方法是最快的:

a = np.mat(women.xarr); b = np.mat(women.yarr).Ttimeit (a.I * b)99.9 µs ± 941 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)timeit ata.I * (a.T*b)64.9 µs ± 717 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)timeit np.linalg.solve(ata, a.T*b)15.1 µs ± 126 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

希望本文所述對大家Python程序設計有所幫助。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苗栗县| 盐津县| 营山县| 长葛市| 湄潭县| 临夏县| 龙川县| 铜鼓县| 扎兰屯市| 博爱县| 延庆县| 健康| 缙云县| 阳山县| 台前县| 北安市| 贵溪市| 墨竹工卡县| 松江区| 馆陶县| 崇仁县| 静宁县| 济宁市| 济南市| 潮安县| 勐海县| 华容县| 界首市| 芦溪县| 合肥市| 建平县| 宣化县| 永宁县| 镇坪县| 乐昌市| 卓尼县| 安国市| 高雄县| 阜宁县| 博白县| 房产|