更過的Numpy教程連載內(nèi)容:http://www.reibang.com/nb/47449944
Numpy數(shù)組的形狀(shape)操作
首先补君,使用 ndarray.shape
可以返回?cái)?shù)組的形狀
改變Numpy數(shù)組形狀的方法
以下的操作均不改變原數(shù)組,而是返回一個(gè)新的更改后的數(shù)組:
-
ndarray.ravel()
: 返回展平后的數(shù)組牍白,按照一行跟這一行的方式展開喇聊。Numpy創(chuàng)建數(shù)組的一般方式也是按照先創(chuàng)建一個(gè)展平的數(shù)組然后按行變形成數(shù)組的形式,所以一般的ravel操作不會(huì)再對(duì)原數(shù)組進(jìn)行復(fù)制,但是使用其他方式創(chuàng)建的數(shù)組鼠哥,比如切片操作等就要復(fù)制一份了 -
numpy.ravel(A, order='C')
: 全局函數(shù),返回A展平后的數(shù)組 -
ndarray.reshape(ints/tuple)
: 復(fù)制一個(gè)新的數(shù)組并進(jìn)行reshape然后返回,不改變原數(shù)組朴恳,參數(shù)可以是多個(gè)整數(shù)或者一個(gè)tuple抄罕。使用多個(gè)int值作為參數(shù)時(shí),可以保留一個(gè) -1 表示不知道該處的大小是多少于颖。 -
numpy.reshape(A,tuple,order='C')
: numpy的全局方法呆贿,對(duì)A進(jìn)行reshape并返回 -
ndarray.resize(ints/tuple)
:對(duì)原數(shù)組進(jìn)行reshape,改變原數(shù)組 -
numpy.resize(A,tuple)
:對(duì)A進(jìn)行reshape森渐,改變A本身 -
ndarray.T
:返回原數(shù)組的轉(zhuǎn)置做入,不改變原數(shù)組
數(shù)組的拼接
-
縱向拼接(行長不變摞加列):
-
np.vstack(tuple)
:參數(shù)是有多個(gè)numpy數(shù)組組成的tuple -
np.row_stack(tuple)
: 等同于上邊的
a = np.array([[1,2],[3,4]]) b = np.array([[5,6],[7,8]]) print(np.vstack((a,b))) print(np.row_stack((a,b))) print(np.vstack is np.row_stack) ------ [[1 2] [3 4] [5 6] [7 8]] [[1 2] [3 4] [5 6] [7 8]] True
-
-
橫向拼接(列長不變摞加行):
-
np.hstack(tuple)
:參數(shù)是有多個(gè)numpy數(shù)組組成的tuple -
np.column_stack(tuple)
: 在處理二維數(shù)組時(shí)等同于上邊hstack的功能,處理一維數(shù)組時(shí)將多個(gè)一維數(shù)組按列拼接成二維數(shù)組同衣。只能處理一維或者二維數(shù)組母蛛,參數(shù)是多個(gè)以為或者二維數(shù)組的tuple。
a = np.array([[1,2],[3,4]]) b = np.array([[5,6],[7,8]]) print(np.hstack((a,b))) print(np.column_stack((a,b))) print(np.hstack is np.column_stack) a = np.array([4.,2.]) b = np.array([3.,8.]) print(np.column_stack((a,b))) print(np.hstack((a,b))) ------ [[1 2 5 6] [3 4 7 8]] [[1 2 5 6] [3 4 7 8]] False [[4. 3.] [2. 8.]] [4. 2. 3. 8.]
-
-
多維數(shù)組拼接
-
vstack
沿著第一個(gè)軸拼接 -
hstack
沿著第二個(gè)軸拼接 -
numpy.concatenate((a1, a2, ...), axis=0, out=None)
:指定一個(gè)軸進(jìn)行拼接
-
-
另外的簡單方式
-
numpy.r_[]
: 返回有中括號(hào)內(nèi)的多個(gè)數(shù)組沿第一個(gè)軸拼接的結(jié)果 -
numpy.c_[]
: 返回有中括號(hào)內(nèi)的多個(gè)數(shù)組沿第二個(gè)軸拼接的結(jié)果
print(np.r_[1:5,0,8]) print(np.c_[np.array([1,2,3]), np.array([4,5,6])]) print(np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]) ------ [1 2 3 4 0 8] [[1 4] [2 5] [3 6]] [[1 2 3 0 0 4 5 6]]
-
數(shù)組的分割
-
numpy.vsplit(A, indices_or_sections)
: 將A沿著第一軸分割乳怎,也就是橫著切一個(gè)二維數(shù)組 -
numpy.hsplit(A, indices_or_sections)
: 將A沿著第二軸分割彩郊,也就是豎著切一個(gè)二維數(shù)組 -
numpy.array_split(A, indices_or_sections, axis=0)
: 指定沿著axis軸分割
以上函數(shù)中第二個(gè)參數(shù)可以是整數(shù)也可以是序列(tuple、array蚪缀、list等)
-
整數(shù):表示平均分成多少份:
a = np.arange(24).reshape(4,6) print(a) print(np.vsplit(a,2)) print(np.hsplit(a,3)) ------ [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]] [array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23]])] [array([[ 0, 1], [ 6, 7], [12, 13], [18, 19]]), array([[ 2, 3], [ 8, 9], [14, 15], [20, 21]]), array([[ 4, 5], [10, 11], [16, 17], [22, 23]])]
-
tuple等序列:表示分別在第幾列后邊分割一次:
a = np.arange(24).reshape(4,6) print(a) print(np.vsplit(a,(2,3,4))) print(np.hsplit(a,(2,3,5))) ------ [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]] [array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]]), array([[12, 13, 14, 15, 16, 17]]), array([[18, 19, 20, 21, 22, 23]]), array([], shape=(0, 6), dtype=int32)] [array([[ 0, 1], [ 6, 7], [12, 13], [18, 19]]), array([[ 2], [ 8], [14], [20]]), array([[ 3, 4], [ 9, 10], [15, 16], [21, 22]]), array([[ 5], [11], [17], [23]])]
給數(shù)組新增維度
-
使用
np.newaxis
給數(shù)組增加一個(gè)維度
np.newaxis
每使用一次都可以給數(shù)組增加一個(gè)維度秫逝,只要在需要拓展的維度使用np.newaxis
作為索引進(jìn)行切片操作就可以了:>>>a = np.array([1, 2, 3, 4, 5, 6]) >>> a.shape (6,) >>>a2 = a[np.newaxis, :] >>> a2.shape (1, 6)
-
使用
np.expand_dims
給數(shù)組增加一個(gè)維度
np.expand_dims(A,axis=?)
可以對(duì)傳入的A數(shù)組增加一個(gè)指定的axis
新軸,從而擴(kuò)展數(shù)組的維度:>>> a = np.array([1, 2, 3, 4, 5, 6]) >>> a.shape (6,) >>> b = np.expand_dims(a, axis=1) >>> b.shape (6, 1) >>> c = np.expand_dims(a, axis=0) >>> c.shape (1, 6)