新聞中心
繼上篇講過Numpy如何構(gòu)建多維數(shù)組之后,今天我們來學習numpy多維數(shù)組的用法。

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),平和企業(yè)網(wǎng)站建設(shè),平和品牌網(wǎng)站建設(shè),網(wǎng)站定制,平和網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,平和網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
加法和減法操作要求操作雙方的維數(shù)信息一致,均為M*N為數(shù)組方可正確執(zhí)行操作。
a = np.arange(4) 輸出: array([0, 1, 2, 3]) b = a**2 輸出: array([0, 1, 4, 9]) c = 10*np.sin(a) 輸出: array([ 0. , 8.41470985, 9.09297427, 1.41120008]) n < 35 輸出: array([ True, True, True, True], dtype=bool) A = np.array([[1,1],[0,1]]) B = np.array([[2,0],[3,4]]) C = A * B # 元素點乘 輸出: array([[2, 0], [0, 4]]) D = A.dot(B) # 矩陣乘法 輸出: array([[5, 4], [3, 4]]) E = np.dot(A,B) # 矩陣乘法 輸出: array([[5, 4], [3, 4]])
多維數(shù)組操作過程中的類型轉(zhuǎn)換
When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting)
即操作不同類型的多維數(shù)組時,結(jié)果自動轉(zhuǎn)換為精度更高類型的數(shù)組,即upcasting
a = np.ones((2,3),dtype=int) # int32 b = np.random.random((2,3)) # float64 b += a # 正確 a += b # 錯誤
a = np.ones(3,dtype=np.int32) b = np.linspace(0,pi,3) c = a + b d = np.exp(c*1j) 輸出: array([ 0.54030231+0.84147098j, -0.84147098+0.54030231j, -0.54030231-0.84147098j]) d.dtype.name 輸出: 'complex128'
多維數(shù)組的一元操作,如求和、求最小值、值等
a = np.random.random((2,3)) a.sum() a.min() a.max() b = np.arange(12).reshape(3,4) 輸出: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) b.sum(axis=0) # 按列求和 輸出: array([12, 15, 18, 21]) b.sum(axis=1) # 按行求和 輸出: array([ 6, 22, 38]) b.cumsum(axis=0) # 按列進行元素累加 輸出: array([[ 0, 1, 2, 3], [ 4, 6, 8, 10], [12, 15, 18, 21]]) b.cumsum(axis=1) # 按行進行元素累加 輸出: array([[ 0, 1, 3, 6], [ 4, 9, 15, 22], [ 8, 17, 27, 38]]) universal functions B = np.arange(3) np.exp(B) np.sqrt(B) C = np.array([2.,-1.,4.]) np.add(B,C)
其他的ufunc函數(shù)包括:
all, any, apply_along_axis, argmax, argmin, argsort, average, bincount, ceil, clip, conj, corrcoef, cov, cross, cumprod, cumsum, diff, dot, floor,inner, lexsort, max, maximum, mean, median, min, minimum, nonzero, outer, prod, re, round, sort, std, sum, trace, transpose, var,vdot, vectorize, where
以上就是python中numpy多維數(shù)組的用法。更多Python學習推薦:PyThon學習網(wǎng)教學中心。
(推薦操作系統(tǒng):windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。)
網(wǎng)頁標題:創(chuàng)新互聯(lián)Python教程:Python中numpy多維數(shù)組的用法
文章路徑:http://www.5511xx.com/article/cdegese.html


咨詢
建站咨詢
