2 views
in Python

I would like to utilize the numpy package to calculate  inverse. But, I'm getting an error:

'numpy.ndarry' object has no attribute

To figure inverse of a matrix in NumPy, say matrix  M, it ought to be just: print M.I

Look at 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 will probably print the INVERSE MATRIX of each conceivable mathematical matrix combination.

by (26.4k points)

The I quality just exists on-matrix objects, not ndarrays. You can utilize numpy.linalg.inv to transform(invert) arrays:

inverse = numpy.linalg.inv(x)

Note that in the manner in which you're creating matrices, not every one of them will be invertible. You will either have to change the manner in which you're producing frameworks (matrices) or skirt 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

Additionally, in the event that you need to go through all 3x3 matrices with components drawn from [0, 10), you need the accompanying:

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

instead of combinations_with_replacement, or you'll skip grids like

numpy.array([[0, 1, 0],

[0, 0, 0],

[0, 0, 0]])

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.