Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print numpy array in binary representation mode

I have a following numpy array

a = np.array([[1,2,3,4 ,11, 12,13,14,21,22,23,24,31,32,33,34 ]], dtype=uint8)

when I print a i am getting the following output

[[ 1  2  3  4 11 12 13 14 21 22 23 24 31 32 33 34]]

How can i get the output in binary representation?

for example

[[ 00000001  00000010  00000011  00000100 ...]]
like image 268
user3543783 Avatar asked Oct 17 '25 17:10

user3543783


1 Answers

You can use numpy.binary_repr

It takes a number as input and returns its binary representation as a string. You can vectorize this function so that it can take an array as input:

np.vectorize(np.binary_repr)(a, width=8)
like image 184
Andreas K. Avatar answered Oct 19 '25 08:10

Andreas K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!