gpt4 book ai didi

python - 递增地将 numpy.arrays 附加到保存文件

转载 作者:太空宇宙 更新时间:2023-11-04 00:22:37 24 4
gpt4 key购买 nike

我已经尝试过 Hpaulji 概述的这种方法,但它似乎不起作用:

How to append many numpy files into one numpy file in python

基本上,我正在遍历生成器,对数组进行一些更改,然后尝试保存每次迭代的数组。

这是我的示例代码:

filename = 'testing.npy'

with open(filename, 'wb') as f:
for x, _ in train_generator:
prediction = base_model.predict(x)
print(prediction[0,0,0,0:5])
np.save(filename, prediction)

current_iteration += 1
if current_iteration == 5:
break

在这里,我将进行 5 次迭代,因此我希望保存 5 个不同的数组。

为了调试目的,我打印了每个数组的一部分:

[ 0.  0.  0.  0.  0.]
[ 0. 3.37349415 0. 0. 1.62561738]
[ 0. 20.28489304 0. 0. 0. ]
[ 0. 0. 0. 0. 0.]
[ 0. 21.98013496 0. 0. 0. ]

但是当我尝试加载数组时,如此处所述多次, How to append many numpy files into one numpy file in python , 我得到一个 EOFERROR:

file = r'testing.npy'

with open(file,'rb') as f:
arr = np.load(f)
print(arr[0,0,0,0:5])
arr = np.load(f)
print(arr[0,0,0,0:5])

它只输出最后一个数组,然后是一个 EOFERROR:

[  0.          21.98013496   0.           0.           0.        ]
EOFError: Ran out of input

print(arr[0,0,0,0:5])

我原以为要保存所有 5 个数组,但是当我多次加载 save .npy 文件时,我只得到最后一个数组。

那么,我应该如何保存保存并将新数组附加到文件?

编辑:使用“.npz”进行测试只保存最后一个数组

filename = 'testing.npz'

current_iteration = 0
with open(filename, 'wb') as f:
for x, _ in train_generator:
prediction = base_model.predict(x)
print(prediction[0,0,0,0:5])
np.savez(f, prediction)



current_iteration += 1
if current_iteration == 5:
break


#loading

file = 'testing.npz'

with open(file,'rb') as f:
arr = np.load(f)
print(arr.keys())


>>>['arr_0']

最佳答案

您对 np.save 的所有调用都使用文件名,而不是文件句柄。由于您不重用文件句柄,因此每次保存都会覆盖文件而不是将数组附加到文件。

这应该有效:

filename = 'testing.npy'

with open(filename, 'wb') as f:
for x, _ in train_generator:
prediction = base_model.predict(x)
print(prediction[0,0,0,0:5])
np.save(f, prediction)

current_iteration += 1
if current_iteration == 5:
break

虽然将多个数组存储在一个 .npy 文件中可能有优势(我想在内存有限的情况下会有优势),但它们是 technically meant存储一个数组,您可以使用 .npz 文件(np.saveznp.savez_compressed)存储多个数组:

filename = 'testing.npz'
predictions = []
for (x, _), index in zip(train_generator, range(5)):
prediction = base_model.predict(x)
predictions.append(prediction)
np.savez(filename, predictions) # will name it arr_0
# np.savez(filename, predictions=predictions) # would name it predictions
# np.savez(filename, *predictions) # would name it arr_0, arr_1, …, arr_4

关于python - 递增地将 numpy.arrays 附加到保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48603119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com