使用python实现线性回归算法
时间: 2023-12-02 18:43:25 浏览: 92
以下是使用Python实现线性回归算法的步骤和代码:
1.导入必要的库和数据集
```python
import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
```
2.生成数据集
```python
X, y = make_regression(n_samples=500, n_features=1, noise=20, random_state=4)
```
3.将数据集分为训练集和测试集
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
```
4.定义线性回归模型
```python
class LinearRegression:
def __init__(self, lr=0.01, n_iters=1000):
self.lr = lr
self.n_iters = n_iters
self.weights = None
self.bias = None
def fit(self, X, y):
# 初始化参数
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
# 梯度下降
for _ in range(self.n_iters):
y_predicted = np.dot(X, self.weights) + self.bias
dw = (1 / n_samples) * np.dot(X.T, (y_predicted - y))
db = (1 / n_samples) * np.sum(y_predicted - y)
self.weights -= self.lr * dw
self.bias -= self.lr * db
def predict(self, X):
y_predicted = np.dot(X, self.weights) + self.bias
return y_predicted
```
5.训练模型并预测结果
```python
regressor = LinearRegression(lr=0.01, n_iters=1000)
regressor.fit(X_train, y_train)
predictions = regressor.predict(X_test)
```
6.评估模型
```python
def mse(y_true, y_pred):
return np.mean((y_true - y_pred) ** 2)
print("Mean Squared Error:", mse(y_test, predictions))
```
阅读全文