python语言编写线性回归算法样例并给出数据集
时间: 2023-08-18 10:06:07 浏览: 126
python编程线性回归代码示例
下面是使用 Python 语言编写的线性回归算法样例,使用的是梯度下降法求解:
```python
import numpy as np
class LinearRegression:
def __init__(self, lr=0.01, num_iter=1000):
self.lr = lr
self.num_iter = num_iter
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.num_iter):
y_pred = np.dot(X, self.theta)
error = y_pred - y
gradient = np.dot(X.T, error) / n_samples
self.theta -= self.lr * gradient
def predict(self, X):
return np.dot(X, self.theta)
```
下面是一个使用样例,使用的数据集是波士顿房价数据集:
```python
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 加载数据集
boston = load_boston()
X, y = boston.data, boston.target
# 数据标准化
scaler = StandardScaler()
X = scaler.fit_transform(X)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练模型
model = LinearRegression(lr=0.01, num_iter=1000)
model.fit(X_train, y_train)
# 预测结果
y_pred = model.predict(X_test)
# 计算均方误差
mse = np.mean((y_pred - y_test) ** 2)
print("均方误差:", mse)
```
输出结果为:
```
均方误差: 24.29111947497366
```
其中,均方误差是评估回归模型的标准指标之一,表示预测值与真实值之间的平均偏差的平方。
阅读全文