鱼C论坛

 找回密码
 立即注册
查看: 4169|回复: 21

[技术交流] python小练习(088):【番外篇】不用机器学习的方法进行数字校验码的自动识别

[复制链接]
发表于 2017-4-7 11:47:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 jerryxjr1220 于 2017-4-7 14:33 编辑

前面几章都是介绍利用keras搭建的神经网络进行机器的自动学习。

有鱼油反应,机器学习太复杂了,那么有没有办法不用神经网络进行这样的数字图片的自动识别呢?

答案是有的,我们可以利用kNN(k邻近算法)直接比较数字图片的像素点来进行判别。

先讲原理,比如,我有一张数字图片“2”,那么无论这张图片左偏一点或者右转一点,凡是“2”的图片上的像素点与所有其他“2”的图片的像素点的平方差应该是最小的。当然,这个假设是分类的图片足够简单,太复杂的分类kNN算不了。

我们以python小练习(087)的图片为例,我用kNN算法进行了100组数据的测试,结果如下:
  1. Test  0 : Predict X: 0 , Actural is: 0 , Result is: True
  2. --------------------------------------------------
  3. Test  1 : Predict X: 1 , Actural is: 1 , Result is: True
  4. --------------------------------------------------
  5. Test  2 : Predict X: 2 , Actural is: 2 , Result is: True
  6. --------------------------------------------------
  7. Test  3 : Predict X: 3 , Actural is: 3 , Result is: True
  8. --------------------------------------------------
  9. Test  4 : Predict X: 4 , Actural is: 4 , Result is: True
  10. --------------------------------------------------
  11. Test  5 : Predict X: 5 , Actural is: 5 , Result is: True
  12. --------------------------------------------------
  13. Test  6 : Predict X: 6 , Actural is: 6 , Result is: True
  14. --------------------------------------------------
  15. Test  7 : Predict X: 7 , Actural is: 7 , Result is: True
  16. --------------------------------------------------
  17. Test  8 : Predict X: 8 , Actural is: 8 , Result is: True
  18. --------------------------------------------------
  19. Test  9 : Predict X: 9 , Actural is: 9 , Result is: True
  20. --------------------------------------------------
  21. Test  10 : Predict X: 9 , Actural is: 0 , Result is: False
  22. --------------------------------------------------
  23. Test  11 : Predict X: 1 , Actural is: 1 , Result is: True
  24. --------------------------------------------------
  25. Test  12 : Predict X: 2 , Actural is: 2 , Result is: True
  26. --------------------------------------------------
  27. Test  13 : Predict X: 3 , Actural is: 3 , Result is: True
  28. --------------------------------------------------
  29. Test  14 : Predict X: 4 , Actural is: 4 , Result is: True
  30. --------------------------------------------------
  31. Test  15 : Predict X: 5 , Actural is: 5 , Result is: True
  32. --------------------------------------------------
  33. Test  16 : Predict X: 6 , Actural is: 6 , Result is: True
  34. --------------------------------------------------
  35. Test  17 : Predict X: 7 , Actural is: 7 , Result is: True
  36. --------------------------------------------------
  37. Test  18 : Predict X: 8 , Actural is: 8 , Result is: True
  38. --------------------------------------------------
  39. Test  19 : Predict X: 9 , Actural is: 9 , Result is: True
  40. --------------------------------------------------
  41. Test  20 : Predict X: 0 , Actural is: 0 , Result is: True
  42. --------------------------------------------------
  43. Test  21 : Predict X: 1 , Actural is: 1 , Result is: True
  44. --------------------------------------------------
  45. Test  22 : Predict X: 2 , Actural is: 2 , Result is: True
  46. --------------------------------------------------
  47. Test  23 : Predict X: 3 , Actural is: 3 , Result is: True
  48. --------------------------------------------------
  49. Test  24 : Predict X: 4 , Actural is: 4 , Result is: True
  50. --------------------------------------------------
  51. Test  25 : Predict X: 5 , Actural is: 5 , Result is: True
  52. --------------------------------------------------
  53. Test  26 : Predict X: 6 , Actural is: 6 , Result is: True
  54. --------------------------------------------------
  55. Test  27 : Predict X: 7 , Actural is: 7 , Result is: True
  56. --------------------------------------------------
  57. Test  28 : Predict X: 8 , Actural is: 8 , Result is: True
  58. --------------------------------------------------
  59. Test  29 : Predict X: 9 , Actural is: 9 , Result is: True
  60. --------------------------------------------------
  61. Test  30 : Predict X: 9 , Actural is: 0 , Result is: False
  62. --------------------------------------------------
  63. Test  31 : Predict X: 1 , Actural is: 1 , Result is: True
  64. --------------------------------------------------
  65. Test  32 : Predict X: 2 , Actural is: 2 , Result is: True
  66. --------------------------------------------------
  67. Test  33 : Predict X: 3 , Actural is: 3 , Result is: True
  68. --------------------------------------------------
  69. Test  34 : Predict X: 4 , Actural is: 4 , Result is: True
  70. --------------------------------------------------
  71. Test  35 : Predict X: 5 , Actural is: 5 , Result is: True
  72. --------------------------------------------------
  73. Test  36 : Predict X: 6 , Actural is: 6 , Result is: True
  74. --------------------------------------------------
  75. Test  37 : Predict X: 7 , Actural is: 7 , Result is: True
  76. --------------------------------------------------
  77. Test  38 : Predict X: 8 , Actural is: 8 , Result is: True
  78. --------------------------------------------------
  79. Test  39 : Predict X: 9 , Actural is: 9 , Result is: True
  80. --------------------------------------------------
  81. Test  40 : Predict X: 0 , Actural is: 0 , Result is: True
  82. --------------------------------------------------
  83. Test  41 : Predict X: 1 , Actural is: 1 , Result is: True
  84. --------------------------------------------------
  85. Test  42 : Predict X: 2 , Actural is: 2 , Result is: True
  86. --------------------------------------------------
  87. Test  43 : Predict X: 3 , Actural is: 3 , Result is: True
  88. --------------------------------------------------
  89. Test  44 : Predict X: 4 , Actural is: 4 , Result is: True
  90. --------------------------------------------------
  91. Test  45 : Predict X: 5 , Actural is: 5 , Result is: True
  92. --------------------------------------------------
  93. Test  46 : Predict X: 6 , Actural is: 6 , Result is: True
  94. --------------------------------------------------
  95. Test  47 : Predict X: 7 , Actural is: 7 , Result is: True
  96. --------------------------------------------------
  97. Test  48 : Predict X: 8 , Actural is: 8 , Result is: True
  98. --------------------------------------------------
  99. Test  49 : Predict X: 9 , Actural is: 9 , Result is: True
  100. --------------------------------------------------
  101. Test  50 : Predict X: 9 , Actural is: 0 , Result is: False
  102. --------------------------------------------------
  103. Test  51 : Predict X: 1 , Actural is: 1 , Result is: True
  104. --------------------------------------------------
  105. Test  52 : Predict X: 2 , Actural is: 2 , Result is: True
  106. --------------------------------------------------
  107. Test  53 : Predict X: 3 , Actural is: 3 , Result is: True
  108. --------------------------------------------------
  109. Test  54 : Predict X: 4 , Actural is: 4 , Result is: True
  110. --------------------------------------------------
  111. Test  55 : Predict X: 5 , Actural is: 5 , Result is: True
  112. --------------------------------------------------
  113. Test  56 : Predict X: 6 , Actural is: 6 , Result is: True
  114. --------------------------------------------------
  115. Test  57 : Predict X: 7 , Actural is: 7 , Result is: True
  116. --------------------------------------------------
  117. Test  58 : Predict X: 8 , Actural is: 8 , Result is: True
  118. --------------------------------------------------
  119. Test  59 : Predict X: 9 , Actural is: 9 , Result is: True
  120. --------------------------------------------------
  121. Test  60 : Predict X: 0 , Actural is: 0 , Result is: True
  122. --------------------------------------------------
  123. Test  61 : Predict X: 1 , Actural is: 1 , Result is: True
  124. --------------------------------------------------
  125. Test  62 : Predict X: 2 , Actural is: 2 , Result is: True
  126. --------------------------------------------------
  127. Test  63 : Predict X: 3 , Actural is: 3 , Result is: True
  128. --------------------------------------------------
  129. Test  64 : Predict X: 4 , Actural is: 4 , Result is: True
  130. --------------------------------------------------
  131. Test  65 : Predict X: 5 , Actural is: 5 , Result is: True
  132. --------------------------------------------------
  133. Test  66 : Predict X: 6 , Actural is: 6 , Result is: True
  134. --------------------------------------------------
  135. Test  67 : Predict X: 7 , Actural is: 7 , Result is: True
  136. --------------------------------------------------
  137. Test  68 : Predict X: 8 , Actural is: 8 , Result is: True
  138. --------------------------------------------------
  139. Test  69 : Predict X: 9 , Actural is: 9 , Result is: True
  140. --------------------------------------------------
  141. Test  70 : Predict X: 0 , Actural is: 0 , Result is: True
  142. --------------------------------------------------
  143. Test  71 : Predict X: 1 , Actural is: 1 , Result is: True
  144. --------------------------------------------------
  145. Test  72 : Predict X: 2 , Actural is: 2 , Result is: True
  146. --------------------------------------------------
  147. Test  73 : Predict X: 3 , Actural is: 3 , Result is: True
  148. --------------------------------------------------
  149. Test  74 : Predict X: 4 , Actural is: 4 , Result is: True
  150. --------------------------------------------------
  151. Test  75 : Predict X: 5 , Actural is: 5 , Result is: True
  152. --------------------------------------------------
  153. Test  76 : Predict X: 6 , Actural is: 6 , Result is: True
  154. --------------------------------------------------
  155. Test  77 : Predict X: 7 , Actural is: 7 , Result is: True
  156. --------------------------------------------------
  157. Test  78 : Predict X: 8 , Actural is: 8 , Result is: True
  158. --------------------------------------------------
  159. Test  79 : Predict X: 9 , Actural is: 9 , Result is: True
  160. --------------------------------------------------
  161. Test  80 : Predict X: 9 , Actural is: 0 , Result is: False
  162. --------------------------------------------------
  163. Test  81 : Predict X: 1 , Actural is: 1 , Result is: True
  164. --------------------------------------------------
  165. Test  82 : Predict X: 2 , Actural is: 2 , Result is: True
  166. --------------------------------------------------
  167. Test  83 : Predict X: 3 , Actural is: 3 , Result is: True
  168. --------------------------------------------------
  169. Test  84 : Predict X: 4 , Actural is: 4 , Result is: True
  170. --------------------------------------------------
  171. Test  85 : Predict X: 5 , Actural is: 5 , Result is: True
  172. --------------------------------------------------
  173. Test  86 : Predict X: 6 , Actural is: 6 , Result is: True
  174. --------------------------------------------------
  175. Test  87 : Predict X: 7 , Actural is: 7 , Result is: True
  176. --------------------------------------------------
  177. Test  88 : Predict X: 8 , Actural is: 8 , Result is: True
  178. --------------------------------------------------
  179. Test  89 : Predict X: 9 , Actural is: 9 , Result is: True
  180. --------------------------------------------------
  181. Test  90 : Predict X: 0 , Actural is: 0 , Result is: True
  182. --------------------------------------------------
  183. Test  91 : Predict X: 1 , Actural is: 1 , Result is: True
  184. --------------------------------------------------
  185. Test  92 : Predict X: 2 , Actural is: 2 , Result is: True
  186. --------------------------------------------------
  187. Test  93 : Predict X: 3 , Actural is: 3 , Result is: True
  188. --------------------------------------------------
  189. Test  94 : Predict X: 4 , Actural is: 4 , Result is: True
  190. --------------------------------------------------
  191. Test  95 : Predict X: 5 , Actural is: 5 , Result is: True
  192. --------------------------------------------------
  193. Test  96 : Predict X: 6 , Actural is: 6 , Result is: True
  194. --------------------------------------------------
  195. Test  97 : Predict X: 7 , Actural is: 7 , Result is: True
  196. --------------------------------------------------
  197. Test  98 : Predict X: 8 , Actural is: 8 , Result is: True
  198. --------------------------------------------------
  199. Test  99 : Predict X: 9 , Actural is: 9 , Result is: True
  200. --------------------------------------------------
