Sergey Sosnin - 2 months ago 11

Python Question

Suppose, I have a

`numpy`

`n`

`(n,m)`

`m`

`log2(maxnumber)`

`x = numpy.array([32,5,67])`

Because max number I have is

`67`

`numpy.ceil(numpy.log2(67)) == 7`

`(3,7)`

`array([[1, 0, 0, 0, 0, 1, 1],`

[0, 0, 0, 0, 1, 0, 1],

[0, 1, 0, 0, 0, 0, 0]])

The problem rises because I have no quick way to move binary notation from

function

`numpy.binary_repr`

`brepr = numpy.binary_repr(x[i],width=7)`

j = 0

for bin in brepr:

X[i][j] = bin

j += 1

It's very timecost and stupid way, how to make it efficient?

Answer

Here us one way using `np.unpackbits`

and broadcasting:

```
>>> max_size = np.ceil(np.log2(x.max())).astype(int)
>>> np.unpackbits(x[:,None].astype(np.uint8), axis=1)[:,-max_size:]
array([[0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 1, 1]], dtype=uint8)
```