title: Numpy: Quickly count True instances item-wise in 2D bool array
body: <p>I have a 2D boolean array, e.g.:</p>
  
  <pre><code>[[False, True, True],
  [True, False, True],
  [True, False, False],
  [True, True, True]]
  </code></pre>
  
  <p>For each row in the array, I want to count the instances of True, and create a new item-wise array of the same shape as the original array in which each entry is the sum of all previous True instances in the row. For the above example, my desired output would be:</p>
  
  <pre><code>[[0, 0, 1],
  [0, 1, 1],
  [0, 1, 1],
  [0, 1, 2]]
  </code></pre>
  
  <p>Is there a way to do this quickly in Numpy rather than checking each item in the array like so:</p>
  
  <pre><code>boolarr = np.array([[0, 1, 1], [1, 0, 1], [1, 0, 0], [1, 1, 1]], dtype=np.bool)
  
  newarr = np.zeros(boolarr.shape)
  
  for i, row in enumerate(boolarr):
      for index, item in enumerate(row):
          if item:
              newarr[i][index+1:] += 1
  </code></pre>
  
  <p>My arrays are large enough (40 x 1260) and speed is a factor as this needs to be done repetitively a great number of times.</p>
  
  <p>Thanks.</p>