3

I have a numpy array 'A' of size 571x24 and I am trying to find the index of zeros in it so I do:

>>>A.shape
(571L, 24L)

import numpy as np
z1 = np.where(A==0)

z1 is a tuple with following size:

>>> len(z1)
2
>>> len(z1[0])
29
>>> len(z1[1])
29

I was hoping to create a z1 of same size as A. How do I achieve that?

Edit: I want to create array z1 of booleans for presence of zero in A such that:

>>>z1.shape
    (571L, 24L)
3
  • "I was hoping to create a z1 of same size as A." - what would you expect that z1 to mean? You seem to be looking for a data representation other than what np.where provides. What data representation are you looking for? Commented Jun 17, 2016 at 16:28
  • If I'm understanding this correctly, you want to find the indices of where zeros occur in the array? Commented Jun 17, 2016 at 16:29
  • 2
    isn't that just z1 = A==0 ? Commented Jun 17, 2016 at 16:36

2 Answers 2

3

You can just check this with the equality operator in python with numpy. Example:

>>> A = np.array([[0,2,2,1],[2,0,0,3]])
>>> A == 0
array([[ True, False, False, False],
       [False,  True,  True, False]], dtype=bool)

np.where() does something else, see documentation. Although, it is possible to achieve this with np.where() using broadcasting. See documentation.

>>> np.where(A == 0, True, False)
array([[ True, False, False, False],
       [False,  True,  True, False]], dtype=bool)
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

import numpy as np
myarray = np.array([[0,3,4,5],[9,4,0,4],[1,2,3,4]])
ix = np.in1d(myarray.ravel(), 0).reshape(myarray.shape)

Output of ix:

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

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.