import numpy as np
m = np.matrix('[1, 2; 3, 4]')
print(m[0][0])
I expected to see 1
.
But it shows me [[1 2]]
.
How can I get 1
instead?
CodePudding user response:
As Tranbi mentioned, the np.matrix
official documentation states:
It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.
If you still wish to use np.matrix
, it supports the np.matrix.item() method, and you can get the first element by:
m.item((0, 0))
or
m.item(0)
which will yield:
1
If you do follow the recommendation of switching to np.array,
m = np.array([[1, 2], [3, 4]])
m[0][0]
would yield 1