numpy.packbits doesn't accept a bool array
See original GitHub issueExample of problem
data = numpy.array([True])
byte_values = numpy.packbits(data)
This results with an exception – TypeError: Expected an input array of integer data type. This seems to go against the documentation of the function, which says it expects a ‘binary-valued array’.
To make this work, you need to first convert the data type of the array to int, which seems redundant. eg.
byte_values = numpy.packbits(numpy.array(data, dtype=int))
A better use case of where I encountered the problem.
data = numpy.random.sample(1024)
truth_values = data >= threshold
bits = numpy.packbits(truth_values)
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
numpy boolean array with 1 bit entries - Stack Overflow
Is there a way in numpy to create an array of booleans that uses just 1 bit for each entry? The standard np....
Read more >numpy.packbits — NumPy v1.24 Manual
Packs the elements of a binary-valued array into bits in a uint8 array. ... An array of integers or booleans whose elements should...
Read more >The Best Technical Questions And Answers - Tutorialspoint
To pack the elements of a binary-valued array into bits in a uint8 array, use the numpy.packbits() method in Python Numpy. The result...
Read more >jax.numpy package - JAX documentation - Read the Docs
Notably, since JAX arrays are immutable, NumPy APIs that mutate arrays in-place cannot be ... Returns a bool array, where True if input...
Read more >One bit data type in NumPy? : r/Python - Reddit
Right now I'm using NumPy arrays but they only allow bits to be stored ... It does not specifically pack bits, but it...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@LuisBL There is no need to cast the input array to the native
int
dtype - you can avoid generating a copy simply by viewing it asnp.uint8
, which also uses a single byte per element, e.g.:That feature was added in #5319, it does work in current master, so closing this.