1

I have a numpy array (a list of 2 elements lists) a give below and I have a list of 2 elements [30.94, 0.] that I would like to look for.

When I did the following I don't get the desired result. Why?

import numpy as np
a = np.array([[  5.73,   0.  ],
              [ 57.73,  10.  ],
              [ 57.73,  20.  ],
              [ 30.94,   0.  ],
              [ 30.94,  10.  ],
              [ 30.94,  20.  ],
              [  4.14,   0.  ],
              [  4.14,  10.  ]])

np.where(a==np.array([30.94, 0.]))

But I get

(array([0, 3, 3, 4, 5, 6]), array([1, 0, 1, 0, 0, 1]))

which is not true.

4
  • Question back at you - What do you expect with a==np.array([30.94, 0.]? Commented Dec 22, 2016 at 20:05
  • It should be [3, 3] for the indices of the rows and [0, 1] for the indices of the columns or something like this. Commented Dec 22, 2016 at 20:07
  • Nah I mean without np.where, just : a==np.array([30.94, 0.]. My idea is if you look at its output, you would have a better picture, hopefully. Commented Dec 22, 2016 at 20:08
  • Maybe [0, 0, 0, 1, 0, 0, 0, 0] ? Commented Dec 22, 2016 at 20:09

2 Answers 2

4

As Divakar hinted, a == np.array([30.94, 0.]) is not what you expect. The array is broadcast, and the comparison is done elementwise. Here is the result:

array([[False,  True],
       [False, False],
       [False, False],
       [ True,  True],
       [ True, False],
       [ True, False],
       [False,  True],
       [False, False]], dtype=bool)

However, we can get what we want with np.all:

>>> np.all(a==np.array([30.94, 0.]), axis=-1)
array([False, False, False,  True, False, False, False, False], dtype=bool)
>>> np.where(_)
(array([3]),)

So you can see that row 3 matches, as expected. Note that the usual caveats to using == with floating-point numbers will apply here.

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

3 Comments

To account for floating pt numbers, we can use np.isclose(), which allows broadcasting.
@kmario23 "broadcast" is a valid past participle for "broadcast".
@DietrichEpp I just realized that but it felt more natural to have it in ed form. Thanks for pointing it out, btw!
1

Yet another solution but please be aware that this will be little bit slower than Dietrich's solution, particularly for large arrays.

In [1]: cond = np.array([30.94, 0.])
In [2]: arr = np.array([[  5.73,   0.  ],
                       [ 57.73,  10.  ],
                       [ 57.73,  20.  ],
                       [ 30.94,   0.  ],
                       [ 30.94,  10.  ],
                       [ 30.94,  20.  ],
                       [  4.14,   0.  ],
                       [  4.14,  10.  ]])

In [3]: [idx for idx, el in enumerate(arr) if np.array_equal(el, cond)]
Out[3]: [3]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.