Python 如何将 numpy 数组从“float64”转换为“float”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32599719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a numpy array from 'float64' to 'float'
提问by dbliss
How do I convert a numpy
array
from type 'float64'
to type 'float'
? Specifically, how do I convert an entire array
with dtype
'float64'
to have dtype
'float'
? Is this possible? The answer for scalarsin the thought-to-be duplicate question above does not address my question.
如何将 a numpy
array
from type转换'float64'
为 type 'float'
?具体来说,如何将整个array
with转换dtype
'float64'
为 have dtype
'float'
?这可能吗? 上面认为是重复的问题中标量的答案没有解决我的问题。
Consider this:
考虑一下:
>>> 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'>
If you're unsure why I might want to do this, see this questionand its answers.
如果您不确定我为什么要这样做,请参阅此问题及其答案。
采纳答案by John La Rooy
You can create an anonymous type float
like this
您可以float
像这样创建匿名类型
>>> new_array = my_array.astype(type('float', (float,), {}))
>>> type(new_array[0])
<type 'float'>
回答by Anand S Kumar
Yes, actually when you use Python's native float
to specify the dtype for an array , numpy converts it to float64
. As given in documentation -
是的,实际上当您使用 Python 的 nativefloat
为 array 指定 dtype 时,numpy 会将其转换为float64
. 如文档中所述 -
Note that, above, we use the Python float object as a dtype. NumPy knows that
int
refers tonp.int_
,bool
meansnp.bool_
, thatfloat
isnp.float_
andcomplex
isnp.complex_
. The other data-types do not have Python equivalents.
请注意,在上面,我们使用 Python 浮点对象作为 dtype。NumPy的人都知道
int
是指np.int_
,bool
手段np.bool_
,那float
是np.float_
和complex
是np.complex_
。其他数据类型没有 Python 等价物。
And -
和 -
float_- Shorthand for float64.
float_- float64 的简写。
This is why even though you use float
to convert the whole array to float , it still uses np.float64
.
这就是为什么即使您使用float
将整个数组转换为 float ,它仍然使用np.float64
.
According to the requirement from the other question , the best solution would be converting to normal float object after taking each scalar value as -
根据另一个问题的要求,最好的解决方案是将每个标量值转换为普通浮点对象 -
float(new_array[0])
A solution that I could think of is to create a subclass for float
and use that for casting (though to me it looks bad). But I would prefer the previous solution over this if possible. Example -
我能想到的一个解决方案是创建一个子类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)