I have a NumPy array such as this:
[ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1 -1 -1
1 1 1 1 1 1 -1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 -1 1 -1
-1 -1 -1 1 1 -1 -1 -1 -1 -1 1 -1 -1 1 1 1 1 1 -1 1 1 1 1 1
1 1 1 1 1 1 1 -1 -1 1 1 1 1 1 1 1 -1 1 1 1 1 1 1 1
-1 1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 -1 1
1 -1 -1 1 1 1 1 1 1]
I am trying to access the index of an elements equaling -1 in a np array. I found NumPy has an equivalent to list.index but I keep getting an attribute error: 'numpy.ndarray' object has no attribute 'where'
I saw here Is there a NumPy function to return the first index of something in an array? that I should use where, however when I try the following I get the above error:
for pred in outlier_preds:
if pred == -1:
index = where(predictions)
I also tried:
for pred in outlier_preds:
if pred == -1:
index = outlier_preds.where(pred == -1)
outlier_indecies.append(index)
And:
for pred in outlier_preds:
if pred == -1:
index = outlier_preds.where(preds)
There is obviously an attribute where() but for some reason it isn't working, I guess the way I am trying to use it?
Obviously I can get around it by converting it to a list but I would like to know how to use where correctly if possible