本文簡要介紹 python 語言中 numpy.savez
的用法。
用法:
numpy.savez(file, *args, **kwds)
以未壓縮的
.npz
格式將多個數組保存到一個文件中。提供數組作為關鍵字參數,以將它們存儲在輸出文件中的相應名稱下:
savez(fn, x=x, y=y)
。如果數組被指定為位置參數,即,
savez(fn, x, y)
,他們的名字將是arr_0,arr_1, 等等。- file: 字符串或文件
將保存數據的文件名(字符串)或打開的文件(file-like 對象)。如果文件是一個字符串或一個路徑,
.npz
擴展名將被附加到文件名(如果它不存在)。- args: 參數,可選
要保存到文件的數組。請使用關鍵字參數(參見下麵的 kwds)為數組分配名稱。指定為 args 的數組將命名為 “arr_0”、“arr_1” 等。
- kwds: 關鍵字參數,可選
要保存到文件的數組。每個數組都將以其對應的關鍵字名稱保存到輸出文件中。
- None
參數:
返回:
注意:
.npz
文件格式是文件的壓縮存檔,以它們包含的變量命名。存檔未壓縮,存檔中的每個文件都包含一個.npy
格式的變量。有關.npy
格式的說明,請參閱numpy.lib.format
。打開保存的時候
.npz
文件與numpy.load a NpzFile對象被返回。這是一個類似字典的對象,可以查詢其數組列表(使用.files
屬性),以及數組本身。傳入的 key 千瓦時用作 ZIP 存檔中的文件名。因此, key 應該是有效的文件名;例如,避免以
/
或包含.
.使用關鍵字參數命名變量時,不能命名變量
file
,因為這會導致file
參數在調用savez
時被定義兩次。例子:
>>> from tempfile import TemporaryFile >>> outfile = TemporaryFile() >>> x = np.arange(10) >>> y = np.sin(x)
將
savez
與 *args 一起使用,數組將以默認名稱保存。>>> np.savez(outfile, x, y) >>> _ = outfile.seek(0) # Only needed here 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])
將
savez
與 **kwds 一起使用,數組將與關鍵字名稱一起保存。>>> 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])
相關用法
- Python numpy savez_compressed用法及代碼示例
- Python numpy save用法及代碼示例
- Python numpy savetxt用法及代碼示例
- Python numpy searchsorted用法及代碼示例
- Python numpy shape用法及代碼示例
- Python numpy scimath.log用法及代碼示例
- Python numpy signbit用法及代碼示例
- Python numpy setdiff1d用法及代碼示例
- Python numpy seterr用法及代碼示例
- Python numpy sort用法及代碼示例
- Python numpy scimath.logn用法及代碼示例
- Python numpy square用法及代碼示例
- Python numpy std用法及代碼示例
- Python numpy scimath.log2用法及代碼示例
- Python numpy sum用法及代碼示例
- Python numpy spacing用法及代碼示例
- Python numpy seterrobj用法及代碼示例
- Python numpy squeeze用法及代碼示例
- Python numpy scimath.arccos用法及代碼示例
- Python numpy shares_memory用法及代碼示例
- Python numpy s_用法及代碼示例
- Python numpy swapaxes用法及代碼示例
- Python numpy sctype2char用法及代碼示例
- Python numpy show_config用法及代碼示例
- Python numpy set_printoptions用法及代碼示例
注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.savez。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。