Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.empty giving nonempty array

Tags:

python

numpy

When I create an empty numpy array using foo = np.empty(1) the resulting array contains a float64:

>>> foo = np.empty(1)
>>> foo
array([ 0.])
>>> type(foo[0])
<type 'numpy.float64'>

Why doesn't it just return array([])?

like image 987
G Warner Avatar asked Aug 25 '26 11:08

G Warner


1 Answers

You didn't read the documentation, which says:

Return a new array of given shape and type, without initializing entries.

empty has nothing to do with creating an array that is "empty" in the sense of having no elements. It just means the array doesn't have its values initialized (i.e., they are unpredictable and depend on whatever happens to be in the memory allocated for the array).

like image 113
BrenBarn Avatar answered Aug 28 '26 01:08

BrenBarn