當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.load()用法及代碼示例

numpy.load()函數從具有npy擴展名(.npy)的磁盤文件返回輸入數組。

用法:numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding=’ASCII’)

參數:
file ::file-like對象,字符串或pathlib.Path。要讀取的文件。 File-like對象必須支持seek()和read()方法。
mmap_mode :如果不為None,則使用給定模式memory-map文件(有關詳細信息,請參見numpy.memmap
模式說明)。
allow_pickle :允許加載存儲在npy文件中的醃製對象數組。
fix_imports :僅在在Python 3上加載Python 2生成的醃製文件時有用,該文件包括包含對象數組的npy /npz文件。
encoding :僅當在Python 3中加載Python 2生成的醃製文件時有用,該文件包含包含對象數組的npy /npz文件。


Returns :數據存儲在文件中。對於.npz文件,必須關閉NpzFile類的返回實例,以避免泄漏文件描述符。

代碼1:工作

# Python program explaining  
# load() function  
  
import numpy as geek 
  
a = geek.array(([i + j for i in range(3)  
                       for j in range(3)])) 
# a is printed. 
print("a is:") 
print(a) 
  
geek.save('geekfile', a) 
print("the array is saved in the file geekfile.npy") 
  
# the array is saved in the file geekfile.npy  
b = geek.load('geekfile.npy') 
  
# the array is loaded into b 
print("b is:") 
print(b) 
  
# b is printed from geekfile.npy 
print("b is printed from geekfile.npy")

輸出:

a is:
[0, 1, 2, 1, 2, 3, 2, 3, 4]
the array is saved in the file geekfile.npy
b is:
[0, 1, 2, 1, 2, 3, 2, 3, 4]
b is printed from geekfile.npy


代碼2:

# Python program explaining  
# load() function  
  
import numpy as geek 
  
# a and b are numpy arrays. 
a = geek.array(([i + j for i in range(3)  
                       for j in range(3)])) 
b = geek.array([i + 1 for i in range(3)]) 
  
# a and b are printed. 
print("a is:") 
print(a) 
print("b is:") 
print(b) 
  
# a and b are stored in geekfile.npz 
geek.savez('geekfile.npz', a = a, b = b) 
  
print("a and b are stored in geekfile.npz") 
  
# compressed file is loaded 
c = geek.load('geekfile.npz') 
  
print("after loading...") 
print("a is:", c['a']) 
print("b is:", c['b'])

輸出:

a is:
[0 1 2 1 2 3 2 3 4]
b is:
[1 2 3]
a and b are stored in geekfile.npz
after loading...
a is:[0 1 2 1 2 3 2 3 4]
b is:[1 2 3]


相關用法


注:本文由純淨天空篩選整理自ArkadipGhosh大神的英文原創作品 numpy.load() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。