波士顿房价预测线性回归python代码
时间: 2023-09-04 16:16:21 浏览: 387
python实现线性回归预测波士顿房价-机器学习经典问题项目源码(高分项目).zip
好的,下面是波士顿房价预测的线性回归Python代码:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# 读取数据集
boston = load_boston()
X = pd.DataFrame(boston.data, columns=boston.feature_names)
y = pd.Series(boston.target)
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 建立线性回归模型
model = LinearRegression()
model.fit(X_train, y_train)
# 预测并计算误差
y_pred = model.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print("MSE: ", mse)
```
希望这能回答您的问题!
阅读全文