簡體   English   中英

按列索引的Numpy RecArray截斷浮點數

[英]Numpy recarray indexing by column truncates floats

在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]

為什么會這樣? 我可以通過列索引獲取完整的,非截斷的值嗎?

數字數組的打印精度為8位:

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

但是在顯示RecArray或其記錄時,它不使用該值。 您可能還會看到帶有標量值的更長打印內容:

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

array2string handles floats differently for structured array and ndarray 結構化數組記錄中數字的格式不遵循print options

正如其他人所提到的,這是打印精度的問題。 您可以使用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