python使用后k近邻回归算法,针对红酒数据集进行训练和预测,输出其准确率与混淆矩阵
时间: 2024-02-24 15:00:16 浏览: 73
好的,下面我介绍使用Python进行k近邻回归算法的过程,并且进行红酒数据集的训练和预测,输出准确率和混淆矩阵。
首先,我们需要准备数据集。红酒数据集可以从scikit-learn库中直接导入:
```python
from sklearn.datasets import load_wine
wine = load_wine()
X = wine.data
y = wine.target
```
然后,我们将数据集分成训练集和测试集:
```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)
```
接下来,我们选择k值和距离度量方法。这里我们选择k=5,使用欧氏距离度量:
```python
from sklearn.neighbors import KNeighborsRegressor
knn = KNeighborsRegressor(n_neighbors=5, metric='euclidean')
```
然后,我们使用训练集来训练模型:
```python
knn.fit(X_train, y_train)
```
接着,我们用测试集进行预测,并计算模型的准确率:
```python
from sklearn.metrics import mean_squared_error, r2_score
y_pred = knn.predict(X_test)
print("Mean squared error: %.2f" % mean_squared_error(y_test, y_pred))
print("Coefficient of determination R^2: %.2f" % r2_score(y_test, y_pred))
```
最后,我们可以使用混淆矩阵来分析模型的分类效果:
```python
from sklearn.metrics import confusion_matrix
y_pred = knn.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
print(cm)
```
完整代码如下:
```python
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error, r2_score, confusion_matrix
wine = load_wine()
X = wine.data
y = wine.target
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, metric='euclidean')
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
print("Mean squared error: %.2f" % mean_squared_error(y_test, y_pred))
print("Coefficient of determination R^2: %.2f" % r2_score(y_test, y_pred))
cm = confusion_matrix(y_test, y_pred)
print(cm)
```
这样,我们就完成了使用Python进行k近邻回归算法的过程,并且进行红酒数据集的训练和预测,输出准确率和混淆矩阵。
阅读全文