對numpy中shape的理解

2022-02-20 07:02:50 字數 4221 閱讀 8607

from:

環境:windows, python3.5

>>>> import numpy as np  

>>> a = np.array([2,3,33])

>>> a

array([ 2 3 33 ])

>>> print(a)

[ 2 3 33 ]

>>> a.shape

(3, )

>>> a.shape[0]  

3>>> a.shape[1]  

traceback (most recent call last):

file "", line 1, in

indexerror: tuple index out of range

一維情況中array建立的可以看做list(或一維陣列),建立時用()和[ ]都可以,多維也一樣,兩種方法建立後的輸出顯示結果也相同,這裡使用[ ]進行建立

輸出a的shape會顯示乙個引數,就是這個list中元素個數

建立時也可以直接使用np.zeros([1]),這樣會建立全0的list,或者np.ones([1]),不需要我們輸入資料,見下圖:

>>>> a = np.zeros([1])  

b = np.ones([1])

>>> print(a)

[ 0. ]

>>> print(b)

[ 1. ]

>>> a = np.array([[2,3,33],[2,1,1]])

>>> a

array([[ 2, 3, 33],

[ 2, 1, 1]])

>>> a.shape[0]

2>>> a.shape[1]

3>>> a.shape[2]

traceback (most recent call last):

file "", line 1, in indexerror: tuple index out of range

二維情況中array建立的可以看做二維陣列(矩陣),注意建立時需要使用2個[ ],輸出a的shape顯示的(2,3)相當於有2行,每行3個數,使用np.ones建立結果如下:

>>> a = np.ones([2, 3])  

>>> a

array([[ 1., 1., 1.],

[ 1., 1., 1.]])

多維情況統一使用np.ones進行建立,先看三維情況:

>>> a = np.ones([1,1,1])  

>>> a

array([[[ 1.]]])

>>> a = np.ones([1,1,2])

>>> a

array([[[ 1., 1.]]])

>>> a = np.ones([1,2,1])

>>> a

array([[[ 1.],

[ 1.]]])

>>> a = np.ones([2,1,1])

>>> a

array([[[ 1.]],

[[ 1.]]])

從上面的**可以看出,三維情況建立時後面2個引數可以看做是建立二維陣列,第1個引數看做建立的二維陣列的個數,所以建立時輸入的引數為2,3,2時,就相當於建立了2個3行2列的二維陣列,如下:

>>> a = np.ones([2,3,2])  

>>> a

array([[[ 1., 1.],

[ 1., 1.],

[ 1., 1.]],

[[ 1., 1.],

[ 1., 1.],

[ 1., 1.]]])

然後看四維情況:

>>> a = np.ones([1,1,1,1])  

>>> a

array([[[[ 1.]]]])

>>> a = np.ones([1,1,1,2])

>>> a

array([[[[ 1., 1.]]]])

>>> np.ones([1,1,2,1])

array([[[[ 1.],

[ 1.]]]])

>>> np.ones([1,2,1,1])

array([[[[ 1.]],

[[ 1.]]]])

>>> np.ones([2,1,1,1])

array([[[[ 1.]]],

[[[ 1.]]]])

從上面**可以看出:四維時將第乙個引數設定為2和第二個引數設定為2時,輸出結果中間的空行數量不同,我把它理解成先建立1行1列的二維陣列[[ 1. ]],然後按照第2個引數打包這樣的二維陣列,如果第二個引數是2,則打包2個2維陣列變成[[[ 1. ]],[[ 1. ]]](小包),然後按照第1個引數再打包這樣的包,如果第乙個引數是2,則變成[[[[ 1. ]], [[ 1. ]]], [[[ 1. ]], [[ 1. ]]]](大包),就是下面的結果:

>>> np.ones([2,2,1,1]) 

array([[[[ 1.]],

[[ 1.]]],

[[[ 1.]],

[[ 1.]]]])

四維以上的結果也是這麼理解~輸出中區分引數用空行~

然後來看一下特定輸出:

>>> m = np.ones([2,3,2,3])  

>>> m

array([[[[ 1., 1., 1.],

[ 1., 1., 1.]],

[[ 1., 1., 1.],

[ 1., 1., 1.]],

[[ 1., 1., 1.],

[ 1., 1., 1.]]],

[[[ 1., 1., 1.],

[ 1., 1., 1.]],

[[ 1., 1., 1.],

[ 1., 1., 1.]],

[[ 1., 1., 1.],

[ 1., 1., 1.]]]])

>>> m[1,:,:,:]

array([[[ 1., 1., 1.],

[ 1., 1., 1.]],

[[ 1., 1., 1.],

[ 1., 1., 1.]],

[[ 1., 1., 1.],

[ 1., 1., 1.]]])

>>> m[:,1,:,:]

array([[[ 1., 1., 1.],

[ 1., 1., 1.]],

[[ 1., 1., 1.],

[ 1., 1., 1.]]])

>>> m[:,:,1,:]

array([[[ 1., 1., 1.],

[ 1., 1., 1.],

[ 1., 1., 1.]],

[[ 1., 1., 1.],

[ 1., 1., 1.],

[ 1., 1., 1.]]])

>>> m[:,:,:,1]

array([[[ 1., 1.],

[ 1., 1.],

[ 1., 1.]],

[[ 1., 1.],

[ 1., 1.],

[ 1., 1.]]])

然後m[1,:,:,:],:代表預設值(就是一開始你輸入時指定的值),這句**相當於輸出2個包中的第1個包(從0開始計數),這個包裡面有3個小包,小包裡面是2*3的二維陣列,所以結果就是上面的~

然後m[:,1,:,:],相當於輸出2個大包,每個大包輸出第1個小包,小包裡面是2*3的二維陣列

然後m[:,:,1,:],相當於輸出2個大包,每個大包輸出3個小包,小包裡面是二維陣列的第1行

然後m[:,:,:,1],相當於輸出2個大包,每個大包輸出3個小包,小包裡面是1*2的二維陣列

其他結果可以自己去試試~

總結:採用np.array()建立時需要幾個維度就要用幾個[

]括起來,這種建立方式要給定資料;採用np.ones()或np.zeros()建立分別產生全1或全0的資料,用a.shape會輸出你建立時的輸入,建立時輸入了幾個維度輸出就會用幾個[

]括起來,shape的返回值是乙個元組,裡面每個數字表示每一維的長度

np.shape是對應到某一維上輸出指定維的長度

筆記 對numpy中shape的理解

環境 windows,python2.7 import numpy as np a np.array 2,3,33 a array 2 3 33 print a 2 3 33 a.shape 3,一維情況中array建立的可以看做list 或一維陣列 建立時用 和 都可以,多維也一樣,兩種方法建立後...

numpy中shape和reshape的用法

1 shape 在python中匯入numpy庫之後,我們可以通過矩陣的屬性shape獲取矩陣的維度。如圖所示 import numpy as np b np.array 1,2,3,4 5,6,7,8 print b print b.shape 輸出行數和列數 print b.shape 0 輸出...

Python的numpy庫中的shape用法

shape函式是numpy.core.fromnumeric中的函式,它的功能是讀取矩陣的維度。例 shape matrixa 返回matrixa的 行數,列數 元組 shape matrixa 0 行數 shape matrixa 1 列數 shape的輸入引數可以使乙個實數,乙個一維列表 陣列 ...