我刚刚写了一个非常简单的手写数字识别。Here is 8kb archive包含以下代码+十个.PNG图像文件。它可以工作:被公认为。在
简而言之,数据库的每个数字(50x50像素=250个系数)被汇总到一个10系数向量中(通过保持10个最大的单数值,请参见Low-rank approximation with SVD)。在
然后,为了识别数字,我们最小化了与数据库中数字的距离。在
from scipy import misc
import numpy as np
import matplotlib.pyplot as plt
digits = []
for i in range(11):
M = misc.imread(str(i) + '.png', flatten=True)
U, s, V = np.linalg.svd(M, full_matrices=False)
s[10:] = 0 # keep the 10 biggest singular values only, discard others
S = np.diag(s)
M_reduced = np.dot(U, np.dot(S, V)) # reconstitution of image with 10 biggest singular values
digits.append({'original': M, 'singular': s[:10], 'reduced': M_reduced})
# each 50x50 pixels digit is summarized into a vector of 10 coefficients : the 10 biggest singular values s[:10]
# 0.png to 9.png = all the digits (for machine training)
# 10.png = the digit to be recognized
toberecognizeddigit = digits[10]
digits = digits[:10]
# we find the nearest-neighbour by minimizing the distance between singular values of toberecoginzeddigit and all the digits in database
recognizeddigit = min(digits[:10], key=lambda d: sum((d['singular']-toberecognizeddigit['singular'])**2))
plt.imshow(toberecognizeddigit['reduced'], interpolation='nearest', cmap=plt.cm.Greys_r)
plt.show()
plt.imshow(recognizeddigit['reduced'], interpolation='nearest', cmap=plt.cm.Greys_r)
plt.show()
代码是有效的(您可以在ZIP存档中运行代码),但是我们如何改进它以获得更好的结果?(我认为主要是数学技巧)。在
例如,在我的测试中,9和3有时会相互混淆。在
数字识别是一个相当困难的领域。尤其是当数字以非常不同或不清楚的方式书写时。为了解决这个问题,人们采取了很多方法,整个比赛都致力于这个主题。有关示例,请参见Kaggle's digit recognizer competition。这场比赛是基于著名的MNIST data set。在那里的论坛里,你会发现很多关于这个问题的想法和方法,但是我会给你一些快速的建议。
很多人把这个问题看作是一个分类问题。解决这些问题的可能算法包括,例如,kNN,神经网络,或梯度推进。
然而,一般来说,仅仅依靠算法是不足以得到最优分类率的。提高分数的另一个重要方面是特征提取。这个想法是计算特征,使区分不同的数字成为可能。这个数据集的一些示例特性可能包括彩色像素的数量,或者数字的宽度和高度。
虽然其他算法可能不是您所要寻找的,但是添加更多特性可能也可以提高您当前使用的算法的性能。
相关问题 更多 >
编程相关推荐