使用k近邻回归算法,对波士顿房价数据进行预测,并画出真实值与预测值的图像对比图python
时间: 2024-02-24 12:00:43 浏览: 71
好的,我可以为您提供代码。但在此之前,需要确保您已经安装了以下库:numpy、pandas、sklearn、matplotlib。
接下来,我们可以按照以下步骤进行操作:
1. 加载波士顿房价数据集
```python
from sklearn.datasets import load_boston
boston = load_boston()
```
2. 将数据集拆分为特征和目标变量
```python
X = boston.data
y = boston.target
```
3. 对数据集进行训练集和测试集的划分
```python
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
4. 导入k近邻回归算法
```python
from sklearn.neighbors import KNeighborsRegressor
```
5. 创建k近邻回归模型并进行训练
```python
knn = KNeighborsRegressor(n_neighbors=5)
knn.fit(X_train, y_train)
```
6. 对测试集进行预测
```python
y_pred = knn.predict(X_test)
```
7. 绘制真实值与预测值的对比图像
```python
import matplotlib.pyplot as plt
plt.scatter(y_test, y_pred)
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.show()
```
完整代码如下:
```python
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
import matplotlib.pyplot as plt
# 加载波士顿房价数据集
boston = load_boston()
# 将数据集拆分为特征和目标变量
X = boston.data
y = boston.target
# 对数据集进行训练集和测试集的划分
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建k近邻回归模型并进行训练
knn = KNeighborsRegressor(n_neighbors=5)
knn.fit(X_train, y_train)
# 对测试集进行预测
y_pred = knn.predict(X_test)
# 绘制真实值与预测值的对比图像
plt.scatter(y_test, y_pred)
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.show()
```
希望对您有所帮助!
阅读全文