I have seen this same topic some other place but no real answer to my question. I have a numpy array and I need to find the index of a number.
a=np.argsort(cosine_similarity(tfidf_matrix[11:12], tfidf_matrix)) #numbers are from 0 to 11
b=np.equal(a,10)
# b values are [[False False False False False False False False True False False False]]
How do I get it to return index 8? (The index for the true value in the array)
You are looking for numpy.where
b = np.where(a==10)
Here b
will be an array that contains the indices of the items that matched your condition. You can select the first element (b[0]
) if you are interested only in the first occurrence of the item.
The documentation on numpy.equal says that:
Return (x1 == x2) element-wise.
And that is exactly what you received, an array that contains an element wise comparison of the array a
and the value 10
.