Python – 线性回归(Linear Regression) 的 Python 实现

背景

学习 Linear Regression in Python – Real Python,前面几篇文章分别讲了“regression怎么理解“,”线性回归怎么理解“,现在该是实现的时候了。

线性回归的 Python 实现:基本思路

  • 导入 Python 包: 有哪些包推荐呢?
  • 准备数据
  • 建模拟合
  • 验证模型的拟合度
  • 预测:用模型来预测新的数据

实现细节

以最简单的线性回归为例,代码参考的是原文。

重点是掌握基本思路,以及关键的几个函数。影响拟合度的因素很多,数据源首当其冲,模型的选择也是关键,这些在实际应用中具体讨论,这里就简单的对应前面的基本思路将 sample 代码及运行结果贴一下,稍加解释。

安装并导入包

根据自己的需要导入

pip install scikit-learn
pip install numpy
pip install statsmodels

from sklearn.preprocessing import PolynomialFeatures
import numpy as np
from sklearn.linear_model import LinearRegression
import statsmodels.api as sm

准备数据

“”” prepare data
x: regressor
y: predictor
reshape: make it two dimentional – one column and many rows
y can also be 2 dimensional
“””

x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1))
"""
[[ 5]
 [15]
 [25]
 [35]
 [45]
 [55]] 
"""
y = np.array([5, 20, 14, 32, 22, 38])
print(x, y)
# [ 5 20 14 32 22 38]

建模

\'\'\'create a model and fit it\'\'\'
model = LinearRegression()
model = model.fit(x, y)
print(model)
# LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

验证模型的拟合度

\'\'\'get result
y = b0 + b1x
\'\'\'
r_sq = model.score(x, y)
print(\'coefficient of determination(
版权声明:本文为learnbydoing原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/learnbydoing/p/12190168.html?ivk_sa=1024320u