当前位置: 首页 > news >正文

基于KNN算法的鸢尾花种类预测

导入数据

iris_data = load_iris()
iris_data.data[0:5, :]
array([[5.1, 3.5, 1.4, 0.2],[4.9, 3. , 1.4, 0.2],[4.7, 3.2, 1.3, 0.2],[4.6, 3.1, 1.5, 0.2],[5. , 3.6, 1.4, 0.2]])
# 特征值名称
iris_data.feature_names
['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']
# 分类标签
print(iris_data.target_names)
pd.DataFrame(iris_data.target).value_counts()
['setosa' 'versicolor' 'virginica']0    50
1    50
2    50
dtype: int64

简单统计分析

X = pd.DataFrame(iris_data.data, columns=iris_data.feature_names)
y = iris_data.target
X.describe()
sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)
count150.000000150.000000150.000000150.000000
mean5.8433333.0573333.7580001.199333
std0.8280660.4358661.7652980.762238
min4.3000002.0000001.0000000.100000
25%5.1000002.8000001.6000000.300000
50%5.8000003.0000004.3500001.300000
75%6.4000003.3000005.1000001.800000
max7.9000004.4000006.9000002.500000
plt.figure(figsize=(3,3))
sns.heatmap(X.corr(), annot=True)
<Axes: >

在这里插入图片描述

plt.figure(figsize=(4,4))
sns.pairplot(X)
<seaborn.axisgrid.PairGrid at 0x16b89fbf640><Figure size 400x400 with 0 Axes>

划分数据集

x_train, x_test , y_train,y_test = train_test_split(X, y, test_size=.2 , random_state=47)
x_train.shape, y_train.shape, x_test.shape, y_test.shape
((120, 4), (120,), (30, 4), (30,))

特征工程

归一化/标准化

transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
x_train.shape, x_test.shape
((120, 4), (30, 4))

模型训练

knn_model = KNeighborsClassifier(n_neighbors=5)
knn_model.fit(x_train, y_train)
KNeighborsClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
KNeighborsClassifier()

模型评估

y_pred = knn_model.predict(x_test)
y_pred == y_test
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True,  True,  True,  True,  True,True,  True,  True,  True,  True, False,  True,  True,  True,True,  True,  True])
knn_model.score(x_train, y_train)
0.9666666666666667
knn_model.score(x_test, y_test)
0.9666666666666667
http://www.lryc.cn/news/148044.html

相关文章:

  • 英语-面试
  • 文件传输协议
  • Llama-2大模型本地部署研究与应用测试
  • 白嫖idea
  • PyCharm切换虚拟环境
  • 自动化运维工具-----Ansible入门详解
  • 一、Mycat2介绍与下载安装
  • 链表的介绍
  • 深度剖析:数据服务API的安全性与隐私保护
  • MediaPlayer音频与视频的播放介绍
  • 【Terraform学习】Terraform模块基础操作(Terraform模块)
  • 改进的KMeans 点云聚类算法 根据体元中的点数量计算点密度,并获取前K个点密度最大的体元作为初始聚类中心(附 matlab 代码)
  • php user.ini详解
  • 用 PHP 和 JavaScript 显示地球卫星照片
  • Ubantu安装mongodb,开启远程访问和认证
  • 高手速成|数据库脚本生成工具
  • 振动国标2009GB/T 19873.2-2009/ISO 13373-2:2005笔记
  • SpringBoot中自定义starter
  • git-tf clone 路径有空格处理方案
  • IP 地址与域名是一对多的关系。一个 IP 地址可以对应多个域名,但一个域名只对应一个 IP地址。这句话如何理解?
  • DNS解析分类
  • 部署你自己的导航站-dashy
  • 运用谱分解定理反求实对称矩阵
  • Qt——Qt工作原理:事件驱动、信号与槽机制
  • find ./* -type d -empty -exec touch {}/.gitkeep \;
  • 计算机行业前景展望
  • TCP/UDP原理
  • 操作符算数转换题
  • Centos7 安装 Docker
  • Java虚拟机内部组成