Why is numpy's default value for this ndarray equal to a relatively large number and not ~0.0?

Viewed 40

In my PyCharm IDE if I open up a Python Console (Interpreter) and attempt to instantiate a numpy.ndarray with initial shape [1] I get the following result:

Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 8.4.0
Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
import numpy as np
np.ndarray([1])
Out[3]: array([0.0078125])

This default value seems relatively (>>) larger than I expected. I would expect something close to 0.0 or essentially 0.0 (i.e. 1.234e+178). Given it's the size of the value it does not seem to be some sort of rounding error.

Checking the documentation it looks like users are not really intended to make an instance of ndarray directly, but rather rely on array. Though, the documentation even provides an example of instantiating a (2, 2) shaped array.


Relatedly interesting, it seems that the default value seems to change "suddenly" after instantiating different sized ndarray. If I keep playing around in the same terminal from above I get the following behavior

Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
IPython 8.4.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 8.4.0
Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
import numpy as np
np.ndarray([1])
Out[3]: array([0.0078125])
np.ndarray([1, 2])
Out[4]: array([[1.02724393e-188, 6.85721358e+215]])
np.ndarray(1)
Out[5]: array([6.85721358e+215])
np.ndarray([1])
Out[6]: array([6.85721358e+215])

Various online Python interactive terminals produce the same behavior.


Why is the default value for numpy.ndarray([1]) equal to such a large value?

Is it that the default value of the buffer is somehow equal to this value "by default" for a size (1) array?..

Note: 1/128 = 0.0078125

1 Answers

numpy.ndarray([1]) is interpreted as an empty array with a shape of (1,) (ie. 1D array of size 1). There is no default value. This means neither Numpy nor the operating system initialize the array. As a result the array can contains non-zero values. In practice, it is often 0 because most operating systems zeroize memory areas for security reasons (you generally do not want data from another process to leak like passwords from your browser). Note however that the allocator of CPython is free to recycle previously allocated memory now freed. Some values are close to zeros because they are small integers that are interpreted as small floating-point numbers (most integers tends to be small in programs). When exponent bits are set, the resulting floating-point value can be big like in your example. Relying on this value is not a good idea as it results in an undefined behaviour (the value can be a silent NaN or even a signalling NaN possibly unhandled interruptions). Use np.zeros instead.

Related