波士顿房价预测pycharm代码
时间: 2023-11-11 20:59:29 浏览: 122
以下是波士顿房价预测的PyCharm代码:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# 加载数据集
boston = load_boston()
data = pd.DataFrame(boston.data, columns=boston.feature_names)
data['PRICE'] = boston.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(data[boston.feature_names], data['PRICE'], 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)
```
阅读全文