Inverse of a matrix using numpy

PythonNumpyMatrix

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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]])

Solution 2 - Python

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. ]])

Solution 3 - Python

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.

Solution 4 - Python

What about inv?

e.g.: my_inverse_array = inv(my_array)

Solution 5 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJake ZView Question on Stackoverflow
Solution 1 - Pythonuser2357112View Answer on Stackoverflow
Solution 2 - PythondagrhaView Answer on Stackoverflow
Solution 3 - PythonEric LeschinskiView Answer on Stackoverflow
Solution 4 - Pythonuser1330052View Answer on Stackoverflow
Solution 5 - PythonPierce AquilonenView Answer on Stackoverflow