84

I'd like to use numpy to calculate the inverse. But I'm getting an error:

'numpy.ndarry' object has no attribute I

To calculate inverse of a matrix in numpy, say matrix M, it should be simply: print M.I

Here's the code:

x = numpy.empty((3,3), dtype=int)
for comb in combinations_with_replacement(range(10), 9):
   x.flat[:] = comb
   print x.I

I'm presuming, this error occurs because x is now flat, thus 'I' command is not compatible. Is there a work around for this?

My goal is to print the INVERSE MATRIX of every possible numerical matrix combination.

1
  • 1
    commented on the other answer also, but you have to defined x as a matrix np.matrix(x) so that the .I method is available.
    – M4rtini
    Feb 7, 2014 at 22:32

5 Answers 5

109

The I attribute only exists on matrix objects, not ndarrays. You can use numpy.linalg.inv to invert arrays:

inverse = numpy.linalg.inv(x)

Note that the way you're generating matrices, not all of them will be invertible. You will either need to change the way you're generating matrices, or skip the ones that aren't invertible.

try:
    inverse = numpy.linalg.inv(x)
except numpy.linalg.LinAlgError:
    # Not invertible. Skip this one.
    pass
else:
    # continue with what you were doing

Also, if you want to go through all 3x3 matrices with elements drawn from [0, 10), you want the following:

for comb in itertools.product(range(10), repeat=9):

rather than combinations_with_replacement, or you'll skip matrices like

numpy.array([[0, 1, 0],
             [0, 0, 0],
             [0, 0, 0]])
11
  • 'Module' object has no attribute inv ... =/
    – Jake Z
    Feb 7, 2014 at 22:33
  • Yeah I tried that, I get the 'singular matrix' error. O_O
    – Jake Z
    Feb 7, 2014 at 22:36
  • 3
    @JakeZ: That's because you're trying to invert uninvertible matrices. For example, one of the matrices you're generating is the 0 matrix. Feb 7, 2014 at 22:37
  • Amazing! I completely forgot to check for singular matrixes -_-' Haha, thank you. worked like a charm.
    – Jake Z
    Feb 7, 2014 at 22:39
  • 1
    @anu: That's a linear algebra issue, not a programming issue. As a matter of linear algebra, your first matrix is invertible and your other two are not. There is no reason to expect all square matrices to have an inverse. Dec 12, 2018 at 21:59
24

Another way to do this is to use the numpy matrix class (rather than a numpy array) and the I attribute. For example:

>>> m = np.matrix([[2,3],[4,5]])
>>> m.I
matrix([[-2.5,  1.5],
       [ 2. , -1. ]])
3
  • I'd rather this method since it's more straight forward. But both of them work exactly the same.
    – Parsa
    Nov 18, 2020 at 22:06
  • 1
    Though convenient, the use of np.matrix is discouraged officially since it leads to ambiguity for np.array users: scipy.linalg Jan 7, 2021 at 15:18
  • 1
    It's also being deprecated: numpy.org/devdocs/reference/generated/… Oct 12, 2021 at 23:28
14

Inverse of a matrix using python and numpy:

>>> import numpy as np
>>> b = np.array([[2,3],[4,5]])
>>> np.linalg.inv(b)
array([[-2.5,  1.5],
       [ 2. , -1. ]])

Not all matrices can be inverted. For example singular matrices are not Invertable:

>>> import numpy as np
>>> b = np.array([[2,3],[4,6]])
>>> np.linalg.inv(b)

LinAlgError: Singular matrix

Solution to singular matrix problem:

try-catch the Singular Matrix exception and keep going until you find a transform that meets your prior criteria AND is also invertable.

3

What about inv?

e.g.: my_inverse_array = inv(my_array)

2
  • I tried but i get the 'Singular Matrix' error...most likely because its flattened.... I wonder if theres a way to reshape it back to its original state and then inverse it?
    – Jake Z
    Feb 7, 2014 at 22:31
  • It might be easier to stash its original state and then refer back to it, as with an object that contains the current state and an attribute that says what it was originally. numpy.linalg.lstsq will attempt to give you a least-squares solution, but I don't know of anything especially clean. Feb 7, 2014 at 22:38
2

IDK if anyone already mentioned this but I want to point out that matrix_object. I and np.linalg.inv(matrix_object) don't give a true inverse. This has given me a lot of grief. It's true that for a matrix object m, np.dot(m, m.I) = an identity matrix, but np.dot(m.I, m) =/= I. Same goes for np.linalg.inv(I).

Be careful with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.