title: NumPy: convert decimals to fractions
body: <p>I compute the reverse of matrix <strong>A</strong>, for instance,</p>
  
  <pre><code>import numpy as np
  
  A = np.diag([1, 2, 3])
  A_inv = np.linalg.pinv(A)
  print(A_inv)
  </code></pre>
  
  <p>I got, </p>
  
  <pre><code>[[ 1.          0.          0.        ]
   [ 0.          0.5         0.        ]
   [ 0.          0.          0.33333333]]
  </code></pre>
  
  <p>But, I want this,</p>
  
  <pre><code>[[ 1.          0.          0. ]
   [ 0.          1/2         0. ]
   [ 0.          0.          1/3]]
  </code></pre>
  
  <p>I tried <code>np.set_printoptions</code>,</p>
  
  <pre><code>import fractions
  np.set_printoptions(formatter={'all':lambda x: str(fractions.Fraction(x))})
  print(A_inv)
  </code></pre>
  
  <p>but I got this,</p>
  
  <pre><code>[[1 0 0]
   [0 1/2 0]
   [0 0 6004799503160661/18014398509481984]]
  </code></pre>
  
  <p>How do I convert decimals to fractions in NumPy? </p>