rougier/numpy-100

The solution to 65 doesn't make sense

maxim5 opened this issue · 3 comments

The task is as follows: How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? To me this means that F should contain some elements of X, in the order defined by the indices vector.

The proposed solution looks like this:

X = [1,2,3,4,5,6]
I = [1,3,9,3,4,1]
F = np.bincount(I,X)
print(F)

Which results in: [0. 7. 0. 6. 5. 0. 0. 0. 0. 3.]. X doesn't contain 0 or 7. Why is this a solution?

I think the question is ill-posed but the result is the expected one. The idea is that instead of directly accumulating values in I, there is an indirection. For example, at index 1 in the results, instead of 2 (because there are two 1 in I), we have X[0] + X[5] = 7 where 0 and 5 corresponds to the indices of value 1 in I.

So the question needs to be rephrased, if you have a suggestion...

@rougier I see, thanks. "Accumulate" means sum up, not collect. And the task directly corresponds to weights in numpy.bincount. Here's how they describe it:

If weights is specified the input array is weighted by it, i.e. if a value n is found at position i, out[n] += weight[i] instead of out[n] += 1.

I think you can mention F[n] += X[i] too to make it more specific.

Even with this mention, I'm not sure it clarifies the question. I wrote the original question but I had some difficulties in finding what I meant 😞. Would you have some better formulation?