簡體   English   中英

將numpy.arrays增量添加到保存文件

[英]Incremently appending numpy.arrays to a save file

我已經嘗試了Hpaulji概述的這種方法,但是它似乎不起作用:

如何在Python中將多個numpy文件附加到一個numpy文件中

基本上,我要遍歷一個生成器,對數組進行一些更改,然后嘗試保存每個迭代的數組。

這是我的示例代碼如下所示:

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

但是當我嘗試多次加載數組時,如此處所述, 如何在python中將多個numpy文件附加到一個numpy文件中 ,出現了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所有調用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文件中可能會有好處(我想在內存有限的情況下有好處), .npz 技術上講 ,它們是用來存儲一個數組的,您可以使用.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM