from:http://blog.csdn.net/by_study/article/details/67633593
環境: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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module> 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[]是對應到某一維上輸出指定維的長度