numpy binary notation quick generation
<p>Suppose, I have a <code>numpy</code> vector with <code>n</code> elements, so I'd like to encode numbers in this vector as a binary notation, so resulting shape will be <code>(n,m)</code> where <code>m</code>  is <code>log2(maxnumber)</code> for example:</p>

<pre><code>x = numpy.array([32,5,67])
</code></pre>

<p>Because max number I have is <code>67</code>, I need <code>numpy.ceil(numpy.log2(67)) == 7</code> bits to encode this vector, so shape of the result will be <code>(3,7)</code></p>

<pre><code>array([[1, 0, 0, 0, 0, 1, 1],
       [0, 0, 0, 0, 1, 0, 1],
       [0, 1, 0, 0, 0, 0, 0]])
</code></pre>

<p>The problem rises because I have no quick way to move binary notation from<br>
function <code>numpy.binary_repr</code> to numpy array. Now I have to iterate over result, and put each bit severally: </p>

<pre><code>brepr = numpy.binary_repr(x[i],width=7)
j = 0
for bin in brepr:
   X[i][j] = bin
   j += 1
</code></pre>

<p>It's very timecost and stupid way, how to make it efficient?</p>

<p>You can use numpy byte string.</p>

<p>For the case you have in hand:</p>

<pre><code>res = numpy.array(len(x),dtype='S7')
for i in range(len(x)):
    res[i] = numpy.binary_repr(x[i])
</code></pre>

<p>Or more compactly</p>

<pre><code>res = numpy.array([numpy.binary_repr(val) for val in x])
</code></pre>

python numpy