小編給大家分享一下Python中numpy.load()的使用方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
目前創(chuàng)新互聯(lián)已為1000多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站改版維護、企業(yè)網(wǎng)站設(shè)計、全南網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。numpy.load()函數(shù)從具有npy擴展名(.npy)的磁盤文件返回輸入數(shù)組。
用法:
numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding=’ASCII’)
參數(shù):
file :file-like對象,字符串或pathlib.Path。要讀取的文件。 File-like對象必須支持seek()和read()方法。
mmap_mode :如果不為None,則使用給定模式memory-map文件(有關(guān)詳細(xì)信息,請參見numpy.memmap
模式說明)。
allow_pickle :允許加載存儲在npy文件中的腌制對象數(shù)組。
fix_imports :僅在在Python 3上加載Python 2生成的腌制文件時有用,該文件包括包含對象數(shù)組的npy /npz文件。
encoding :僅當(dāng)在Python 3中加載Python 2生成的腌制文件時有用,該文件包含包含對象數(shù)組的npy /npz文件。
Returns :數(shù)據(jù)存儲在文件中。對于.npz文件,必須關(guān)閉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()的使用方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!