Reputation: 35
I am searching for a dynamic way to perform a numpy convert (integer to binary).
I tried to use the 'astype' way but it didn't work out.
I got a 3-dim matrix like that one:
x = np.array([[[1, 2, 44],[1,5,3]],[[7, 88, 12],[1,15,60]]])
I would like to convert each value of x to its binary form (max size of 8 bits) for example, if x includes 6 it will be converted to '00000110'.
This is a reference for how to convert integer to binary as I need:
Converting integer to binary in python
Upvotes: 1
Views: 1022
Reputation: 972
Is this what you are looking for?
vfunc = np.vectorize(lambda i: '{0:08b}'.format(i))
x_bin = vfunc(x)
Output of print(x_bin)
:
[[['00000001' '00000010' '00101100']
['00000001' '00000101' '00000011']]
[['00000111' '01011000' '00001100']
['00000001' '00001111' '00111100']]]
Upvotes: 2