How to get the length of repeated numbers column wise?
<p>I am trying to get the length of repeated numbers in Python Numpy. For example, let's consider a simple ndarray</p>

<pre><code>import numpy as np

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

<p>The first column has <code>[0, 1, 0, 1]</code>, the position of <code>1</code> is <code>1</code>, now start counting from there, we get <code>ones = 2</code> and <code>zeros = 1</code>. So I have to start counting <code>ones</code> and <code>zeros</code> when <code>1</code> is encountered (starting position).</p>

<p>so the answer for <code>a</code> would be</p>

<pre><code>ones = [2, 2, 1, 1, 1, 3, 2, 2, 1, 1]
zeros = [1, 0, 2, 1, 0, 0, 1, 1, 1, 2]
</code></pre>

<p>Can any one please help me out?</p>

<p><strong>Update</strong></p>

<p>3D array:</p>

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

<p>The expected output should be</p>

<pre><code>ones = [
         [2, 3, 0, 0, 1, 3, 2, 2, 1, 0],
         [1, 3, 0, 2, 1, 1, 1, 2, 1, 1]
       ]
zeros = [
          [1, 0, 0, 0, 0, 0, 1, 1, 1, 0],
          [0, 0, 0, 0, 2, 0, 0, 0, 2, 2]
        ]
</code></pre>

<p>With focus on performance, here's one generic approach for ndarrays -</p>

<pre><code>ones_count = a.sum(-2)
zeros_count = (a.shape[-2] - ones_count - a.argmax(-2))*a.any(-2)
</code></pre>

<p>One alternative to get <code>zeros_count</code> with selections using <code>np.where</code>, would be -</p>

<pre><code>zeros_count = np.where(a.any(-2),a.shape[-2] - ones_count - a.argmax(-2),0)
</code></pre>

<p><strong>Sample runs</strong></p>

<p><code>2D</code> case :</p>

<pre><code>In [60]: a
Out[60]: 
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 0, 1, 0, 0, 1, 1, 1, 0, 1],
       [0, 1, 0, 1, 0, 1, 0, 0, 1, 0],
       [1, 1, 0, 0, 1, 1, 1, 1, 0, 0]])

In [61]: ones_count = a.sum(-2)
    ...: zeros_count = (a.shape[-2] - ones_count - a.argmax(-2))*a.any(-2)
    ...: 

In [62]: ones_count
Out[62]: array([2, 2, 1, 1, 1, 3, 2, 2, 1, 1])

In [63]: zeros_count
Out[63]: array([1, 0, 2, 1, 0, 0, 1, 1, 1, 2])
</code></pre>

<p>3D case :</p>

<pre><code>In [65]: a = np.array([
    ...:     [
    ...:         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ...:         [1, 1, 0, 0, 0, 1, 1, 1, 0, 0],
    ...:         [0, 1, 0, 0, 0, 1, 0, 0, 1, 0],
    ...:         [1, 1, 0, 0, 1, 1, 1, 1, 0, 0],
    ...:     ],
    ...:     [
    ...:         [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ...:         [0, 1, 0, 0, 1, 0, 0, 0, 1, 1],
    ...:         [0, 1, 0, 1, 0, 0, 0, 1, 0, 0],
    ...:         [1, 1, 0, 1, 0, 1, 1, 1, 0, 0],
    ...:     ]
    ...: ])

In [66]: ones_count = a.sum(-2)
    ...: zeros_count = (a.shape[-2] - ones_count - a.argmax(-2))*a.any(-2)
    ...: 

In [67]: ones_count
Out[67]: 
array([[2, 3, 0, 0, 1, 3, 2, 2, 1, 0],
       [1, 3, 0, 2, 1, 1, 1, 2, 1, 1]])

In [68]: zeros_count
Out[68]: 
array([[1, 0, 0, 0, 0, 0, 1, 1, 1, 0],
       [0, 0, 0, 0, 2, 0, 0, 0, 2, 2]])
</code></pre>

<p>and so on for higher dim arrays.</p>

python numpy