NumPy中astype的有效参数

时间:2021-11-05 15:50:02

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

我是NumPy和SciPy的新手。与Matlab不同,似乎NumPy中的每个数组都有一个数据类型。

Suppose that we have an integer array x:

假设我们有一个整数数组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:

如果我想将数组转换为float,那么它似乎如下工作:

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:

但是对于用于y1和y2的其他表达式,如果我省略单引号,则它不起作用:

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?

所以,我有点困惑为什么y4工作,但y5和y6导致错误。有人可以开导我吗?

3 个解决方案

#1


5  

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.

其他表达式工作,您只需要从numpy导入类型。你不需要为float做这个,因为它是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.

字符串类型和类型类型输入都在内部转换为numpy.dtype对象,这是您在使用ndarray.dtype属性时看到的内容。

#2


2  

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.

我得到一个NameError:名称'float64'未定义。在将任何内容传递给x.astype方法之前,Python解释器会产生错误。

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

如果您只是在交互式解释器中键入float64,则会收到相同的错误。

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

np.float64确实有效,因为np命名空间中有这样的变量。它实际上是一个numpy类。

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).

浮动也有效。它也是一个类,一个基类Python(它也可以用作函数,将字符串或数字转换为浮点对象)。

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

'float64'是一个字符串,astype可以理解,可能是通过在表中查找某些东西。 (我可以看一下)。

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('bar')给出了同样的错误。

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

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

都返回相同的dtype('float64')对象。

#3


1  

reading the documentation of astype:

阅读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.

当你使用没有引号的float时,你就是在使用dtype。当你使用“浮动”时,你正在使用str。

float64 and float_aren't dtypes in python.

python中的float64和float_are not dtypes。

#1


5  

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.

其他表达式工作,您只需要从numpy导入类型。你不需要为float做这个,因为它是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.

字符串类型和类型类型输入都在内部转换为numpy.dtype对象,这是您在使用ndarray.dtype属性时看到的内容。

#2


2  

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.

我得到一个NameError:名称'float64'未定义。在将任何内容传递给x.astype方法之前,Python解释器会产生错误。

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

如果您只是在交互式解释器中键入float64,则会收到相同的错误。

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

np.float64确实有效,因为np命名空间中有这样的变量。它实际上是一个numpy类。

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).

浮动也有效。它也是一个类,一个基类Python(它也可以用作函数,将字符串或数字转换为浮点对象)。

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

'float64'是一个字符串,astype可以理解,可能是通过在表中查找某些东西。 (我可以看一下)。

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('bar')给出了同样的错误。

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

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

都返回相同的dtype('float64')对象。

#3


1  

reading the documentation of astype:

阅读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.

当你使用没有引号的float时,你就是在使用dtype。当你使用“浮动”时,你正在使用str。

float64 and float_aren't dtypes in python.

python中的float64和float_are not dtypes。