Python 将结果保存为LUT
我必须将结果保存为查找表,以备将来使用。Python 将结果保存为LUT,python,numpy,Python,Numpy,我必须将结果保存为查找表,以备将来使用。 在我的真实案例中,文件将非常大,大约1GB。所以我需要更快速的方式来读取文件。 保存以供将来访问的最佳方式和文件格式是什么? import numpy as np, itertools x1 = np.linspace(0.1, 3.5, 3) x2 = np.arange(5, 24, 3) x3 = np.arange(50.9, 91.5, 3) def calculate(x1,x2,x3): res = x1**5+x2*x1+x3
在我的真实案例中,文件将非常大,大约1GB。所以我需要更快速的方式来读取文件。 保存以供将来访问的最佳方式和文件格式是什么?
import numpy as np, itertools
x1 = np.linspace(0.1, 3.5, 3)
x2 = np.arange(5, 24, 3)
x3 = np.arange(50.9, 91.5, 3)
def calculate(x1,x2,x3):
res = x1**5+x2*x1+x3
return res
products = list(itertools.product(x1,x2,x3))
results = [calculate(a,b,c) for a,b,c in products]
我的未来用途如下:
outputs = np.column_stack((products,results))
np.savetxt('test.out',outputs, delimiter = ',')
我将从列表中构造一个一维数组,并将其保存:
#given_x1,given_x2,given_x3 = 0.2, 8, 60
#open the look up table
#read the neighbouring two values for the given values
#linearly interpolate between two values for the results.
这是一维阵列吗?您可以将其加载到一个numpy数组中,然后使用
np.savetxt
将其写出。在我的实际情况中,该文件将非常大,大约1GB。稍后用文本文件读取数据是可以的。应该是,现在必须走,但如果这个问题仍然没有答案,我将在一个小时内破解time@EdChum你在吗?
In [37]:
a = np.array([calculate(a,b,c) for a,b,c in products])
np.savetxt(r'c:\data\lut.txt', a)
In [39]:
b = np.loadtxt(r'c:\data\lut.txt')
np.all(a==b)
Out[39]:
True