Jupyter —— 简单线性回归分析

本文介绍了如何在Jupyter环境中使用sklearn库进行线性回归分析,包括读取数据、构建模型、计算斜率和截距以及绘制回归曲线。同时,文章提到了sklearn库中normalize参数的弃用,并给出了替代方法。此外,还展示了手动实现线性回归算法的步骤,涉及相关系数的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jupyter —— 简单线性回归分析

我们在前面的博客中已经介绍了什么是线性回归:回归分析

本片博客将从编程的角度介绍线性回归,这里主要分为使用 sklearn 库和非 sklearn 库来两种编程方式

sklearn 库线性回归分析

首先我们要读取本地数据

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

# 读取 excel 表中数据
data = pd.read_excel("D:\code-file\conda\data\weights_heights.xls")
data.shape

这里运行输出(25000,3)表示一共有 5000 组数据,每组有三个数据,对应 excel 表格中的三列

# 读取 20 组数据
y = array(data[['Weight']].values[:20,:])
x = array(data[['Height']].values[:20,:])
# 调用线性回归函数
model = LinearRegression(fit_intercept=True,normalize=True)
model.fit(x,y)

这里我们调用了 sklearn 的 linear_model 中的 LinearRegression() 函数,用于线性回归分析,LinearRegression() 函数共有两个参数:

  • fit_intercept 是否有截据,如果没有则直线过原点

  • normalize 是否将数据归一化

如果我们使用 normalize 参数会生成警告:

警告的意思大概是:

'normalize'在1.0版本中已弃用,将在1.2版本中移除,如果我们要继续使用,这里我们就要用以下方式进行代替:

from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
model = make_pipeline(StandardScaler(with_mean=False), LinearRegression())
# 输出斜率与截距
print("斜率:",model.coef_)
print("截距:",model.intercept_)
b = model.coef_
a = model.intercept_
y = b*x+a
print("线性回归方程为:y = ",float(b),"* x + ",float(a))

最后我们输出了线性回归方程并计算出 R$ ^2 $,

# 绘制图像
prediction = model.predict(y)
plt.xlabel('身高')
plt.ylabel('体重')
# 绘制原始数据散点图
plt.scatter(x,y)
# 绘制回归曲线线图
y1 = b*x + a
plt.plot(x,y1,c='r')

这里我们使用 matplotlib 库进行绘图,图中显示了原有散点分布和我们计算出来的回归方程对应的趋势曲线

如果我们图像中的汉字乱码,添加如下语句即可:

plt.rcParams['font.sans-serif']=['Simhei']

完整代码如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from numpy import array
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression

# 读取 excel 表中数据
data = pd.read_excel("D:\code-file\conda\data\weights_heights.xls",'weights_heights')
data.shape
# 读取 20 组数据
y = array(data[['Weight']].values[:20,:])
x = array(data[['Height']].values[:20,:])
# 调用线性回归函数
model = make_pipeline(StandardScaler(with_mean=False), LinearRegression())
# model=LinearRegression(fit_intercept=True,normalize=True)
model = LinearRegression(fit_intercept=True)
model.fit(x,y)
# 输出斜率与截距
print("斜率:",model.coef_)
print("截距:",model.intercept_)
b = model.coef_
a = model.intercept_
print("线性回归方程为:y = ",float(b),"* x + ",float(a))
R = model.score(x,y)
print(f'相关回归系数:%.4f'%R)
# 绘制图像
prediction = model.predict(y)
plt.rcParams['font.sans-serif']=['Simhei']
plt.xlabel('身高')
plt.ylabel('体重')
# 绘制原始数据散点图
plt.scatter(x,y)
# 绘制回归曲线线图
y1 = b*x + a
plt.plot(x,y1,c='r')

这里我们要是实现对不同数量的数据进行分析,我们可以直接修改其中一个参数,将数据数量从 [:20,:0] 修改为 [:x,:0] 即可

