python: vectorized cumulative counting
问题 I have a numpy array and would like to count the number of occurences for each value, however, in a cumulative way in = [0, 1, 0, 1, 2, 3, 0, 0, 2, 1, 1, 3, 3, 0, ...] out = [0, 0, 1, 1, 0, 0, 2, 3, 1, 2, 3, 1, 2, 4, ...] I'm wondering if it is best to create a (sparse) matrix with ones at col = i and row = in[i] 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0 Then we could