Change Array of multi integer vectors to single value for each vector in Numpy
<p>I have an array below, which I want to make each row randomly have a single 1 or all zeros, but only the current 1 values can be converted to 0.  I have a check below that was going to do this by seeing if there is a 1 in the row and if the summed value is greater or = to 0. I am hoping there is a simple approach to do this that is just escaping me at present.</p>

<pre><code>A = np.array([
        [   0   ,   1   ,   1   ,   0   ,   1   ]   ,
        [   1   ,   0   ,   0   ,   1   ,   1   ]   ,
        [   0   ,   0   ,   1   ,   0   ,   0   ]   ,
        [   0   ,   1   ,   0   ,   0   ,   1   ]   ,
        [   1   ,   0   ,   0   ,   0   ,   0   ]   ,
        [   0   ,   0   ,   1   ,   1   ,   0   ]   ,
        [   0   ,   0   ,   0   ,   0   ,   0   ]   ,
        [   1   ,   0   ,   1   ,   0   ,   0   ]   ,
        [   1   ,   0   ,   1   ,   1   ,   1   ]   ,
        [   0   ,   0   ,   1   ,   1   ,   0   ]   ,
        [   0   ,   1   ,   0   ,   1   ,   0   ]   ,
        [   0   ,   1   ,   0   ,   0   ,   1   ]   ,
        [   0   ,   0   ,   1   ,   1   ,   0   ]   ,
        [   0   ,   1   ,   1   ,   0   ,   1   ]   ,
        [   0   ,   0   ,   1   ,   0   ,   0   ]   ])

if np.any(A[0] == 1)==True and np.sum(A[0])&gt;=0:
    change row to all 0's randomly or keep one of the existing 1 values randomly.  Ideally, if it could do it to the whole array, it would be very useful, but row by row is fine.   
</code></pre>

<p>You can randomly select per row the column you want to keep:</p>

<pre><code>m, n = A.shape
J = np.random.randint(n, size=m)
</code></pre>

<p>You can use these to create a new array:</p>

<pre><code>I = np.arange(m)
B = np.zeros_like(A)
B[I,J] = A[I,J]
</code></pre>

<p>Or if you want to modify <code>A</code>, e.g. use bit shifting:</p>

<pre><code>I = np.arange(m)
A[I,J] &lt;&lt;= 1
A &gt;&gt;= 1
</code></pre>

python numpy