numpy keeps turning zeroes into very small numbers and "-2147483648"
Question:
I have this code
import numpy
a=numpy.pad(numpy.empty([8,8]), 1, constant_values=1)
print(a)
50% of the times I execute it it prints a normal array, 50% of times it prints this
[[ 1.00000000e+000 1.00000000e+000 1.00000000e+000 1.00000000e+000
1.00000000e+000 1.00000000e+000 1.00000000e+000 1.00000000e+000
1.00000000e+000 1.00000000e+000]
[ 1.00000000e+000 3.25639960e-265 2.03709399e-231 -7.49281680e-111
9.57832017e-299 8.17611616e-093 9.57832017e-299 1.31887592e+066
-2.29724802e+236 1.00000000e+000]
[ 1.00000000e+000 5.11889256e-014 -2.29724802e+236 2.19853714e-004
-2.29724802e+236 -9.20964279e+232 2.37057719e+043 1.48921177e+048
5.29583156e-235 1.00000000e+000]
...
what is worse, when i do .astype(int) it keeps doing this
[[ 1 1 1 1 1 1
1 1 1 1]
[ 1 0 0 0 -2147483648 0
-2147483648 0 0 1]
[ 1 0 0 -2147483648 0 0
...
I tested it on two different versions of python – normal python 3.11 and anaconda 3.9. Unfortunately, both lead to the same issue.
Answers:
You are using numpy.empty
which is an
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None.
See documentation.
Use either numpy.zeros
or numpy.ones
to start with a proper initialized array.
The problem lies in the fact that an empty array is initialized with np.empty
. I am not an expert on computers and how they work, but what I do know is that when an empty array is initialized, a block of memory is allocated where the values of the new array are to be saved. This block of memory can contain values previously initialized. Basically there is still some 1’s and zeros in the memory that you are now printing.
When you change values in the np.pad
, for the first time, the old memory gets overwritten.
What I think you are trying to do is np.zeros()
.
comparison:
>>> import numpy
>>> a = numpy.empty([2,2])
>>> a
array([[8.88913424e-317, 0.00000000e+000],
[4.01601648e-212, 1.10215522e-317]])
>>> b = numpy.zeros([2,2])
>>> b
array([[0., 0.],
[0., 0.]])
>>>
I have this code
import numpy
a=numpy.pad(numpy.empty([8,8]), 1, constant_values=1)
print(a)
50% of the times I execute it it prints a normal array, 50% of times it prints this
[[ 1.00000000e+000 1.00000000e+000 1.00000000e+000 1.00000000e+000
1.00000000e+000 1.00000000e+000 1.00000000e+000 1.00000000e+000
1.00000000e+000 1.00000000e+000]
[ 1.00000000e+000 3.25639960e-265 2.03709399e-231 -7.49281680e-111
9.57832017e-299 8.17611616e-093 9.57832017e-299 1.31887592e+066
-2.29724802e+236 1.00000000e+000]
[ 1.00000000e+000 5.11889256e-014 -2.29724802e+236 2.19853714e-004
-2.29724802e+236 -9.20964279e+232 2.37057719e+043 1.48921177e+048
5.29583156e-235 1.00000000e+000]
...
what is worse, when i do .astype(int) it keeps doing this
[[ 1 1 1 1 1 1
1 1 1 1]
[ 1 0 0 0 -2147483648 0
-2147483648 0 0 1]
[ 1 0 0 -2147483648 0 0
...
I tested it on two different versions of python – normal python 3.11 and anaconda 3.9. Unfortunately, both lead to the same issue.
You are using numpy.empty
which is an
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None.
See documentation.
Use either numpy.zeros
or numpy.ones
to start with a proper initialized array.
The problem lies in the fact that an empty array is initialized with np.empty
. I am not an expert on computers and how they work, but what I do know is that when an empty array is initialized, a block of memory is allocated where the values of the new array are to be saved. This block of memory can contain values previously initialized. Basically there is still some 1’s and zeros in the memory that you are now printing.
When you change values in the np.pad
, for the first time, the old memory gets overwritten.
What I think you are trying to do is np.zeros()
.
comparison:
>>> import numpy
>>> a = numpy.empty([2,2])
>>> a
array([[8.88913424e-317, 0.00000000e+000],
[4.01601648e-212, 1.10215522e-317]])
>>> b = numpy.zeros([2,2])
>>> b
array([[0., 0.],
[0., 0.]])
>>>