简体   繁体   English

按列索引的Numpy RecArray截断浮点数

[英]Numpy recarray indexing by column truncates floats

In a simple recarray in python, the output value is getting truncated when indexed by column name: 在python中的简单recarray中,按列名索引时,输出值将被截断:

import numpy #1.10.0
arr = numpy.zeros(1, dtype=[('a', np.float)])
arr[0]['a'] = 0.1234567891234
print arr
print arr['a']

[(0.1234567891234,)]
[ 0.12345679]

Why does this happen? 为什么会这样? Can I get the full, non-truncated value with column indexing? 我可以通过列索引获取完整的,非截断的值吗?

The print precision for a numeric array is 8 digits: 数字数组的打印精度为8位:

In [250]: np.get_printoptions()
Out[250]: 
{'edgeitems': 3,
 'formatter': None,
 'infstr': 'inf',
 'linewidth': 75,
 'nanstr': 'nan',
 'precision': 8,
 'suppress': False,
 'threshold': 1000}

But it doesn't use that value when displaying the recarray or its records. 但是在显示RecArray或其记录时,它不使用该值。 You'd probably also see the longer print with the scalar value: 您可能还会看到带有标量值的更长打印内容:

print arr['a'].item()

============== ==============

In [252]: arr = np.zeros(1, dtype=[('a', np.float)])
     ...: arr[0]['a'] = 0.1234567891234
     ...: 

In [253]: arr
Out[253]: 
array([(0.1234567891234,)], 
      dtype=[('a', '<f8')])

In [254]: arr[0]
Out[254]: (0.1234567891234,)

In [255]: arr['a']
Out[255]: array([ 0.12345679])

In [256]: arr['a'].item()
Out[256]: 0.1234567891234

In [257]: arr['a'][0]
Out[257]: 0.1234567891234

================== ==================

https://github.com/numpy/numpy/issues/5463 https://github.com/numpy/numpy/issues/5463

array2string handles floats differently for structured array and ndarray touches on this. array2string handles floats differently for structured array and ndarray The formatting of numbers in a structured array record does not follow print options . 结构化数组记录中数字的格式不遵循print options

As others have mentioned, this is a question of printing precision. 正如其他人所提到的,这是打印精度的问题。 You can set a different value like this, using set_printoptions : 您可以使用set_printoptions设置类似的其他值:

import numpy
numpy.set_printoptions(precision=20)
arr = numpy.zeros(1, dtype=[('a', numpy.float)])
arr[0]['a'] = 0.1234567891234
print arr
print arr['a']

> [(0.1234567891234,)]
> [ 0.12345678912339999589] 

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

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