title: Working Example for Mahalanobis Distance Measure
body: <p>I need to measure the distance between two n-diensional vectors. It seems that Mahalanobis Distance is a good choise here so i want to give it a try.</p>
  
  <p>My Code looks like this:</p>
  
  <pre><code>import numpy as np
  import scipy.spatial.distance.mahalanobis
  
  x = [19, 8, 0, 0, 2, 1, 0, 0, 18, 0, 1673, 9, 218]
  y = [17, 6, 0, 0, 1, 2, 0, 0, 8, 0, 984, 9, 30]
  scipy.spatial.distance.mahalanobis(x,y,np.linalg.inv(np.cov(x,y)))
  </code></pre>
  
  <p>But I get this error message:</p>
  
  <pre><code>/usr/lib/python2.7/dist-packages/scipy/spatial/distance.pyc in mahalanobis(u, v, VI)
      498     v = np.asarray(v, order='c')
      499     VI = np.asarray(VI, order='c')
  --&gt; 500     return np.sqrt(np.dot(np.dot((u-v),VI),(u-v).T).sum())
      501 
      502 def chebyshev(u, v):
  
  ValueError: matrices are not aligned
  </code></pre>
  
  <p>The Scipy Doc says, that <code>VI</code> is the inverse of the covariance matrix, and i think <code>np.cov</code> is the covariance matrix and <code>np.linalg.inv</code> is the inverse of a matrix...</p>
  
  <p>But I see what is the problem here (matrices are not aligned): The Matrix VI has the wrong dimension (2x2 and not 13x13).
  So possible solution is to do this:</p>
  
  <pre><code>VI = np.linalg.inv(np.cov(np.vstack((x,y)).T))
  </code></pre>
  
  <p>but unfortuanly the det of <code>np.cov(np.vstack((x,y)).T)</code> is 0, which means that a inverse matrix does not exsists.</p>
  
  <p>so how can i use mahalanobis distance measure when i even cant compute the covariance matrix?</p>