您好,登錄后才能下訂單哦!
小編給大家分享一下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]
看完了這篇文章,相信你對Python中numpy.load()的使用方法有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。