Specific application of KNN algorithm--Iris flower data processing

The specific application of the KNN algorithm - iris flower data processing

1. Introduction

The KNN algorithm is used here, as shown in the table below, according to the four characteristic values ​​of the iris (calyx length, sepal width, petal length, petal width) to distinguish the iris species, the iris is divided into three types (that is, three Species tag values, respectively, iris iris, iris versicolor, calamus mallow)

feature1 feature2 feature3 feature4 label
Calyx length Calyx width petal length petal width Variety
1 5.1 3.5 1.4 0.2 0
2 4.9 3. 1.4 0.2 0
3 6.2 3.4 5.4 2.3 2

Python's sklearn library comes with iris data sets, so we can use these data sets to train models and predict new iris species.

2. Implementation process
1、数据加载
2、数据获取(获取特征值和标签值)
3、数据分割(原本的数据是按照标签值排序的,所以要将数据打乱。同时将数据分为样本集和测试集)
4、模型选择(当然是KNN)
5、模型训练fit
6、模型评分score
7、模型预测
3. Specific code
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier #分类算法
#数据加载
iris_dataset = load_iris()
#特征数据
iris_feature = iris_dataset["data"]
#标签数据
iris_label = iris_dataset["target"]
#数据分割,这里的x_train和y_train代表样本集的特征值和标签值,x_test和y_test代表测试集的特征值和标签值
x_train,x_test,y_train,y_test = 		train_test_split(iris_feature,iris_label,test_size=0.2,random_state=6)
#算法选择,KNN算法中的K值也是在这里定的
knn = KNeighborsClassifier(n_neighbors=3)
#根据数据训练模型
knn.fit(x_train,y_train)
#模型评分
score = knn.score(x_test,y_test)
print(score)
#模型测试,将分出来的测试集传到训练好的模型中,看看是否和真实的标签值一致
y_predict = knn.predict(x_test)
print(predict_y == y_test)

Guess you like

Origin blog.csdn.net/weixin_58512942/article/details/125676717