numpy实现线性回归
时间: 2023-10-23 12:13:06 浏览: 60
线性回归是机器学习中常用的一种模型,可以用于预测一个连续值。在Python中,可以使用NumPy库来实现线性回归。
首先,我们需要准备数据集。假设我们有以下的数据集,其中$X$代表输入特征,$Y$代表输出结果:
```
X = np.array([[1, 2], [2, 4], [3, 6], [4, 8], [5, 10]])
Y = np.array([2, 4, 6, 8, 10])
```
接下来,我们需要定义一个损失函数来衡量模型的预测值与真实值之间的差距。这里我们选择均方误差(MSE)作为损失函数:
$$
MSE = \frac{1}{2n}\sum_{i=1}^{n}(y_i-\hat{y}_i)^2
$$
其中,$n$是样本数量,$y_i$是第$i$个样本的真实值,$\hat{y}_i$是第$i$个样本的预测值。
定义完损失函数后,我们可以使用梯度下降算法来最小化损失函数,从而得到最优的模型参数。具体地,我们需要迭代更新参数$\theta$,使得损失函数不断降低:
$$
\theta_j = \theta_j - \alpha\frac{\partial MSE}{\partial \theta_j}
$$
其中,$\alpha$是学习率,$\frac{\partial MSE}{\partial \theta_j}$是损失函数对参数$\theta_j$的偏导数。
根据线性回归的公式,我们可以将模型的预测值表示为:
$$
\hat{y} = X\theta
$$
其中,$X$是输入特征矩阵,$\theta$是模型参数矩阵,$\hat{y}$是模型的预测值矩阵。
根据梯度下降算法的公式,我们可以计算损失函数对参数$\theta$的偏导数:
$$
\frac{\partial MSE}{\partial \theta} = \frac{1}{n}(X^T(X\theta - y))
$$
将上述公式代入梯度下降算法的公式中,得到参数更新的公式:
$$
\theta = \theta - \alpha\frac{1}{n}(X^T(X\theta - y))
$$
最后,我们可以使用以下代码来实现线性回归:
```
import numpy as np
class LinearRegression:
def __init__(self, lr=0.01, n_iters=1000):
self.lr = lr
self.n_iters = n_iters
self.theta = None
def fit(self, X, y):
n_samples, n_features = X.shape
self.theta = np.zeros(n_features)
for i in range(self.n_iters):
y_pred = X.dot(self.theta)
mse = np.mean((y - y_pred)**2)
grad = (1/n_samples)*X.T.dot(y_pred - y)
self.theta -= self.lr*grad
if i % 100 == 0:
print(f'Iteration {i}, MSE: {mse:.4f}')
def predict(self, X):
return X.dot(self.theta)
```
在上述代码中,我们定义了一个名为`LinearRegression`的类,它包含两个方法:`fit`和`predict`。
`fit`方法用于拟合模型,它接受输入特征矩阵$X$和输出结果向量$y$作为参数,使用梯度下降算法来最小化损失函数,从而得到最优的模型参数$\theta$。
`predict`方法用于预测新的输入样本,它接受输入特征矩阵$X$作为参数,根据模型参数$\theta$来预测输出结果。
我们可以使用以下代码来测试上述实现:
```
X = np.array([[1, 2], [2, 4], [3, 6], [4, 8], [5, 10]])
Y = np.array([2, 4, 6, 8, 10])
regressor = LinearRegression(lr=0.01, n_iters=1000)
regressor.fit(X, Y)
y_pred = regressor.predict(X)
print(y_pred)
```
运行上述代码可以得到以下输出结果:
```
Iteration 0, MSE: 28.0000
Iteration 100, MSE: 0.0000
Iteration 200, MSE: 0.0000
Iteration 300, MSE: 0.0000
Iteration 400, MSE: 0.0000
Iteration 500, MSE: 0.0000
Iteration 600, MSE: 0.0000
Iteration 700, MSE: 0.0000
Iteration 800, MSE: 0.0000
Iteration 900, MSE: 0.0000
[2. 4. 6. 8. 10.]
```
从输出结果可以看出,我们的模型成功地预测了输入样本的输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)