網(wǎng)站設計需求方案正規(guī)網(wǎng)站建設平臺
鶴壁市浩天電氣有限公司
2026/01/24 14:14:22
網(wǎng)站設計需求方案,正規(guī)網(wǎng)站建設平臺,開鎖做網(wǎng)站哪個好,自貢網(wǎng)頁制作NumPy二進制文件
save()、savez()和load()函數(shù)以 numpy 專用的二進制類型#xff08;.npy、.npz#xff09;保存和讀取數(shù)據(jù)#xff0c;這三個函數(shù)會自動處理ndim、dtype、shape等信息#xff0c;使用它們讀寫數(shù)組非常方便#xff0c;但是save()和savez()輸出的文件很難與…NumPy二進制文件save()、savez()和load()函數(shù)以 numpy 專用的二進制類型.npy、.npz保存和讀取數(shù)據(jù)這三個函數(shù)會自動處理ndim、dtype、shape等信息使用它們讀寫數(shù)組非常方便但是save()和savez()輸出的文件很難與其它語言編寫的程序兼容。def save(file, arr, allow_pickleTrue, fix_importsTrue):save()函數(shù)以.npy格式將數(shù)組保存到二進制文件中。.npy格式以二進制的方式存儲文件在二進制文件第一行以文本形式保存了數(shù)據(jù)的元信息ndimdtypeshape等可以用二進制工具查看內容。def load(file, mmap_modeNone, allow_pickleFalse, fix_importsTrue, encoding‘ASCII’):load()函數(shù)從.npy、.npz或 pickled文件加載數(shù)組或pickled對象。mmap_mode: {None, ‘r’, ‘r’, ‘w’, ‘c’};讀取文件的方式。allow_pickleFalse允許加載存儲在.npy文件中的pickled對象數(shù)組。fix_importsTrue若為Truepickle將嘗試將舊的python2名稱映射到python3中使用的新名稱。encoding‘ASCII’制定編碼格式默認為“ASCII”。#將一個數(shù)組保存到一個文件中importnumpyasnp outfiler. est.npynp.random.seed(20200619)xnp.random.uniform(low0,high1,size[3,5])np.save(outfile,x)ynp.load(outfile)print(y)# [[0.01123594 0.66790705 0.50212171 0.7230908 0.61668256]# [0.00668332 0.1234096 0.96092409 0.67925305 0.38596837]# [0.72342998 0.26258324 0.24318845 0.98795012 0.77370715]]def savez(file, *args, **kwds):savez()函數(shù)以未壓縮的.npz格式將多個數(shù)組保存到單個文件中。.npz格式以壓縮打包的方式存儲文件可以用壓縮軟件解壓。savez()函數(shù)第一個參數(shù)是文件名其后的參數(shù)都是需要保存的數(shù)組也可以使用關鍵字參數(shù)為數(shù)組起一個名字非關鍵字參數(shù)傳遞的數(shù)組會自動起名為arr_0, arr_1, …。savez()函數(shù)輸出的是一個壓縮文件擴展名為.npz其中每個文件都是一個save()保存的.npy文件文件名對應于數(shù)組名。load()自動識別.npz文件并且返回一個類似于字典的對象可以通過數(shù)組名作為關鍵字獲取數(shù)組的內容。#將多個數(shù)組保存到一個文件importnumpyasnp outfiler. est.npzxnp.linspace(0,np.pi,5)ynp.sin(x)znp.cos(x)np.savez(outfile,x,y,z_dz)datanp.load(outfile)np.set_printoptions(suppressTrue)print(data.files)# [z_d, arr_0, arr_1]print(data[arr_0])# [0. 0.78539816 1.57079633 2.35619449 3.14159265]print(data[arr_1])# [0. 0.70710678 1. 0.70710678 0. ]print(data[z_d])# [ 1. 0.70710678 0. -0.70710678 -1. ]用解壓軟件打開 test.npz 文件會發(fā)現(xiàn)其中有三個文件arr_0.npy,arr_1.npy,z_d.npy其中分別保存著數(shù)組x,y,z的內容。文本文件savetxt()loadtxt()和genfromtxt()函數(shù)用來存儲和讀取文本文件如.TXT.CSV等。genfromtxt()比loadtxt()更加強大可對缺失數(shù)據(jù)進行處理。def savetxt(fname, X, fmt‘%.18e’, delimiter’ ‘, newline’
’,header‘’, footer‘’, comments# , encodingNone):fname文件路徑X存入文件的數(shù)組。fmt‘%.18e’寫入文件中每個元素的字符串格式默認’%.18e’保留18位小數(shù)的浮點數(shù)形式。delimiter’ 分割字符串默認以空格分隔。def loadtxt(fname, dtypefloat, comments‘#’, delimiterNone,convertersNone, skiprows0, usecolsNone, unpackFalse,ndmin0, encoding‘bytes’, max_rowsNone):fname文件路徑。dtypefloat數(shù)據(jù)類型默認為float。comments‘#’: 字符串或字符串組成的列表默認為’#表示注釋字符集開始的標志。skiprows0跳過多少行一般跳過第一行表頭。usecolsNone元組元組內數(shù)據(jù)為列的數(shù)值索引 用來指定要讀取數(shù)據(jù)的列第一列為0。unpackFalse當加載多列數(shù)據(jù)時是否需要將數(shù)據(jù)列進行解耦賦值給不同的變量。#寫入和讀出TXT文件importnumpyasnp outfiler. est.txtxnp.arange(0,10).reshape(2,-1)np.savetxt(outfile,x)ynp.loadtxt(outfile)print(y)# [[0. 1. 2. 3. 4.]# [5. 6. 7. 8. 9.]]test.txt文件如下0.000000000000000000e00 1.000000000000000000e00 2.000000000000000000e00 3.000000000000000000e00 4.000000000000000000e005.000000000000000000e00 6.000000000000000000e00 7.000000000000000000e00 8.000000000000000000e00 9.000000000000000000e00#寫入和讀出CSV文件importnumpyasnp outfiler. est.csvxnp.arange(0,10,0.5).reshape(4,-1)np.savetxt(outfile,x,fmt%.3f,delimiter,)ynp.loadtxt(outfile,delimiter,)print(y)# [[0. 0.5 1. 1.5 2. ]# [2.5 3. 3.5 4. 4.5]# [5. 5.5 6. 6.5 7. ]# [7.5 8. 8.5 9. 9.5]]test.csv文件如下0.000,0.500,1.000,1.500,2.0002.500,3.000,3.500,4.000,4.5005.000,5.500,6.000,6.500,7.0007.500,8.000,8.500,9.000,9.500def genfromtxt(fname, dtypefloat, comments‘#’, delimiterNone,skip_header0, skip_footer0, convertersNone,missing_valuesNone, filling_valuesNone, usecolsNone,namesNone, excludelistNone,deletechars‘’.join(sorted(NameValidator.defaultdeletechars)),replace_space‘_’, autostripFalse, case_sensitiveTrue,defaultfmt“f%i”, unpackNone, usemaskFalse, looseTrue,invalid_raiseTrue, max_rowsNone, encoding‘bytes’):genfromtxt()函數(shù)從文本文件加載數(shù)據(jù)并按指定方式處理缺少的值是面向結構數(shù)組和缺失數(shù)據(jù)處理的。。namesNone設置為True時程序將把第一行作為列名稱。data.csv文件不帶缺失值id,value1,value2,value31,123,1.4,232,110,0.5,183,164,2.1,19importnumpyasnp outfiler.data.csvxnp.loadtxt(outfile,delimiter,,skiprows1)print(x)# [[ 1. 123. 1.4 23. ]# [ 2. 110. 0.5 18. ]# [ 3. 164. 2.1 19. ]]xnp.loadtxt(outfile,delimiter,,skiprows1,usecols(1,2))print(x)# [[123. 1.4]# [110. 0.5]# [164. 2.1]]val1,val2np.loadtxt(outfile,delimiter,,skiprows1,usecols(1,2),unpackTrue)print(val1)# [123. 110. 164.]print(val2)# [1.4 0.5 2.1]importnumpyasnp outfiler.data.csvxnp.genfromtxt(outfile,delimiter,,namesTrue)print(x)# [(1., 123., 1.4, 23.) (2., 110., 0.5, 18.) (3., 164., 2.1, 19.)]print(type(x))# class numpy.ndarrayprint(x.dtype)# [(id, f8), (value1, f8), (value2, f8), (value3, f8)]print(x[id])# [1. 2. 3.]print(x[value1])# [123. 110. 164.]print(x[value2])# [1.4 0.5 2.1]print(x[value3])# [23. 18. 19.]data1.csv文件帶有缺失值id,value1,value2,value31,123,1.4,232,110,,183,,2.1,19【例子】importnumpyasnp outfiler.data1.csvxnp.genfromtxt(outfile,delimiter,,namesTrue)print(x)# [(1., 123., 1.4, 23.) (2., 110., nan, 18.) (3., nan, 2.1, 19.)]print(type(x))# class numpy.ndarrayprint(x.dtype)# [(id, f8), (value1, f8), (value2, f8), (value3, f8)]print(x[id])# [1. 2. 3.]print(x[value1])# [123. 110. nan]print(x[value2])# [1.4 nan 2.1]print(x[value3])# [23. 18. 19.]文本格式選項def set_printoptions(precisionNone, thresholdNone, edgeitemsNone,linewidthNone, suppressNone, nanstrNone, infstrNone,formatterNone, signNone, floatmodeNone, **kwarg):set_printoptions()函數(shù)設置打印選項。這些選項決定浮點數(shù)、數(shù)組和其它NumPy對象的顯示方式。precision8設置浮點精度控制輸出的小數(shù)點個數(shù)默認是8。threshold1000概略顯示超過該值則以“…”的形式來表示默認是1000。linewidth75用于確定每行多少字符數(shù)后插入換行符默認為75。suppressFalse當suppressTrue表示小數(shù)不需要以科學計數(shù)法的形式輸出默認是False。nanstrnan浮點非數(shù)字的字符串表示形式默認nan。infstrinf浮點無窮大的字符串表示形式默認inf。formatter一個字典自定義格式化用于顯示的數(shù)組元素。鍵為需要格式化的類型值為格式化的字符串。‘bool’‘int’‘float’‘str’ : all other strings‘a(chǎn)ll’ : sets all types…importnumpyasnp np.set_printoptions(precision4)xnp.array([1.123456789])print(x)# [1.1235]np.set_printoptions(threshold20)xnp.arange(50)print(x)# [ 0 1 2 ... 47 48 49]np.set_printoptions(thresholdnp.iinfo(np.int).max)print(x)# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23# 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47# 48 49]epsnp.finfo(float).eps xnp.arange(4.)xx**2-(xeps)**2print(x)# [-4.9304e-32 -4.4409e-16 0.0000e00 0.0000e00]np.set_printoptions(suppressTrue)print(x)# [-0. -0. 0. 0.]xnp.linspace(0,10,10)print(x)# [ 0. 1.1111 2.2222 3.3333 4.4444 5.5556 6.6667 7.7778 8.8889# 10. ]np.set_printoptions(precision2,suppressTrue,threshold5)print(x)# [ 0. 1.11 2.22 ... 7.78 8.89 10. ]np.set_printoptions(formatter{all:lambdax:int: str(-x)})xnp.arange(3)print(x)# [int: 0 int: -1 int: -2]np.set_printoptions()# formatter gets resetprint(x)# [0 1 2]恢復默認選項np.set_printoptions(edgeitems3,infstrinf,linewidth75,nanstrnan,precision8,suppressFalse,threshold1000,formatterNone)get_printoptions()函數(shù)獲取當前打印選項