写一段代码,生成或从sklearn.datasets中读取任意数据集(例如鸢尾花数据 iris ),用KNN和决策树两个模型分别训练和测试,用3个及以上评价标注评估两个模型的性能。
时间: 2024-05-12 08:16:39 浏览: 59
好的,以下是代码实现:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score,confusion_matrix
# 加载数据集
iris = load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练和测试KNN模型
knn_clf = KNeighborsClassifier(n_neighbors=5)
knn_clf.fit(X_train, y_train)
knn_y_pred = knn_clf.predict(X_test)
# 训练和测试决策树模型
dt_clf = DecisionTreeClassifier(random_state=42)
dt_clf.fit(X_train, y_train)
dt_y_pred = dt_clf.predict(X_test)
# 评估两个模型性能
metrics = ['accuracy', 'precision_macro', 'recall_macro', 'f1_macro']
for metric in metrics:
knn_score = eval(f"{metric}_score(y_test, knn_y_pred)")
dt_score = eval(f"{metric}_score(y_test, dt_y_pred)")
print(f"{metric}: KNN score = {knn_score:.3f}, Decision Tree score = {dt_score:.3f}")
# 输出混淆矩阵
knn_cm = confusion_matrix(y_test, knn_y_pred)
dt_cm = confusion_matrix(y_test, dt_y_pred)
print("KNN Confusion Matrix:")
print(knn_cm)
print("Decision Tree Confusion Matrix:")
print(dt_cm)
```
代码中先用`load_iris()`从sklearn.datasets中读取鸢尾花数据,然后使用`train_test_split()`函数划分训练集和测试集。接着,分别应用KNN和决策树模型进行训练和测试,并使用`accuracy_score()`、`precision_score()`、`recall_score()`及`f1_score()`等评价指标评估两个模型性能。最后,输出两个混淆矩阵,以便更直观地查看不同类别被错误分类的情况。
阅读全文