knn鸢尾花python
时间: 2023-09-07 21:17:59 浏览: 88
python机器学习入门,KNN鸢尾花分类
5星 · 资源好评率100%
KNN(K-Nearest Neighbor)算法是一种基于实例的学习方法,用于分类和回归问题。对于分类问题,它是一种监督学习方法,可以根据已知的数据集训练一个模型,然后根据新的输入数据进行分类。这里我们以鸢尾花分类为例来演示KNN算法的实现。
首先,我们需要导入相关的库和数据集:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# 加载鸢尾花数据集
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.3, random_state=42)
```
接下来,我们可以使用KNeighborsClassifier类来构建KNN模型,并将训练集数据传递给fit函数进行模型训练:
```python
# 构建KNN模型
knn = KNeighborsClassifier(n_neighbors=3)
# 训练模型
knn.fit(X_train, y_train)
```
在模型训练完成后,我们可以使用测试集数据来进行模型评估:
```python
# 预测测试集数据
y_pred = knn.predict(X_test)
# 计算模型准确率
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
完整的代码如下:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# 加载鸢尾花数据集
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.3, random_state=42)
# 构建KNN模型
knn = KNeighborsClassifier(n_neighbors=3)
# 训练模型
knn.fit(X_train, y_train)
# 预测测试集数据
y_pred = knn.predict(X_test)
# 计算模型准确率
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
运行结果:
```
Accuracy: 1.0
```
阅读全文