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

A new tremandous BUG in Advanced Indexing? #8866

Closed
Levstyle opened this issue Mar 30, 2017 · 3 comments
Closed

A new tremandous BUG in Advanced Indexing? #8866

Levstyle opened this issue Mar 30, 2017 · 3 comments

Comments

@Levstyle
Copy link

This is a BUG

train = np.zeros([26,26])
train2 = np.zeros([26,26])
tmp = [1,2,3,4,5,6,1,2,3,4,5,6]
train[tmp[0:-1], tmp[1:]] += 1
for i in range(len(tmp)-1):
    train2[tmp[i], tmp[i+1]] += 1
print(np.sum(train - train2))

the output is -5
I find that train is not equal to train2 when there are some duplicate tuples in tmp. Is there something wrong in numpy slicing.

@RogerMonkey
Copy link

The same to you.

@MSeifert04
Copy link
Contributor

MSeifert04 commented Mar 30, 2017

You know that you're trying to write to the "same position" twice, for example [1, 2]. Unfortunatly (or fortunatly, depending on the point of view) that doesn't work.

But you can use np.add.at which according to the docs "For addition ufunc, this method is equivalent to a[indices] += b, except that results are accumulated for elements that are indexed more than once.":

train = np.zeros([26,26])
np.add.at(train, [tmp[:-1], tmp[1:]], 1)

@Levstyle
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants