Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid parameters for astype in NumPy

I'm new to NumPy and SciPy. Unlike Matlab, it seems like there is a data type associated with each array in NumPy.

Suppose that we have an integer array x:

import numpy as np
x = np.array([1, 2, 3])

If I want to convert the array into float, then it seems like the following works:

y1 = x.astype('float64')  # Works!
y2 = x.astype('float_')   # Works!
y3 = x.astype('float')    # Works!

But I'm somewhat puzzled to see the following also works without the single quotation marks.

y4 = x.astype(float)      # Still works!!

But for other expressions used for y1 and y2, if I omit the single quotation mark, it doesn't work:

y5 = x.astype(float64)  # Doesn't work.
y6 = x.astype(float_)   # Doesn't work.

So, I'm somewhat confused about why y4 works, but y5 and y6 cause an error. Could someone enlighten me on this?

like image 231
chanwcom Avatar asked Dec 16 '15 19:12

chanwcom


3 Answers

The other expressions work, you just need to import the types from numpy. You don't need to do this for float because it is a built-in type for Python.

y5 = x.astype(np.float64)
y6 = x.astype(np.float_)

Both the string-type and type-type inputs are converted to a numpy.dtype object internally, which is what you see when using the ndarray.dtype attribute.

like image 74
Martin Valgur Avatar answered Oct 27 '22 15:10

Martin Valgur


These 2 don't work because there isn't, in your workspace, variables with those names:

y5 = x.astype(float64)  # Doesn't work.
y6 = x.astype(float_)   # Doesn't work.

I get a NameError: name 'float64' is not defined. The error is produced by the Python interpreter, before anything is passed to the x.astype method.

You'd get the same error if you just typed float64 in the interactive interpreter.

np.float64 does work because there is such a variable in the np namespace. It is actually a numpy class.

float also works. It too is a class, a base Python one (it can also be used as a function, converting a string or number to a float object).

'float64' is a string, that astype understands, probably by looking up something in a table. (I could look that up).

On the other hand if I give astype something random string I get a different error

In [967]: A.astype('bar')
...
TypeError: data type "bar" not understood

np.dtype('bar') gives the same error.

np.dtype(float)
np.dtype('float64')
np.dtype('float')

all return the same dtype('float64') object.

like image 20
hpaulj Avatar answered Oct 27 '22 15:10

hpaulj


reading the documentation of astype:

dtype : str or dtype
    Typecode or data-type to which the array is cast.

When you're using float without quotes, then you're using dtype. When you're using "float", then you're using str.

float64 and float_aren't dtypes in python.

like image 1
tglaria Avatar answered Oct 27 '22 14:10

tglaria