python - numpy.float32 到浮点转换不起作用
问题描述
我有许多类型的 numpy.float32 我想转储到一个 json 文件中,所以我需要之前转换为 float 但是当我执行 int(number) 时,更改不正确。
我有一个包含 numpy.float32 数字的列表
解决方案
“提取” a 有多种方法float32
:
In [86]: z=np.float32(1.234)
In [87]: z.tolist() # also used to make a list of Python numbers
Out[87]: 1.2339999675750732
In [88]: type(_)
Out[88]: float
In [89]: z.item() # also for a single item array
Out[89]: 1.2339999675750732
In [90]: type(_)
Out[90]: float
In [91]: float(z) # only for single item array
Out[91]: 1.2339999675750732
In [92]: type(_)
Out[92]: float
In [93]: int(z) # truncates the float
Out[93]: 1