1.使用k近邻回归算法,对波士顿房价数据进行预测,并画出真实值与预测值的图像对比冬 2.使用k近邻分类算法,针对红酒数据集进行训练和预测,输出其准确率与混淆矩阵
时间: 2024-01-12 07:03:16 浏览: 95
K最近邻算法在分类和预测中的应用
5星 · 资源好评率100%
好的,我会尽力回答您的问题。
1. 对波士顿房价数据进行预测使用k近邻回归算法,并画出真实值与预测值的图像对比
首先,我们需要加载波士顿房价数据集,代码如下:
```python
from sklearn.datasets import load_boston
import pandas as pd
boston = load_boston()
data = pd.DataFrame(boston.data, columns=boston.feature_names)
data['PRICE'] = boston.target
```
接下来,我们将数据集分为训练集和测试集,然后使用k近邻回归算法进行训练和预测,代码如下:
```python
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error
X = data.drop('PRICE', axis=1)
y = data['PRICE']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
knn = KNeighborsRegressor(n_neighbors=5)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
```
最后,我们可以使用matplotlib库画出真实值与预测值的图像对比,代码如下:
```python
import matplotlib.pyplot as plt
plt.scatter(y_test, y_pred)
plt.plot([0, 50], [0, 50], '--k')
plt.xlabel('True price ($1000s)')
plt.ylabel('Predicted price ($1000s)')
plt.title('Boston house prices: true vs. predicted')
plt.show()
```
2. 使用k近邻分类算法,针对红酒数据集进行训练和预测,输出其准确率与混淆矩阵
首先,我们需要加载红酒数据集,代码如下:
```python
from sklearn.datasets import load_wine
import pandas as pd
wine = load_wine()
data = pd.DataFrame(wine.data, columns=wine.feature_names)
data['CLASS'] = wine.target
```
接下来,我们将数据集分为训练集和测试集,然后使用k近邻分类算法进行训练和预测,代码如下:
```python
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
X = data.drop('CLASS', axis=1)
y = data['CLASS']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
```
最后,我们可以使用accuracy_score函数输出准确率,使用confusion_matrix函数输出混淆矩阵,代码如下:
```python
print('Accuracy:', accuracy_score(y_test, y_pred))
print('Confusion matrix:\n', confusion_matrix(y_test, y_pred))
```
希望能够帮助到您!
阅读全文