Python 如何从';浮动64';至';浮动';
如何将Python 如何从';浮动64';至';浮动';,python,arrays,numpy,types,casting,Python,Arrays,Numpy,Types,Casting,如何将numpy数组从类型'float64'转换为类型'float'?具体来说,如何将带有dtype的整个数组转换为dtype?这可能吗上述“被认为是重复的”问题中的标量答案并没有解决我的问题。 考虑这一点: >>> type(my_array[0]) <type 'numpy.float64'> >>> # Let me try to convert this to 'float': >>> new_array = my_ar
numpy
数组
从类型'float64'
转换为类型'float'
?具体来说,如何将带有dtype的整个数组
转换为dtype
?这可能吗上述“被认为是重复的”问题中的标量答案并没有解决我的问题。
考虑这一点:
>>> type(my_array[0])
<type 'numpy.float64'>
>>> # Let me try to convert this to 'float':
>>> new_array = my_array.astype(float)
>>> type(new_array[0])
<type 'numpy.float64'>
>>> # No luck. What about this:
>>> new_array = my_array.astype('float')
>>> type(new_array[0])
<type 'numpy.float64'>
>>> # OK, last try:
>>> type(np.inf)
<type 'float'>
>>> # Yeah, that's what I want.
>>> new_array = my_array.astype(type(np.inf))
>>> type(new_array[0])
<type 'numpy.float64'>
>类型(my_数组[0])
>>>#让我尝试将其转换为“浮动”:
>>>new_array=my_array.astype(float)
>>>类型(新的_数组[0])
>>>运气不好。那么这个呢:
>>>new\u array=my\u array.astype('float')
>>>类型(新的_数组[0])
>>>#好的,最后一次尝试:
>>>类型(np.inf)
>>>是的,这就是我想要的。
>>>new_array=my_array.astype(type(np.inf))
>>>类型(新的_数组[0])
如果您不确定我为什么要这样做,请参阅及其答案。是的,实际上,当您使用Python的原生float
指定数组的数据类型时,numpy会将其转换为float64
。如
注意,在上面,我们使用Python float对象作为数据类型NumPy知道int
指np.int
,bool
指np.bool
,即float
是np.float
,complex
是np.complex
。其他数据类型没有Python等价物
及-
float\是float64的简写
这就是为什么即使使用float
将整个数组转换为float,它仍然使用np.float64
根据另一个问题的要求,最好的解决方案是将每个标量值作为一个变量,然后转换为正常的float对象-
float(new_array[0])
我能想到的一个解决方案是为float
创建一个子类,并将其用于强制转换(尽管在我看来这很糟糕)。但如果可能的话,我更喜欢前面的解决方案。范例-
In [20]: import numpy as np
In [21]: na = np.array([1., 2., 3.])
In [22]: na = np.array([1., 2., 3., np.inf, np.inf])
In [23]: type(na[-1])
Out[23]: numpy.float64
In [24]: na[-1] - na[-2]
C:\Anaconda3\Scripts\ipython-script.py:1: RuntimeWarning: invalid value encountered in double_scalars
if __name__ == '__main__':
Out[24]: nan
In [25]: class x(float):
....: pass
....:
In [26]: na_new = na.astype(x)
In [28]: type(na_new[-1])
Out[28]: float #No idea why its showing float, I would have thought it would show '__main__.x' .
In [29]: na_new[-1] - na_new[-2]
Out[29]: nan
In [30]: na_new
Out[30]: array([1.0, 2.0, 3.0, inf, inf], dtype=object)
您可以像这样创建匿名类型float
>>> new_array = my_array.astype(type('float', (float,), {}))
>>> type(new_array[0])
<type 'float'>
>>new_array=my_array.astype(类型('float',(float,),{}))
>>>类型(新的_数组[0])
如果您想留在numpy,这不是一个好主意,但是如果您已经完成了计算,并且要迁移到本机python中,您可以使用
ndarray.tolist()
这会将数组转换为适当本机类型的列表(列表)。它也适用于numpy标量值。AFAIKfloat
和float64
在numpy中是等效的。可能重复的井可以使用np.errstate忽略特定的警告,这样就不必隐藏其他警告。@AlexanderHuszagh,好的,是的,这很酷。谢谢我一直在用warnings
模块捕捉警告,并且try
/除了
块之外,但是errstate
似乎要好得多。@AlexanderHuszagh,这也是一个很好的选择。但是我认为我在代码中要做的最好、最清楚的事情就是在np.errstate
块中执行减法。聪明的答案,谢谢。也就是说,我认为最好的答案——至少就我的目的而言——是忽略我使用np.errstate
使用float64
s得到的具体警告——请参见上面的评论。我可以回答其中一些问题。Numpy将找不到合适数据类型的所有内容存储为“object”,即Python对象。对于类X,它无法通过类继承看到浮点值,因此它选择对象。由于na_new中的值的类型都是float,但dtype是object,这意味着所有内容都是使用Pythonic类型而不是numpy float64存储的。但是,您可以对对象数据类型执行数学运算,这确实提供了一些优势。