简体   繁体   English

如何将 numpy 数组从“float64”转换为“float”

[英]How to convert a numpy array from 'float64' to 'float'

How do I convert a numpy array from type 'float64' to type 'float' ?如何将numpy array从类型'float64'为类型'float' Specifically, how do I convert an entire array with dtype 'float64' to have dtype 'float' ?具体来说,如何将具有dtype 'float64'的整个array转换为具有dtype 'float' Is this possible?这可能吗? The answer for scalars in the thought-to-be duplicate question above does not address my question.上面认为是重复的问题中标量的答案没有解决我的问题。

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 question and its answers.如果您不确定我为什么要这样做,请参阅此问题及其答案。

Yes, actually when you use Python's native float to specify the dtype for an array , numpy converts it to float64 .是的,实际上当您使用 Python 的原生float指定数组的 dtype 时,numpy 会将其转换为float64 As given in documentation -文档中所述 -

Note that, above, we use the Python float object as a dtype.请注意,在上面,我们使用 Python 浮点对象作为 dtype。 NumPy knows that int refers to np.int_ , bool means np.bool_ , that float is np.float_ and complex is np.complex_ . NumPy 知道int指的是np.int_bool指的是np.bool_floatnp.float_complexnp.complex_ The other data-types do not have Python equivalents.其他数据类型没有 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).我能想到的一个解决方案是为float创建一个子类并将其用于转换(尽管对我来说它看起来很糟糕)。 But I would prefer the previous solution over this if possible.但如果可能的话,我更喜欢以前的解决方案。 Example -例子 -

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)

You can create an anonymous type float like this你可以像这样创建一个匿名类型的float

>>> new_array = my_array.astype(type('float', (float,), {}))
>>> type(new_array[0])
<type 'float'>

This is not a good idea if you're trying to stay in numpy, but if you're done calculating and are moving out into native python, you can use如果您想留在 numpy 中,这不是一个好主意,但是如果您已完成计算并正在迁移到本机 python,则可以使用

ndarray.tolist()

This converts arrays to lists (of lists) of appropriate native types.这将数组转换为适当本机类型的列表(列表)。 It also works on numpy scalar values.它也适用于 numpy 标量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM