Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The solution to 65 doesn't make sense #61

Open
maxim5 opened this issue Mar 21, 2018 · 3 comments
Open

The solution to 65 doesn't make sense #61

maxim5 opened this issue Mar 21, 2018 · 3 comments

Comments

@maxim5
Copy link

maxim5 commented Mar 21, 2018

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?

@rougier
Copy link
Owner

rougier commented Mar 22, 2018

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...

@maxim5
Copy link
Author

maxim5 commented Mar 22, 2018

@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.

@rougier
Copy link
Owner

rougier commented Mar 22, 2018

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?

ToheartZhang added a commit to ToheartZhang/numpy-100 that referenced this issue Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants