jax.numpy.savez#
- jax.numpy.savez(file, *args, allow_pickle=True, **kwds)[原始碼]#
將多個陣列儲存到單個檔案,以未壓縮的
.npz
格式儲存。以關鍵字引數提供陣列,以將它們儲存在輸出檔案中的相應名稱下:
savez(fn, x=x, y=y)
。如果陣列指定為位置引數,即
savez(fn, x, y)
,它們的名稱將為 arr_0、arr_1 等。- 參數:
file (file, str, 或 pathlib.Path) – 檔案名稱(字串)或將儲存資料的開啟檔案(類檔案物件)。如果 file 是字串或 Path,則如果
.npz
副檔名尚未存在,則會附加到檔案名稱。args (Arguments, optional) – 要儲存到檔案的陣列。請使用關鍵字引數(請參閱下面的 kwds)為陣列指定名稱。指定為 args 的陣列將命名為 “arr_0”、“arr_1” 等。
allow_pickle (bool, optional) – 允許使用 Python pickles 儲存物件陣列。不允許 pickles 的原因包括安全性(載入 pickled 資料可能會執行任意程式碼)和可攜性(pickled 物件可能無法在不同的 Python 安裝上載入,例如,如果儲存的物件需要不可用的函式庫,並且並非所有 pickled 資料在不同版本的 Python 之間都相容)。預設值:True
kwds (Keyword arguments, optional) – 要儲存到檔案的陣列。每個陣列將以其對應的關鍵字名稱儲存到輸出檔案。
- 返回類型:
None
另請參閱
save
將單個陣列以 NumPy 格式儲存到二進制檔案。
savetxt
將陣列以純文字格式儲存到檔案。
savez_compressed
將多個陣列儲存到壓縮的
.npz
封存檔
筆記
.npz
檔案格式是以它們包含的變數命名的檔案的 zip 封存檔。該封存檔未經壓縮,並且封存檔中的每個檔案都以.npy
格式包含一個變數。有關.npy
格式的描述,請參閱numpy.lib.format
。當使用 load 開啟已儲存的
.npz
檔案時,將返回 ~lib.npyio.NpzFile 物件。這是一個類似字典的物件,可以查詢其陣列列表(使用.files
屬性)以及陣列本身。在 kwds 中傳遞的金鑰用作 ZIP 封存檔中的檔案名稱。因此,金鑰應為有效的檔案名稱;例如,避免以
/
開頭或包含.
的金鑰。當使用關鍵字引數命名變數時,無法將變數命名為
file
,因為這會導致在呼叫savez
時file
引數被定義兩次。範例
>>> import numpy as np >>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x)
使用 *args 的 savez,陣列會以預設名稱儲存。
>>> np.savez(outfile, x, y) >>> _ = outfile.seek(0) # Only needed to simulate closing & reopening file >>> npzfile = np.load(outfile) >>> npzfile.files ['arr_0', 'arr_1'] >>> npzfile['arr_0'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
使用 **kwds 的 savez,陣列會以關鍵字名稱儲存。
>>> outfile = TemporaryFile() >>> np.savez(outfile, x=x, y=y) >>> _ = outfile.seek(0) >>> npzfile = np.load(outfile) >>> sorted(npzfile.files) ['x', 'y'] >>> npzfile['x'] array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])