python 线性回归示例_线性回归示例(Julia, Python, R)

如何用最小二乘法拟合下列数据?x :258.0, 270.0, 294.0, 320.0, 342.0, 368.0, 396.0, 446.0, 480.0, 586.0

y :236.4, 234.4, 252.8, 298.6, 314.2, 342.2, 360.8, 368.0, 391.2, 390.8

下面采用Julia, Python, R三种科学计算语言进行处理。

julia代码如下:using DataFrames, GLM;using Plots;# Datasetsx = [258.0, 270.0, 294.0, 320.0, 342.0, 368.0, 396.0, 446.0, 480.0, 586.0];y = [236.4, 234.4, 252.8, 298.6, 314.2, 342.2, 360.8, 368.0, 391.2, 390.8];data = DataFrame(X=x, Y=y);# modelols = fit(LinearModel, @formula(Y ~ X), data); # ols = lm(@formula(Y ~ X), data)ypredict = round.(predict(ols), digits=5);println(ols);println(coef(ols));# plotscatter(x, y, marker=:c , c=:blue);plot!(x, ypredict, ls=:solid, c=:red, frame>xlabel!("x");ylabel!("y");savefig("example8_julia");

结果如下:Y ~ 1 + X

Coefficients:

──────────────────────────────────────

Estimate  Std. Error  t value  Pr(>|t|)  Lower 95%   Upper 95%

──────────────────────────────────────

(Intercept)  115.786     33.3936     3.4673     0.0085   38.7797   192.791

X              0.540304   0.0859298  6.28774    0.0002    0.34215    0.738459

──────────────────────────────────────

[115.78555030272875, 0.5403043874927427]

=

python代码如下:# encoding:utf8import numpy as npimport matplotlib.pyplot as pltfrom sklearn import linear_modelfrom sklearn.metrics import mean_squared_error, r2_score# Datasetsx = np.array([258.0, 270.0, 294.0, 320.0, 342.0, 368.0, 396.0, 446.0, 480.0, 586.0])[:, np.newaxis]y = np.array([236.4, 234.4, 252.8, 298.6, 314.2, 342.2, 360.8, 368.0, 391.2, 390.8])# modelreg = linear_model.LinearRegression()reg.fit(x, y)ypredict = reg.predict(x)print("Coefficients: \n", reg.coef_) # The coefficientsprint("Mean squared error: %.2f"% mean_squared_error(y, ypredict)) # The mean squared errorprint("Variance score: %.2f" % r2_score(y, ypredict)) # Explained variance score: 1 is perfect prediction# plotplt.figure(figsize=(10, 6))plt.plot(x, y, "o")plt.plot(x, ypredict, "-")plt.xlabel("x", fontsize = 20)plt.ylabel("y", fontsize = 20)plt.savefig("example8_python.png")plt.show()

结果如下:Coefficients: [0.54030439]

Mean squared error: 569.78

Variance score: 0.83

=

R代码如下:# Datasetsx

结果如下:> print(summary(lm(y~x)))

Call:

lm(formula = y ~ x)

Residuals:

Min     1Q Median     3Q    Max

-41.60 -21.07  10.58  15.46  31.05

Coefficients:

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

(Intercept) 115.78555   33.39361   3.467 0.008476 **

x             0.54030    0.08593   6.288 0.000236 ***

---

Signif. codes:  0 "***" 0.001 "**" 0.01 "*" 0.05 "." 0.1 " " 1

Residual standard error: 26.69 on 8 degrees of freedom

Multiple R-squared:  0.8317,    Adjusted R-squared:  0.8107

F-statistic: 39.54 on 1 and 8 DF,  p-value: 0.0002359

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

简称:NLF;

爱好:Coding,数据分析,散步,动漫迷,冥想

更刊说明:受动漫启发,推行一周一刊制度,每刊不限一条内容,每周日定期推刊,偶尔也会休刊

,多多关照!

刊物内容:主推技术流(涉及基于Julia、Python、R、Matlab、C\C++等语言的科普文),学科流(OpenFOAM等流体力学求解器知识、生态水文学习中),统计学;

近期规划:完成硕士毕业论文,备考生态水文学方向的博士。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值