python load_Python实例:numpy.load()的使用
編程函數千千萬,當然新的最有意思。今天小編為大家帶了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學習推薦:JQ教程網Python大全。
總結
以上是生活随笔為你收集整理的python load_Python实例:numpy.load()的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贷款10万一年利息
- 下一篇: python定义字符串1hdhdjdjd