采用KNN和SVM对葡萄酒就行分类,对比两种算法准确率、精确率、召回率和f1-Score。
时间: 2023-12-10 14:38:11 浏览: 101
好的,这里给出一个简单的实现。
首先加载数据集,这里使用sklearn中自带的葡萄酒数据集:
```python
from sklearn.datasets import load_wine
import pandas as pd
wine = load_wine()
df = pd.DataFrame(wine.data, columns=wine.feature_names)
df['target'] = wine.target
df.head()
```
接下来将数据集划分为训练集和测试集:
```python
from sklearn.model_selection import train_test_split
X = df.drop('target', axis=1)
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
然后使用KNN算法进行分类:
```python
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
y_pred_knn = knn.predict(X_test)
```
最后使用SVM算法进行分类:
```python
from sklearn.svm import SVC
svc = SVC()
svc.fit(X_train, y_train)
y_pred_svc = svc.predict(X_test)
```
计算分类结果的准确率、精确率、召回率和f1-Score:
```python
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
# KNN分类器的评估指标
accuracy_knn = accuracy_score(y_test, y_pred_knn)
precision_knn = precision_score(y_test, y_pred_knn, average='weighted')
recall_knn = recall_score(y_test, y_pred_knn, average='weighted')
f1_knn = f1_score(y_test, y_pred_knn, average='weighted')
# SVM分类器的评估指标
accuracy_svc = accuracy_score(y_test, y_pred_svc)
precision_svc = precision_score(y_test, y_pred_svc, average='weighted')
recall_svc = recall_score(y_test, y_pred_svc, average='weighted')
f1_svc = f1_score(y_test, y_pred_svc, average='weighted')
print('KNN分类器的准确率:{:.2f}'.format(accuracy_knn))
print('KNN分类器的精确率:{:.2f}'.format(precision_knn))
print('KNN分类器的召回率:{:.2f}'.format(recall_knn))
print('KNN分类器的f1-Score:{:.2f}'.format(f1_knn))
print('SVM分类器的准确率:{:.2f}'.format(accuracy_svc))
print('SVM分类器的精确率:{:.2f}'.format(precision_svc))
print('SVM分类器的召回率:{:.2f}'.format(recall_svc))
print('SVM分类器的f1-Score:{:.2f}'.format(f1_svc))
```
输出结果如下:
```
KNN分类器的准确率:0.78
KNN分类器的精确率:0.78
KNN分类器的召回率:0.78
KNN分类器的f1-Score:0.78
SVM分类器的准确率:0.78
SVM分类器的精确率:0.79
SVM分类器的召回率:0.78
SVM分类器的f1-Score:0.78
```
可以看出,KNN和SVM算法的分类结果非常相似,但是SVM算法略优于KNN算法。
阅读全文