Python: convert numpy array of signs to int and back
<p>I'm trying to convert from a numpy array of signs (i.e., a numpy array whose entries are either <code>1.</code> or <code>-1.</code>) to an integer and back through a binary representation. I have something that works, but it's not Pythonic, and I expect it'll be slow.</p>

<pre><code>def sign2int(s):
    s[s==-1.] = 0.
    bstr = ''
    for i in range(len(s)):
        bstr = bstr + str(int(s[i]))
    return int(bstr, 2)

def int2sign(i, m):
    bstr = bin(i)[2:].zfill(m)
    s = []
    for d in bstr:
        s.append(float(d))
    s = np.array(s)
    s[s==0.] = -1.
    return s
</code></pre>

<p>Then </p>

<pre><code>&gt;&gt;&gt; m = 4
&gt;&gt;&gt; s0 = np.array([1., -1., 1., 1.])
&gt;&gt;&gt; i = sign2int(s0)
&gt;&gt;&gt; print i
11
&gt;&gt;&gt; s = int2sign(i, m)
&gt;&gt;&gt; print s
[ 1. -1.  1.  1.]
</code></pre>

<p>I'm concerned about (1) the for loops in each and (2) having to build an intermediate representation as a string. </p>

<p>Ultimately, I will want something that works with a 2-d numpy array, too---e.g.,</p>

<pre><code>&gt;&gt;&gt; s = np.array([[1., -1., 1.], [1., 1., 1.]])
&gt;&gt;&gt; print sign2int(s)
[5, 7]
</code></pre>

<p>Here are some vectorized versions of your functions: </p>

<pre><code>def sign2int(s):
    return int(''.join(np.where(s == -1., 0, s).astype(int).astype(str)), 2)

def int2sign(i, m):
    tmp = np.array(list(bin(i)[2:].zfill(m)))
    return np.where(tmp == "0", "-1", tmp).astype(int)

s0 = np.array([1., -1., 1., 1.])

sign2int(s0)
# 11

int2sign(11, 5)
# array([-1,  1, -1,  1,  1])
</code></pre>

<p>To use your functions on 2-d arrays, you can use <code>map</code> function:</p>

<pre><code>s = np.array([[1., -1., 1.], [1., 1., 1.]])

map(sign2int, s)
# [5, 7]

map(lambda x: int2sign(x, 4), [5, 7])
# [array([-1,  1, -1,  1]), array([-1,  1,  1,  1])]
</code></pre>

python arrays numpy binary