擅长:python、mysql、java
<p>以下是一些函数的矢量化版本:</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>要在二维数组上使用函数,可以使用<code>map</code>函数:</p>
^{pr2}$