5

numpyloadtxt関数を使用して大量のデータを読み込みます。データは四捨五入されているようです。例:テキストファイルの番号は-3.79000000000005E + 01ですが、numpyは番号を-37.9として読み取ります。loadtxt呼び出しでdypteをnp.float64に設定しました。元のデータファイルの精度を維持する方法はありますか?

4

1 に答える 1

6

loadtxt数値を四捨五入していません。表示されているのは、NumPy が配列を出力するために選択した方法です

In [80]: import numpy as np

In [81]: x = np.loadtxt('test.dat', dtype = np.float64)

In [82]: print(x)
-37.9

実際の値は、入力された値に最も近い np.float64 です。

In [83]: x
Out[83]: array(-37.9000000000005)

または、より高い次元の配列を持っている可能性が高いインスタンスでは、

In [2]: x = np.loadtxt('test.dat', dtype = np.float64)

のが切り捨てられているようreprに見える場合:x

In [3]: x
Out[3]: array([-37.9, -37.9])

np.set_printoptionsより高い精度を得るために使用できます:

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

In [5]: np.set_printoptions(precision = 17)

In [6]: x
Out[6]: array([-37.90000000000050306, -37.90000000000050306])

(これを指摘してくれた@mgilsonに感謝します。)

于 2013-02-14T23:09:10.370 に答える