mirror of
https://github.com/apachecn/ailearning.git
synced 2026-11-03 22:15:57 +08:00
修改2.k-近邻算法.md文件,添加项目实战2:手写数字识别系统
This commit is contained in:
@@ -120,7 +120,7 @@ ax.scatter(datingDataMat[:, 1], datingDataMat[:, 2])
|
||||
plt.show()
|
||||
```
|
||||
|
||||
[完整代码地址](https://github.com/apachecn/MachineLearning/blob/master/src/python/10.kmeans/kMeans.py): <https://github.com/apachecn/MachineLearning/blob/master/src/python/10.kmeans/kMeans.py>
|
||||
[完整代码地址](https://github.com/apachecn/MachineLearning/blob/master/src/python/2.KNN/kNN.py): <https://github.com/apachecn/MachineLearning/blob/master/src/python/2.KNN/kNN.py>
|
||||
|
||||
> 优化点
|
||||
|
||||
@@ -128,6 +128,43 @@ plt.show()
|
||||
|
||||
### 项目实战2: 手写数字识别系统
|
||||
|
||||
> 概述
|
||||
|
||||
构造一个能识别数字 0 到 9 的基于 KNN 分类器的手写识别系统。
|
||||
|
||||
需要识别的数字是存储在文本文件中的具有相同的色彩和大小:宽高是 32 像素 * 32 像素的黑白图像。
|
||||
|
||||
> 使用 KNN 的手写识别系统
|
||||
|
||||
```
|
||||
收集数据:提供文本文件。
|
||||
准备数据:编写函数 img2vector(), 将图像格式转换为分类器使用的向量格式
|
||||
分析数据:在 Python 命令提示符中检查数据,确保它符合要求。
|
||||
训练算法:此步骤不适用于 KNN
|
||||
测试算法:编写函数使用提供的部分数据集作为测试样本,测试样本与非测试样本的
|
||||
区别在于测试样本是已经完成分类的数据,如果预测分类与实际类别不同,
|
||||
则标记为一个错误。
|
||||
使用算法:本例没有完成此步骤,若你感兴趣可以构建完整的应用程序,从图像中提取
|
||||
数字,并完成数字识别,美国的邮件分拣系统就是一个实际运行的类似系统。
|
||||
```
|
||||
|
||||

|
||||
|
||||
* 将图像转换为向量
|
||||
|
||||
```Python
|
||||
def img2vector(filename):
|
||||
returnVect = zeros((1,1024))
|
||||
fr = open(filename)
|
||||
for i in range(32):
|
||||
lineStr = fr.readLine()
|
||||
for j in range(32):
|
||||
returnVect[0,32*i+j] = int(lineStr[j])
|
||||
return returnVect
|
||||
```
|
||||
|
||||
[完整代码地址](https://github.com/apachecn/MachineLearning/blob/master/src/python/2.KNN/kNN.py): <https://github.com/apachecn/MachineLearning/blob/master/src/python/2.KNN/kNN.py>
|
||||
|
||||
* * *
|
||||
|
||||
* **作者:[羊三](http://www.apache.wiki/display/~xuxin) [小瑶](http://www.apache.wiki/display/~chenyao)**
|
||||
|
||||
BIN
images/2.KNN/knn_2_handWriting.png
Normal file
BIN
images/2.KNN/knn_2_handWriting.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 312 KiB |
Reference in New Issue
Block a user