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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:52:56  来源:igfitidea点击:

How to convert a numpy array from 'float64' to 'float'

pythonarraysnumpytypescasting

提问by dbliss

How do I convert a numpyarrayfrom type 'float64'to type 'float'? Specifically, how do I convert an entire arraywith 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 numpyarrayfrom type转换'float64'为 type 'float'?具体来说,如何将整个arraywith转换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 floatlike 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 floatto 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 intrefers to np.int_, boolmeans np.bool_, that floatis np.float_and complexis np.complex_. The other data-types do not have Python equivalents.

请注意,在上面,我们使用 Python 浮点对象作为 dtype。NumPy的人都知道int是指np.int_bool手段np.bool_,那floatnp.float_complexnp.complex_。其他数据类型没有 Python 等价物。

And -

和 -

float_- Shorthand for float64.

float_- float64 的简写。

This is why even though you use floatto 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 floatand 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)