.基于鸢尾花数据集的算法测试:使用Python的Scikit-learn库对Iris数据集进行KNN分类模型构建、训练、预测以及模型评估,同时利用matplotlib将数据可视化。,给出具体代码
时间: 2024-10-23 17:10:20 浏览: 37
Python:通过scikit-learn了解机器学习
在Python中,我们可以使用Scikit-learn库中的Iris数据集来演示如何构建KNN(K-Nearest Neighbors,K近邻)分类模型。以下是具体的步骤:
```python
# 导入所需库
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report
# 加载鸢尾花数据集
iris = datasets.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分类器并设置k值为3
knn_model = KNeighborsClassifier(n_neighbors=3)
# 训练模型
knn_model.fit(X_train, y_train)
# 预测
y_pred = knn_model.predict(X_test)
# 评估模型
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
print("Classification Report:")
print(classification_report(y_test, y_pred))
# 数据可视化
plt.figure(figsize=(10, 5))
for i in range(3):
plt.subplot(1, 3, i+1)
plt.scatter(X[y == i, 0], X[y == i, 1])
plt.title(f"Iris {i+1} Class")
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.show()
阅读全文