title: Optimize performance for calculation of euclidean distance between two images
body: <p>I implemented the k-nearest-neighbours algorithm in python to classify some randomly picked images from the <a href="http://yann.lecun.com/exdb/mnist/" rel="nofollow">mnist</a> database. However I found my distance function to be quite slow: An analisys of 10 test images against the training set of 10k images takes about 2mins. The images have a resolution of 28x28 pixels. Since I'm new to python I got the feeling this could be faster. The function is supposed to calculate the euclidean distance between two same-sized grayscale images.</p>
  
  <pre><code>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></pre>