手写算法实现线性回归分析

首先调用对应的包,并从 excel 文件中读取数据:

# 调用包
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
# 调用数据
data = pd.read_excel("D:\code-file\conda\data\weights_heights.xls",'weights_heights')
# 20组数据,需要更多数据修改参数就行
d = data.head(20)
d.shape
x=d["Height"]
y=d["Weight"]

我们这里先画出散点图,来确认我们是否成功读取了正确的数据:

# 绘制散点图图像
plt.scatter(x,y)
plt.axis([65,72,0,180])
plt.show()

这里我们成功调用了数据,下面我们开始计算回归方程和相关系数,这里我们直接使用相关公式进行计算:

\[b = \frac{\sum^n_{i = 1}(x_i - \overline{x})(y_i - \bar{y})}{\sum_{i = 1}^n(x_i - \overline{x})^2} \]
\[a = \bar{y} - b \overline{x} \]
\[r = \frac{\sum^n_{i = 1}(x_i - \overline{x})(y_i - \bar{y})}{\sqrt{\sum_{i = 1}^n(x_i - \overline{x})^2}\sqrt{\sum_{i = 1}^n(y_i - \bar{y})^2}} \]
# 利用公式求出 a、b、r
x_mean = np.mean(x)
y_mean = np.mean(y)
num = 0.0       #分子
d = 0.0         #分母
m = 0.0
for x_i, y_i in zip(x,y):
    num += (x_i - x_mean) * (y_i - y_mean)
    d  += (x_i - x_mean) ** 2
    m += (y_i - y_mean) ** 2
a = num/d
b = y_mean - a * x_mean
y1 = a * x + b
r=(num/((d** 0.5)*(m** 0.5)))**2
print("回归方程为:y = ",a,"x + ",b)
print("相关系数:",r)

画出最终的散点图和趋势曲线:

# 画出最终的散点图(带有趋势曲线)
plt.rcParams['font.sans-serif']=['Simhei']
plt.xlabel('身高')
plt.ylabel('体重')
plt.scatter(x,y)
plt.plot(x,y1,color='r')
plt.axis([65,72,0,180])
plt.show()

参考资料

手写机器学习算法系列01——线性回归

Python计算相关系数

### 实现线性回归 要在 Jupyter Notebook 中实现线性回归,需先导入必要的库并设置环境以便于数据处理和可视化。对于图形显示,在 Notebooks 的起始位置应加入 `%matplotlib inline` 命令以确保图像内嵌展示[^1]。 ```python %matplotlib inline import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression plt.style.use('./deeplearning.mplstyle') ``` 创建一些用于拟合模型的人造数据点有助于理解过程: ```python # 创建样本数据 np.random.seed(0) X = 2 * np.random.rand(100, 1) y = 4 + 3 * X + np.random.randn(100, 1) # 可视化这些点 plt.scatter(X, y, color='blue', label="Data points") plt.xlabel('x') plt.ylabel('y') plt.legend() plt.show() ``` 定义并训练线性回归模型涉及实例化 `LinearRegression` 类,并调用其 `.fit()` 方法来适应输入特征矩阵 \(X\) 和目标向量 \(y\)[^2]: ```python lin_reg = LinearRegression() lin_reg.fit(X, y) print(f'Intercept: {lin_reg.intercept_}') print(f'Slope: {lin_reg.coef_}') ``` 完成上述步骤之后,可以利用此模型预测新观测值的结果。下面是一段简单的代码片段用来绘制原始数据及其对应的回归直线: ```python # 绘制最佳拟合线 plt.plot(X, lin_reg.predict(X), color='red', linewidth=2, label="Best Fit Line") plt.scatter(X, y, color='blue', label="Data Points") plt.title("Simple Linear Regression Example") plt.xlabel('Independent Variable (X)') plt.ylabel('Dependent Variable (Y)') plt.legend() plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ppqppl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值