title: Vectorizing Outer Loop of euclidean distance using numpy on multi-dimensional data
body: <p>I have a 2D matrix of values. Each row is a data point.</p>
  
  <pre><code>data = np.array(
     [[2, 2, 3],
      [4, 2, 4],
      [1, 1, 4]])
  </code></pre>
  
  <p>Now if my test point is a single 1D numpy array like:</p>
  
  <pre><code>test = np.array([2,3,3])
  </code></pre>
  
  <p>I can do something simple like <code>np.sqrt(np.sum((test-data)**2,axis=1))</code> to calculate the distance of the test point relative to all three data points.</p>
  
  <p>However, if test is itself a 2D array of points to be tested, the above doesn't work and I been using something like:</p>
  
  <pre><code>test = np.array([[2,3,3],[4,1,2]])    
  for i in range(len(test)):
      print np.sqrt(np.sum((test[i]-data)**2,axis=1))
  
  &gt;&gt;&gt; [ 1.          2.44948974  2.44948974]
      [ 2.44948974  2.23606798  3.60555128]
  </code></pre>
  
  <p>In order to calculate each point in my Test set against all the points in the Data set. It seems like there should be a way to vectorize this whole operation so that I get a (2,3) matrix of corresponding distances back without the outer FOR loop</p>
  
  <p>(Note: While this particular example is about Euclidean Distance, I find myself with similar type operations where I would like to perform an operation on all elements of one matrix with the individual elements of another matrix, so I'm hoping there's a generalized way to set up problems of this nature using Numpy.)</p>