2

This question might be pretty simple to answer, but I had problems finding obvious answer in the numpy documentation.

What is the dtype to select when initializing a binary numpy array as follows:

array = np.zeros(8, dtype=np.int)
5
  • 3
    Can you explain how this is not "binary enough" for you? Commented Nov 26, 2018 at 14:33
  • 1
    Were you looking for .packbits ? docs.scipy.org/doc/numpy-1.15.0/reference/generated/… Commented Nov 26, 2018 at 14:34
  • 2
    @mkrieger1 I assume that the integer declaration takes more memory or am I wrong with that? Commented Nov 26, 2018 at 14:36
  • More memory compared to what? Commented Nov 26, 2018 at 14:43
  • @mkrieger1: I just checked and sys.getsizeof(array) returns 128 bytes for dtype=np.int vs. 104 for dtype=np.bool or np.dtype('b'). Commented Nov 26, 2018 at 14:50

1 Answer 1

8

You can use np.bool:

array = np.zeros(8, dtype=np.bool)

output:

array([False, False, False, False, False, False, False, False])
Sign up to request clarification or add additional context in comments.

1 Comment

np.bool is deprecated, use array = np.zeros(8, dtype=bool)

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.