title: Why does numpy say this matrix is singular *sometimes*?
body: <p>Part of my code inverts a matrix (really an ndarray) using numpy.linalg.inv. However, this frequently errors out as follows:</p>
  
  <pre><code>numpy.linalg.linalg.LinAlgError: Singular matrix
  </code></pre>
  
  <p>That would be fine if the matrix was actually singular. But that doesn't seem to be the case.</p>
  
  <p>For example, I'm printing the matrix before trying to invert it. So right before the error it prints this:</p>
  
  <pre><code>[[ 0.76400334  0.22660491]
  [ 0.22660491  0.06721147]]
  </code></pre>
  
  <p>... and then returns the above singularity error when it tries to invert that matrix. But from what I can tell this matrix <em>is</em> invertible. Numpy even seems to agree when asked later.</p>
  
  <pre><code>&gt;&gt;&gt; numpy.linalg.inv([[0.76400334, 0.22660491], [0.22660491,    0.06721147]])
  array([[  2.88436275e+07,  -9.72469076e+07],
     [ -9.72469076e+07,   3.27870046e+08]])
  </code></pre>
  
  <p>Here's the exact code snippet:</p>
  
  <pre><code>print np.dot(np.transpose(X), X)
  print np.linalg.inv(np.dot(np.transpose(X),X))
  </code></pre>
  
  <p>The first line prints the matrix above; the second line fails.</p>
  
  <p>So what distinguishes the two actions above? Why does the stand-alone code work even though it errors out in my script?</p>
  
  <p>EDIT: Per Colonel Beauvel's request, if I do</p>
  
  <pre><code>try:
      print np.dot(np.transpose(X), X)
      z = np.linalg.inv(np.dot(np.transpose(X), X))
  except:
      z = "whoops"
  print z
  </code></pre>
  
  <p>it outputs</p>
  
  <pre><code>[[ 0.01328185  0.1092696 ]
  [ 0.1092696   0.89895982]]
  whoops
  </code></pre>
  
  <p>but trying this on its own I get</p>
  
  <pre><code>&gt;&gt;&gt; numpy.linalg.inv([[0.01328185, 0.1092696], [0.1092696, 0.89895982]])
  array([[  2.24677775e+08,  -2.73098420e+07],
     [ -2.73098420e+07,   3.31954382e+06]])
  </code></pre>