首页 > 分享 > KNN实现鸢尾花分类

KNN实现鸢尾花分类

KNN实现鸢尾花分类 一、导入相关库二、导入数据1.关于鸢尾花的数据2.删除Id(不需要的)数据3.分别利用花萼和花瓣的特征 三、训练集及测试集1.花瓣2.花萼 四、KNN算法1.花瓣2.花萼3.准确度结果 五、数据可视化1.散点图(1) 花萼(2) 花瓣 2.直方图3.小提琴图4.相关热力图

一、导入相关库

import numpy as np import pandas as pd import seaborn as sns from matplotlib import pyplot as plt from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.neighbors import KNeighborsClassifier from sklearn import svm from sklearn import metrics from sklearn.tree import DecisionTreeClassifier 12345678910

二、导入数据

1.关于鸢尾花的数据

在这里插入图片描述
一共有150个数据

2.删除Id(不需要的)数据

iris.drop('Id',axis=1,inplace=True) 1

3.分别利用花萼和花瓣的特征

petal=iris[['PetalLengthCm','PetalWidthCm','Species']] sepal=iris[['SepalLengthCm','SepalWidthCm','Species']] 12

三、训练集及测试集

1.花瓣

#花萼 train_p,test_p=train_test_split(petal,test_size=0.3,random_state=0) train_x_p=train_p[['PetalWidthCm','PetalLengthCm']] train_y_p=train_p.Species test_x_p=test_p[['PetalWidthCm','PetalLengthCm']] test_y_p=test_p.Species 123456

2.花萼

train_s,test_s=train_test_split(sepal,test_size=0.3,random_state=0) train_x_s=train_s[['SepalWidthCm','SepalLengthCm']] train_y_s=train_s.Species test_x_s=test_s[['SepalWidthCm','SepalLengthCm']] test_y_s=test_s.Species 12345

1.花瓣

model=KNeighborsClassifier() model.fit(train_x_p,train_y_p) prediction=model.predict(test_x_p) print('The accuracy of the Petal is:',metrics.accuracy_score(prediction,test_y_p)) 1234

2.花萼

model=KNeighborsClassifier() model.fit(train_x_s,train_y_s) prediction=model.predict(test_x_s) print('The accuracy of the Sepal is:',metrics.accuracy_score(prediction,test_y_s)) 1234

3.准确度结果

在这里插入图片描述

五、数据可视化

1.散点图

(1) 花萼

#花萼长度和宽度的关系,散点图 fig=iris[iris.Species=='Iris-setosa'].plot(kind='scatter',x='SepalLengthCm',y='SepalWidthCm',color='orange',label='Setosa') iris[iris.Species=='Iris-versicolor'].plot(kind='scatter',x='SepalLengthCm',y='SepalWidthCm',color='blue',label='versicolor',ax=fig) iris[iris.Species=='Iris-virginica'].plot(kind='scatter',x='SepalLengthCm',y='SepalWidthCm',color='green',label='virginica',ax=fig) fig.set_xlabel("Sepal Length") fig.set_ylabel("Sepal Width") fig.set_title("Sepal Length VS Width") fig=plt.gcf() fig.set_size_inches(10,6) plt.show() 12345678910

在这里插入图片描述

(2) 花瓣

#花瓣的长度和宽度的关系,散点图 fig=iris[iris.Species=='Iris-setosa'].plot.scatter(x='PetalLengthCm',y='PetalWidthCm',color='orange',label='Setosa') fig=iris[iris.Species=='Iris-versicolor'].plot.scatter(x='PetalLengthCm',y='PetalWidthCm',color='blue',label='versicolor',ax=fig) fig=iris[iris.Species=='Iris-virginica'].plot.scatter(x='PetalLengthCm',y='PetalWidthCm',color='green',label='virginica',ax=fig) fig.set_xlabel("Petal Length") fig.set_ylabel("Petal Width") fig.set_title("Petal Length VS Width") fig=plt.gcf() fig.set_size_inches(10,6) plt.show() 12345678910

在这里插入图片描述

2.直方图

#iris长度和宽度的分布,绘制直方图 iris.hist(edgecolor='black',linewidth=1.2) fig=plt.gcf() fig.set_size_inches(12,6) plt.show() 12345

在这里插入图片描述

3.小提琴图

#iris长度与宽度跟种类的关系,小提琴图 #小提琴图展示了数据随种类的长度跟密度。越窄的部分说明密度较低,越宽的部分说明数据密度高 plt.figure(figsize=(12,6)) plt.subplot(2,2,1) sns.violinplot(x='Species',y='PetalLengthCm',data=iris) plt.subplot(2,2,2) sns.violinplot(x='Species',y='PetalWidthCm',data=iris) plt.subplot(2,2,3) sns.violinplot(x='Species',y='SepalLengthCm',data=iris) plt.subplot(2,2,4) sns.violinplot(x='Species',y='SepalWidthCm',data=iris) plt.show() 123456789101112

在这里插入图片描述

4.相关热力图

#相关性热力图 plt.figure(figsize=(12,6)) sns.heatmap(iris.corr(),annot=True,cmap='cubehelix_r') plt.show() 1234

在这里插入图片描述

KNN算法原理总结:K近邻算法,给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最近邻的K(自己确定)个实例,这K个实例的多数属于某类,就把该输入实例分类到这个类(label)下

相关知识

【机器学习】KNN算法实现鸢尾花分类
Knn算法实现鸢尾花分类
KNN算法实现鸢尾花数据集分类
KNN分类算法介绍,用KNN分类鸢尾花数据集(iris)
【机器学习】基于KNN算法实现鸢尾花数据集的分类
原生python实现knn分类算法(鸢尾花数据集)
鸢尾花数据分类,通过Python实现KNN分类算法。
【机器学习】应用KNN实现鸢尾花种类预测
Python原生代码实现KNN算法(鸢尾花数据集)
Python实现kNN算法,使用鸢尾花作为测试数据

网址: KNN实现鸢尾花分类 https://m.huajiangbk.com/newsview1548739.html

所属分类:花卉
上一篇: 【可视化】鸢尾花(iris)数据
下一篇: 年终汇报有妙招!8款数据可视化工