jupyter notebook波士顿房价预测代码
时间: 2023-10-16 20:09:49 浏览: 108
python jupyter notebook房价预测完整案例
5星 · 资源好评率100%
以下是使用sklearn库中的波士顿房价数据集进行预测的Jupyter Notebook代码:
```python
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_train, X_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2, 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)
```
阅读全文