Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (12.7k points)

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.

1 Answer

0 votes
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.


    inverse = numpy.linalg.inv(x)

except numpy.linalg.LinAlgError:

    # Not invertible. Skip this one.



    # 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.

Related questions

0 votes
1 answer
asked Sep 24, 2019 in Python by Sammy (47.6k points)
+1 vote
1 answer
asked Jul 31, 2019 in Python by Eresh Kumar (45.3k points)
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)

Browse Categories