0

I have a numpy array of arrays, and I want to search through it to find an array (not a value).

values = np.array([[0.73123909, 0.73298429, 0.73472949, 0.73647469, 1.        ],
                   [0.72949389, 0.46596859, 0.39441536, 0.87260035, 1.        ],
                   [0.2600349 , 0.05235602, 0.73298429, 0.96684119, 1.        ],
                   [0.83071553, 0.37172775, 0.7452007 , 0.08202443, 1.        ],
                   [0.27923211, 0.28097731, 0.28272251, 0.28446771, 1.        ]])

item = np.array([0.73123909, 0.73298429, 0.73472949, 0.73647469, 1.        ])
index = np.where(values == item)

I expect to get a result similar to index = 0 or (array([0]),) But I get

(array([  0,   1,   2,   3,   4,]),
array([4, 4, 4, 4, 4 ]))

Similar answers target integers and won't work for float numbers.

1 Answer 1

2

Try this:

>>> np.where(np.isclose(values, item).all(1))
(array([0], dtype=int64),)

By the way, don't check equality with == if you're dealing with floats, but instead use isclose.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.