In Python we can get the index of a value in an array by using .index(). How can I do it with a NumPy array?
When I try to do
decoding.index(i)
it says that the NumPy library doesn't support this function. Is there a way to do it?
In Python we can get the index of a value in an array by using .index(). How can I do it with a NumPy array?
When I try to do
decoding.index(i)
it says that the NumPy library doesn't support this function. Is there a way to do it?
You can use the function
numpy.nonzero()
, or thenonzero()
method of an arrayOutput:
First array in output depicts the row index and second array depicts the corresponding column index.
I'm torn between these two ways of implementing an index of a NumPy array:
Both take the same number of characters, but the second method returns an
int
, instead of anparray
.You can convert a numpy array to list and get its index .
for example
i is just what you want.
Use
np.where
to get the indices where a given condition isTrue
.Examples:
For a 2D
np.ndarray
:For a 1D array:
Which works for conditions like
>=
,<=
,!=
and so forth...You can also create a subclass of
np.ndarray
with anindex()
method:Testing: