使用python语言 学习k近邻分类器的api

欢迎来到我的git查看源代码: https://github.com/linyi0604/kaggle

1 from sklearn.datasets import load_iris

2 from sklearn.cross_validation import train_test_split

3 from sklearn.preprocessing import StandardScaler

4 from sklearn.neighbors import KNeighborsClassifier

5 from sklearn.metrics import classification_report

6

7 ‘‘‘

8 k近邻分类器

9 通过数据的分布对预测数据做出决策

10 属于无参数估计的一种

11 非常高的计算复杂度和内存消耗

12 ‘‘‘

13

14 ‘‘‘

15 1 准备数据

16 ‘‘‘

17 # 读取鸢尾花数据集

18 iris = load_iris()

19 # 检查数据规模

20 # print(iris.data.shape) # (150, 4)

21 # 查看数据说明

22 # print(iris.DESCR)

23 ‘‘‘

24 Iris Plants Database

25 ====================

26

27 Notes

28 -----

29 Data Set Characteristics:

30 :Number of Instances: 150 (50 in each of three classes)

31 :Number of Attributes: 4 numeric, predictive attributes and the class

32 :Attribute Information:

33 - sepal length in cm

34 - sepal width in cm

35 - petal length in cm

36 - petal width in cm

37 - class:

38 - Iris-Setosa

39 - Iris-Versicolour

40 - Iris-Virginica

41 :Summary Statistics:

42

43 ============== ==== ==== ======= ===== ====================

44 Min Max Mean SD Class Correlation

45 ============== ==== ==== ======= ===== ====================

46 sepal length: 4.3 7.9 5.84 0.83 0.7826

47 sepal width: 2.0 4.4 3.05 0.43 -0.4194

48 petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)

49 petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)

50 ============== ==== ==== ======= ===== ====================

51

52 :Missing Attribute Values: None

53 :Class Distribution: 33.3% for each of 3 classes.

54 :Creator: R.A. Fisher

55 :Donor: Michael Marshall (MARSHALL%[email protected])

56 :Date: July, 1988

57

58 This is a copy of UCI ML iris datasets.

59 http://archive.ics.uci.edu/ml/datasets/Iris

60

61 The famous Iris database, first used by Sir R.A Fisher

62

63 This is perhaps the best known database to be found in the

64 pattern recognition literature. Fisher‘s paper is a classic in the field and

65 is referenced frequently to this day. (See Duda & Hart, for example.) The

66 data set contains 3 classes of 50 instances each, where each class refers to a

67 type of iris plant. One class is linearly separable from the other 2; the

68 latter are NOT linearly separable from each other.

69

70 References

71 ----------

72 - Fisher,R.A. "The use of multiple measurements in taxonomic problems"

73 Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to

74 Mathematical Statistics" (John Wiley, NY, 1950).

75 - Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis.

