基于鸢尾花数据集,采用KNN、SVM、随机树对鸢尾花进行分类,通过对比准确率、精确率等指标,选取最佳算法,代码实现
时间: 2024-01-25 07:02:09 浏览: 82
好的,针对您的问题,我可以给您提供一些Python代码实现。下面的代码演示了如何使用KNN、SVM和随机森林算法对Iris数据集进行分类,并对比它们的准确率和精确率指标,最终选择最佳算法。
首先,我们需要导入必要的库和数据集:
```python
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score
iris_data = load_iris()
X = iris_data.data
y = iris_data.target
```
然后,我们将数据集划分为训练集和测试集,并分别使用KNN、SVM和随机森林算法进行分类:
```python
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 定义KNN模型
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
knn_pred = knn.predict(X_test)
knn_accuracy = accuracy_score(y_test, knn_pred)
knn_precision = precision_score(y_test, knn_pred, average='macro')
# 定义SVM模型
svm = SVC(kernel='linear', C=1.0, random_state=42)
svm.fit(X_train, y_train)
svm_pred = svm.predict(X_test)
svm_accuracy = accuracy_score(y_test, svm_pred)
svm_precision = precision_score(y_test, svm_pred, average='macro')
# 定义随机森林模型
rf = RandomForestClassifier(n_estimators=10, random_state=42)
rf.fit(X_train, y_train)
rf_pred = rf.predict(X_test)
rf_accuracy = accuracy_score(y_test, rf_pred)
rf_precision = precision_score(y_test, rf_pred, average='macro')
```
最后,我们将三个算法的准确率和精确率指标进行对比,并选择表现最好的算法:
```python
# 对比三个算法的准确率和精确率指标
print('KNN Accuracy: %.3f' % knn_accuracy)
print('KNN Precision: %.3f' % knn_precision)
print('SVM Accuracy: %.3f' % svm_accuracy)
print('SVM Precision: %.3f' % svm_precision)
print('Random Forest Accuracy: %.3f' % rf_accuracy)
print('Random Forest Precision: %.3f' % rf_precision)
# 选择表现最好的算法
best_accuracy = max(knn_accuracy, svm_accuracy, rf_accuracy)
best_precision = max(knn_precision, svm_precision, rf_precision)
if knn_accuracy == best_accuracy:
print('Best Algorithm: KNN')
elif svm_accuracy == best_accuracy:
print('Best Algorithm: SVM')
else:
print('Best Algorithm: Random Forest')
```
通过运行上述代码,您可以得到每个算法的准确率和精确率指标,并选择表现最好的算法。
希望这些代码可以帮助您解决问题!
阅读全文