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

numpy.empty references previously created empty array #12003

Closed
montefer opened this issue Sep 20, 2018 · 2 comments
Closed

numpy.empty references previously created empty array #12003

montefer opened this issue Sep 20, 2018 · 2 comments

Comments

@montefer
Copy link

Reproducing code example:

import numpy as np

for x in range(5):
    print(np.empty([2,3])+x)

Expected Output

[[0.00000000e+000 4.67296746e-307 1.69121096e-306]
 [1.60219035e-306 8.90104239e-307 3.27489435e-032]]
[[1. 1. 1.]
 [1. 1. 1.]]
[[2. 2. 2.]
 [2. 2. 2.]]
[[3. 3. 3.]
 [3. 3. 3.]]
[[4. 4. 4.]
 [4. 4. 4.]]

So each time an empty array is initialized and then printed out, I think we should expect to see whatever the present value of x is to be added to each 'empty' index in the array? However, what actually occurs is this:

Actual Output

[[0.00000000e+000 4.67296746e-307 1.69121096e-306]
 [1.60219035e-306 8.90104239e-307 3.27489435e-032]]
[[1. 1. 1.]
 [1. 1. 1.]]
[[3. 3. 3.]
 [3. 3. 3.]]
[[6. 6. 6.]
 [6. 6. 6.]]
[[10. 10. 10.]
 [10. 10. 10.]]

It appears that numpy continues to reference the last created 'empty' array even though we're calling the function five different times, so the values add cumulatively.

I think we should expect it to act similarly to np.zeros in that the values aren't added cumulatively, see below for an example.

Output with np.zeros

>>>import numpy as np

>>>for x in range(5):
...    print(np.zeros([2,3])+x)
…
[[0. 0. 0.]
 [0. 0. 0.]]
[[1. 1. 1.]
 [1. 1. 1.]]
[[2. 2. 2.]
 [2. 2. 2.]]
[[3. 3. 3.]
 [3. 3. 3.]]
[[4. 4. 4.]
 [4. 4. 4.]]

Is this a bug? Or is this just the way that np.empty is supposed to act?

Numpy/Python version information:

1.15.1 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]

@pv
Copy link
Member

pv commented Sep 20, 2018

The behavior is correct, that's how the allocator works.

@pv pv closed this as completed Sep 20, 2018
@eric-wieser
Copy link
Member

empty does not mean zeros

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

3 participants