【发布时间】:2016-01-15 18:48:44
【问题描述】:
我在 python 中实现了 k-nearest-neighbours 算法,以对来自 mnist 数据库的一些随机选取的图像进行分类。但是我发现我的距离函数非常慢:针对 10k 图像的训练集分析 10 幅测试图像大约需要 2 分钟。图像的分辨率为 28x28 像素。由于我是 python 新手,我觉得这可能会更快。该函数应该计算两个相同大小的灰度图像之间的欧式距离。
def calculateDistance(image1, image2):
distance = 0
for i in range(len(image1)):
for j in range(len(image1)):
distance += math.pow((image1[i][j]-image2[i][j]),2)
distance = numpy.sqrt(distance)
return distance
【问题讨论】:
-
这个问题似乎很适合Code Review。
标签: python algorithm image-processing matrix euclidean-distance