Home > Software engineering >  numpy keeps turning zeroes into very small numbers and "-2147483648"
numpy keeps turning zeroes into very small numbers and "-2147483648"

Time:11-29

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]
 [ 1.00000000e 000  6.37391724e 057  5.68896808e-235  2.73626021e 067
   6.08210460e-235  1.17578020e 077  6.66029790e-235  7.05235822e-235
   2.13106310e-308  1.00000000e 000]
 [ 1.00000000e 000  7.83852638e-235  2.13214956e-308  8.62479942e-235
   2.13323602e-308  9.41107246e-235  2.13432248e-308  1.61214828e 063
   1.35001671e-284  1.00000000e 000]
 [ 1.00000000e 000  7.20990215e-264  9.57831969e-299  5.06352214e 139
   3.18093720e 144  1.21642092e-234  1.25562635e-234  2.13866833e-308
   1.41045067e-234  1.00000000e 000]
 [ 1.00000000e 000  2.13975479e-308  1.56770528e-234  2.14084125e-308
   1.72495988e-234  2.14192771e-308  1.88221449e-234  2.14301418e-308
   2.03946910e-234  1.00000000e 000]
 [ 1.00000000e 000  2.14410064e-308  2.19672371e-234  2.14518710e-308
   2.35397832e-234  2.14627356e-308  1.61656736e 063  1.35004493e-284
   7.20998544e-264  1.00000000e 000]
 [ 1.00000000e 000  3.93674833e-241  7.20999301e-264  6.00700127e-246
   2.03709519e-231 -5.20176578e-111  9.57832021e-299  5.66452894e 075
  -2.29724802e 236  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]]

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
            0           0 -2147483648           1]
 [          1           0           0           0           0 -2147483648
            0           0           0           1]
 [          1           0           0           0           0           0
  -2147483648           0           0           1]
 [          1           0           0 -2147483648           0           0
            0           0           0           1]
 [          1           0 -2147483648           0           0           0
  -2147483648           0 -2147483648           1]
 [          1           0 -2147483648 -2147483648           0 -2147483648
            0           0 -2147483648           1]
 [          1           0           0           0           0           0
            0           0           0           1]
 [          1           1           1           1           1           1
            1           1           1           1]]

tried on normal python 3.11 and anaconda 3.9.

I googled but I couldn't find a way to fix this, so any help would be much appreciated. The post needs to have more text so that it isn't "mostly code" and it lets me post it. I would like to know if there are any good ways to solve the issue I've described. As I wrote, I tested it on two different versions of python. Unfortunately, both lead to the same issue.

CodePudding user response:

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.

CodePudding user response:

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.]])
>>> 
  • Related