Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy empty list type inference

Tags:

python

numpy

Why is the empty list [] being inferred as float type when using np.append?

np.append([1,2,3], [0])
# output: array([1, 2, 3, 0]), dtype = np.int64

np.append([1,2,3], [])
# output: array([1., 2., 3.]), dtype = np.float64

This is persistent even when using a np.array([1,2,3], dtype=np.int32) as arr.

It's not possible to specify a dtype for append, so I am just curious on why this happens. Numpy's concatenate does the same thing, but when I try to specify the dtype I get an error:

np.concatenate([[1,2,3], []], dtype=np.int64)

Error:

TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'same_kind'

But finally if I set the unsafe casting rule it works:

np.concatenate([[1,2,3], []], dtype=np.int64, casting='unsafe')

Why is [] considered a float?

like image 869
Kevin Avatar asked May 28 '21 22:05

Kevin


People also ask

How do I check if a list is empty in Python?

Empty lists are considered False in Python, hence the bool() function would return False if the list was passed as an argument. Other methods you can use to check if a list is empty are placing it inside an if statement, using the len() methods, or comparing it with an empty list.

How do you declare an empty Numpy array in Python?

empty() in Python. numpy. empty(shape, dtype = float, order = 'C') : Return a new array of given shape and type, with random values.

How do you create an empty list in Python?

You can create an empty list using an empty pair of square brackets [] or the type constructor list() , a built-in function that creates an empty list when no arguments are passed. Square brackets [] are commonly used in Python to create empty lists because it is faster and more concise.


1 Answers

np.append is subject to well-defined semantic rules like any Numpy binary operation. As a result, it first converts the input operands to Numpy arrays if this is not the case (typically with np.array) and then apply the semantic rules to find the type of the resulting array and check it is a valid operation before applying the actual operation (here the concatenation). The array type returned by np.array is "determined as the minimum type required to hold the objects in the sequence" regarding to the documentation. When the list is empty, like in your case, the default type is numpy.float64 as stated in the documentation of np.empty. This arbitrary choice was made long ago and has not been changed since in order not to break old codes. Please note that It seems not all Numpy developers agree with the current choice and so this is a matter of debate. For more information, you can read this opened issue.

The rule of thumb is to use either existing Numpy arrays or to perform an explicit conversion to a Numpy array using np.array with a fixed dtype parameter (as described in the above comments).

like image 59
Jérôme Richard Avatar answered Nov 18 '22 13:11

Jérôme Richard