76 (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.

77 - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System

78 Structure and Classification Rule for Recognition in Partially Exposed

79 Environments". IEEE Transactions on Pattern Analysis and Machine

80 Intelligence, Vol. PAMI-2, No. 1, 67-71.

81 - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions

82 on Information Theory, May 1972, 431-433.

83 - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II

84 conceptual clustering system finds 3 classes in the data.

85 - Many, many more ...

86

87 共有150个数据样本

88 均匀分布在3个亚种上

89 每个样本采样4个花瓣、花萼的形状描述

90 ‘‘‘

91

92 ‘‘‘

93 2 划分训练集合和测试集合

94 ‘‘‘

95 x_train, x_test, y_train, y_test = train_test_split(iris.data,

96 iris.target,

97 test_size=0.25,

98 random_state=33)

99

100 ‘‘‘

101 3 k近邻分类器 学习模型和预测

102 ‘‘‘

103 # 训练数据和测试数据进行标准化

104 ss = StandardScaler()

105 x_train = ss.fit_transform(x_train)

106 x_test = ss.transform(x_test)

107

108 # 建立一个k近邻模型对象

109 knc = KNeighborsClassifier()

110 # 输入训练数据进行学习建模

111 knc.fit(x_train, y_train)

112 # 对测试数据进行预测

113 y_predict = knc.predict(x_test)

114

115 ‘‘‘

116 4 模型评估

117 ‘‘‘

118 print("准确率:", knc.score(x_test, y_test))

119 print("其他指标:\n", classification_report(y_test, y_predict, target_names=iris.target_names))

120 ‘‘‘

121 准确率: 0.8947368421052632

122 其他指标:

123 precision recall f1-score support

124

125 setosa 1.00 1.00 1.00 8

126 versicolor 0.73 1.00 0.85 11

127 virginica 1.00 0.79 0.88 19

128

129 avg / total 0.92 0.89 0.90 38

130 ‘‘‘

python分类器鸢尾花怎么写_机器学习之路: python k近邻分类器 鸢尾花分类预测相关推荐

  1. 基于python的分类预测_机器学习算法(五): 基于支持向量机的分类预测

    声明:本次撰写以Datawhale团队提供的学习材料以自学为主,代码为Datawhale团队提供,利用阿里云天池实验室与编辑器pycharm完成测试. 支持向量机(Support Vector Mac ...

  2. python设计思路怎么写_初中信息技术 初识Python教学设计

    案例名称 神秘的蟒蛇-初识 Python 科目 信息技术 教学对象 八年级 设计者 叶新苗 课时 1 课时 所用教材 湖北教育出版社 一.教材内容分析 <初识 Python > 是鄂教版教 ...

  3. 【机器学习入门】(1) K近邻算法:原理、实例应用(红酒分类预测)附python完整代码及数据集

    各位同学好,今天我向大家介绍一下python机器学习中的K近邻算法.内容有:K近邻算法的原理解析:实战案例--红酒分类预测.红酒数据集.完整代码在文章最下面. 案例简介:有178个红酒样本,每一款红酒 ...

  4. 除了 Python ,这些语言写的机器学习项目也很牛(二)

    2019独角兽企业重金招聘Python工程师标准>>> Python 由于本身的易用优势和强大的工具库储备,成为了在人工智能及其它相关科学领域中最常用的语言之一.尤其是在机器学习,已 ...

  5. 机器学习 —— 基础整理(三)生成式模型的非参数方法: Parzen窗估计、k近邻估计;k近邻分类器...

    本文简述了以下内容: (一)生成式模型的非参数方法 (二)Parzen窗估计 (三)k近邻估计 (四)k近邻分类器(k-nearest neighbor,kNN) (一)非参数方法(Non-param ...

  6. 【Python机器学习】多项式回归、K近邻KNN回归的讲解及实战(图文解释 附源码)

    需要源码请点赞关注收藏后评论区留言私信~~~ 多项式回归 非线性回归是用一条曲线或者曲面去逼近原始样本在空间中的分布,它"贴近"原始分布的能力一般较线性回归更强. 多项式是由称为不 ...

  7. 机器学习算法(七): 基于LightGBM的分类预测(基于英雄联盟10分钟数据判断红蓝方胜负)

    机器学习算法(七)基于LightGBM的分类预测 1. 实验室介绍 1.1 LightGBM的介绍 LightGBM是2017年由微软推出的可扩展机器学习系统,是微软旗下DMKT的一个开源项目,由20 ...

  8. 图片分类-K近邻分类器

    你可以注意到当我们做预测的时候,如果仅仅用最近的图片的是远远不够的.其实,我们更常用的是k近邻分类器.这个思想非常简单.代替寻找训练集中最近的图片,我们会寻找k个最相近的图片,并且让他们再测试图片上投 ...

  9. K近邻分类器(李飞飞CS231n学习笔记---lecture2:K最近邻算法)

    在讲解K近邻分类器之前,我们先来看一下最近邻分类器(Nearest Neighbor Classifier),它也是K = 1时的K近邻分类器. 目录 最近邻分类器 定义 存在问题 K近邻分类器(KN ...

  10. k近邻分类器的使用:简单例子

    #k近邻分类器的简单例子 from sklearn.neighbors import KNeighborsClassifier #创建一组数据x和它对应的标签y: x = [[0], [1], [2] ...

最新文章

  1. BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]
  2. 抢红包算法 c语言,红包分配算法,抢红包算法
  3. DFS(6)——hdu1342Lotto
  4. Java高并发编程:多个线程之间共享数据的方式探讨
  5. 选对工具,你也能做出别人家的酷炫大屏
  6. Postman同时发送多个对象+文件到Controller的实现方法
  7. Python之SQLAlchemy学习
  8. Java for 语句简化写法_Java 8 Lambda 写法与简化
  9. Kotlin基础-对象声明和表达式
  10. 利用HttpOnly来防御xss攻击
  11. [iView warn]: please transfer a valid prop path to form item
  12. 私活之安卓论坛Demo
  13. 知识整理的八种笔记方法
  14. cad 2019 mac安装破解详细图文教程
  15. 分析社会热点与网络营销的关联
  16. redhat红帽官方软件仓库同步方案
  17. 三大报再呼救市:A股估值到了崩溃边缘
  18. 美通社企业新闻汇总 | 2019.1.10 | 全球最受欢迎商旅城市上海第四,华为发布AI数据中心交换机...
  19. 广东省揭阳市谷歌卫星地图下载
  20. android如何添加透明图片按钮,如何拥有透明的ImageButton:Android

热门文章

  1. matplotlib易混概念理解与画图详解
  2. 建立云服务器_中国云游戏元年 顺网科技跻身头号玩家队列
  3. python get 函数-python的__get__、__set__、__delete__(1)
  4. mysql double 和Oracle,oraclemysql对比
  5. html限制显示字数其余用...代替,html实现钝角效果;html实现限制一行字数的显示,超出的部分用省略号(....)来代替...
  6. android 字体适配_移动端postcss-pxtorem rem适配方案
  7. 动态提出的数据怎么换行 js_前端代码动态生成应用及改造
  8. os系统安装python_在MacOS系统上安装疯子Python
  9. 现场操作前,软件界面的各个功能面板应该通过拖动进行合理布局.请看参考图
  10. 直观讲解--RPC调用和HTTP调用的区别