简体   繁体   English

Python:numpy数据类型转换

[英]Python: numpy data type casting

I'm confused by some output I'm seeing. 我对看到的某些输出感到困惑。

If p is a list of floats, what is the difference between: 如果p是浮点数列表,则之间有什么区别?

input = np.array([p]).astype('f')

and

input = np.array([p],float)

If I use the second option and then do a print(input), I always get something like: 如果我使用第二个选项,然后执行print(input),则总会得到类似以下内容:

[array([-0.662, 0.246, 1.029])] [数组([-0.662,0.246,1.029])]

But if I use the first option, sometimes I get simply: [[ 0.61900002 1.71300006 2.16899991]] 但是,如果我使用第一个选项,有时我会简单地得到:[[0.61900002 1.71300006 2.16899991]]

but other times I get the [array([])] form. 但是其他时候我得到[array([])]表格。

here is the explanation: 这里是解释:

In [217]: np.array([1.1,1.2,1.3]).astype('f')
Out[217]: array([ 1.10000002,  1.20000005,  1.29999995], dtype=float32)

In [218]: np.array([1.1,1.2,1.3]).astype('float')
Out[218]: array([ 1.1,  1.2,  1.3])

In [219]: np.array([1.1,1.2,1.3]).astype(float)
Out[219]: array([ 1.1,  1.2,  1.3])

Types: 类型:

In [220]: np.array([1.1,1.2,1.3]).astype(float).dtype
Out[220]: dtype('float64')

In [221]: np.array([1.1,1.2,1.3]).astype('f').dtype
Out[221]: dtype('float32')

so you will have the same result using np.array([p], 'f') : 因此,使用np.array([p], 'f')会得到相同的结果:

In [224]: np.array([1.1,1.2,1.3],'f')
Out[224]: array([ 1.10000002,  1.20000005,  1.29999995], dtype=float32)

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

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