Frequency count per sub array or slice in a 3D NumPy array
<p>I am trying to get the frequency count (without the zeros) per sub-array in a numpy 3d-array. However, the scipy.stats.itemfreq tool returns the frequency count in a 2d array.</p>

<p>What I get is:</p>

<pre><code>array_3d= array([[[1, 0, 0],
    [1, 0, 0],
    [0, 2, 0]],

   [[0, 0, 0],
    [0, 0, 3],
    [3, 3, 3]],

   [[0, 0, 4],
    [0, 0, 4],
    [0, 0, 4]]])

&gt;&gt;&gt; itemfreq(array_3d)[1:,]
# outputs
array([ 1,  2],
   [ 2,  1],
   [ 3,  4],
   [ 4,  3]], dtype=int64)
</code></pre>

<p>While I would like the output:</p>

<pre><code>array([[ 1,  2, 2, 1],
   [ 3,  4],
   [ 4,  3]], dtype=object)
</code></pre>

<p>The idea is that the uneven number is always the unique value and the even number the frequency. </p>

<p>Another output could be:</p>

<pre><code>array([ 1,  2, 0],
   [ 2,  1, 0],
   [ 3,  4, 1],
   [ 4,  3, 2]], dtype=int64)
</code></pre>

<p>Where the third column represents the subset number in the 3d array.</p>

<p>I am also open to other outputs/solutions!</p>

<p>Thanks in advance!</p>