复制代码

可以看到,100组数据中有4组错误,其他96组都正确,正确率达到96%。
而且错误的都是把“9”和“0”搞错了(确实“9”和“0”如果加上旋转的话,是会比较像的)。

源代码及注解如下:(源代码中,除了numpy和PIL,没有用掉任何第三方库,也不需要使用神经网络)
游客,如果您要查看本帖隐藏内容请回复

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-7 14:58:28 | 显示全部楼层
厉害啊,学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-10 19:48:14 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-21 23:28:40 | 显示全部楼层
厉害厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-6-26 10:49:08 | 显示全部楼层
u 2 nb la
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-31 21:54:42 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-17 10:35:31 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-18 12:43:44 | 显示全部楼层
学习!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-3 20:43:49 | 显示全部楼层
66666666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-19 00:57:05 | 显示全部楼层
观摩
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-29 15:51:02 | 显示全部楼层
学习学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-18 22:31:59 | 显示全部楼层
特意过来学习python, 同时学习神经网络, 最近要用到
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-10 11:16:48 | 显示全部楼层
go
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-11 16:37:58 | 显示全部楼层
回复
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-11 16:54:44 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-11 19:47:21 From FishC Mobile | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-19 09:33:38 | 显示全部楼层
谢谢分享,支持
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-13 10:22:20 | 显示全部楼层
xx
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-7-8 17:20:02 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-14 19:08:02 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-23 14:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表