軸含義說明
axis的含義:由此可以看出,通過不同的axis,numpy會沿着不同的方向進行操作:
如果不設置,那麼對所有的元素操作;如果axis=0,則沿着縱軸進行操作;axis=1,則沿着橫軸進行操作。
但這只是簡單的二位數組,如果是多維的呢?可以總結爲一句話:
設axis=i,則numpy沿着第i個下標變化的放向進行操作。
例如剛剛的例子,可以將表示爲:
import numpy as np
data = np.array([[1,2],[3,4]])
data = [
[a00, a01],
[a10, a11]
],
所以axis=0時,沿着第0個下標變化的方向進行操作,也就是a00->a10, a01->a11,
也就是縱座標的方向,axis=1時也類似。下面我們舉一個四維的求sum的例子來驗證一下:
data = np.random.randint(0, 5, [4,3,2,3])
print data
[[[[4 3 0]
[0 4 0]]
[[3 2 1]
[2 0 3]]
[[2 2 4]
[1 2 0]]]
[[[0 2 4]
[2 4 0]]
[[4 2 4]
[0 4 0]]
[[3 0 3]
[4 1 1]]]
[[[0 2 0]
[3 3 3]]
[[4 3 2]
[3 2 3]]
[[0 2 4]
[0 3 2]]]
[[[2 2 0]
[0 2 2]]
[[2 3 3]
[1 2 4]]
[[0 1 0]
[1 0 1]]]]
print "axis=0"
#numpy驗證第0維的方向來求和,也就是第一個元素值=a0000+a1000+a2000+a3000=11,
#第二個元素=a0001+a1001+a2001+a3001=5,同理可得最後的結果如下:
print data.sum(axis=0)
print "axis=1"
print data.sum(axis=1)
print "axis=2"
print data.sum(axis=2)
#當axis=3時,numpy驗證第3維的方向來求和,也就是第一個元素值=a0000+a0001+a0002=5,
#第二個元素=a0010+a0011+a0012=7,同理可得最後的結果如下:
print "axis=3"
print data.sum(axis=3)
axis=0
[[[13 6 9]
[ 7 4 5]]
[[12 9 11]
[ 6 10 1]]
[[ 8 13 12]
[ 9 6 6]]]
axis=1
[[[10 9 8]
[ 5 3 4]]
[[ 9 7 10]
[ 5 4 2]]
[[ 9 5 10]
[ 8 5 3]]
[[ 5 7 4]
[ 4 8 3]]]
axis=2
[[[5 4 3]
[5 1 3]
[5 7 6]]
[[4 1 4]
[4 7 4]
[6 3 4]]
[[7 2 4]
[5 4 3]
[5 4 6]]
[[4 3 3]
[4 7 2]
[1 5 2]]]
axis=3
[[[ 9 3]
[ 8 1]
[10 8]]
[[ 5 4]
[12 3]
[ 9 4]]
[[ 9 4]
[ 6 6]
[ 9 6]]
[[ 5 5]
[ 6 7]
[ 5 3]]]
相關矩陣堆疊(concatenate, hstack, vstack, dstack, stack)用法的說明
arr=[np.random.randn(2,3) for _ in range(5)]
print arr
print arr[0].shape
[array([[-0.80612405, 0.31055887, -0.442608 ],
[-0.53218701, 1.73229849, 0.87374842]]), array([[ 2.30505457, 0.2061082 , -1.02476858],
[ 0.01919642, -0.62611687, -0.35686779]]), array([[ 1.8560166 , 0.61116091, 0.80456681],
[-0.11137362, -1.22763138, -0.4134618 ]]), array([[-0.6849199 , 0.09698213, 0.82980694],
[-1.12956127, -0.33650792, 0.34616903]]), array([[ 1.78424831, 0.09591247, -0.19971545],
[ 0.62067279, -1.21686687, -2.0698592 ]])]
(2, 3)
np.stack
將兩個矩陣沿着一個新指定的軸堆起來
更專業的英語說明:
np.stack
Join a sequence of arrays along a new axis.
The axis parameter specifies the index of the new axis in the dimensions of the result.
For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.
print "axis=0"
print np.stack(arr,axis=0)
print "previous: "+str(arr[0].shape)+" now: "+str(np.stack(arr,axis=0).shape)
print "axis=1"
print np.stack(arr,axis=1)
print "previous: "+str(arr[0].shape)+" now: "+str(np.stack(arr,axis=1).shape)
print "axis=2"
print np.stack(arr,axis=2)
print "previous: "+str(arr[0].shape)+" now: "+str(np.stack(arr,axis=2).shape)
axis=0
[[[-0.80612405 0.31055887 -0.442608 ]
[-0.53218701 1.73229849 0.87374842]]
[[ 2.30505457 0.2061082 -1.02476858]
[ 0.01919642 -0.62611687 -0.35686779]]
[[ 1.8560166 0.61116091 0.80456681]
[-0.11137362 -1.22763138 -0.4134618 ]]
[[-0.6849199 0.09698213 0.82980694]
[-1.12956127 -0.33650792 0.34616903]]
[[ 1.78424831 0.09591247 -0.19971545]
[ 0.62067279 -1.21686687 -2.0698592 ]]]
previous: (2, 3) now: (5, 2, 3)
axis=1
[[[-0.80612405 0.31055887 -0.442608 ]
[ 2.30505457 0.2061082 -1.02476858]
[ 1.8560166 0.61116091 0.80456681]
[-0.6849199 0.09698213 0.82980694]
[ 1.78424831 0.09591247 -0.19971545]]
[[-0.53218701 1.73229849 0.87374842]
[ 0.01919642 -0.62611687 -0.35686779]
[-0.11137362 -1.22763138 -0.4134618 ]
[-1.12956127 -0.33650792 0.34616903]
[ 0.62067279 -1.21686687 -2.0698592 ]]]
previous: (2, 3) now: (2, 5, 3)
axis=2
[[[-0.80612405 2.30505457 1.8560166 -0.6849199 1.78424831]
[ 0.31055887 0.2061082 0.61116091 0.09698213 0.09591247]
[-0.442608 -1.02476858 0.80456681 0.82980694 -0.19971545]]
[[-0.53218701 0.01919642 -0.11137362 -1.12956127 0.62067279]
[ 1.73229849 -0.62611687 -1.22763138 -0.33650792 -1.21686687]
[ 0.87374842 -0.35686779 -0.4134618 0.34616903 -2.0698592 ]]]
previous: (2, 3) now: (2, 3, 5)
np.hstack, np.vstack, np.dstack
- np.hstack
將兩個矩陣水平方向(行所在的軸)堆起來Stack arrays in sequence horizontally (column wise),
等同於 np.concatenate(arr,axis=1) - np.vstack
矩陣沿着垂直的方向堆疊,Stack arrays in sequence vertically (row wise)
等同於 np.concatenate(arr,axis=0) - np.dstack
矩陣沿着深度方向堆疊,即第三個軸 Stack arrays in sequence depth wise (along third dimension)
print np.hstack(arr)
print "previous: "+str(arr[0].shape)+" now: "+str(np.hstack(arr).shape)
print np.vstack(arr)
print "previous: "+str(arr[0].shape)+" now: "+str(np.vstack(arr).shape)
print np.dstack(arr)
print "previous: "+str(arr[0].shape)+" now: "+str(np.dstack(arr).shape)
[[-0.80612405 0.31055887 -0.442608 2.30505457 0.2061082 -1.02476858
1.8560166 0.61116091 0.80456681 -0.6849199 0.09698213 0.82980694
1.78424831 0.09591247 -0.19971545]
[-0.53218701 1.73229849 0.87374842 0.01919642 -0.62611687 -0.35686779
-0.11137362 -1.22763138 -0.4134618 -1.12956127 -0.33650792 0.34616903
0.62067279 -1.21686687 -2.0698592 ]]
previous: (2, 3) now: (2, 15)
[[-0.80612405 0.31055887 -0.442608 ]
[-0.53218701 1.73229849 0.87374842]
[ 2.30505457 0.2061082 -1.02476858]
[ 0.01919642 -0.62611687 -0.35686779]
[ 1.8560166 0.61116091 0.80456681]
[-0.11137362 -1.22763138 -0.4134618 ]
[-0.6849199 0.09698213 0.82980694]
[-1.12956127 -0.33650792 0.34616903]
[ 1.78424831 0.09591247 -0.19971545]
[ 0.62067279 -1.21686687 -2.0698592 ]]
previous: (2, 3) now: (10, 3)
[[[-0.80612405 2.30505457 1.8560166 -0.6849199 1.78424831]
[ 0.31055887 0.2061082 0.61116091 0.09698213 0.09591247]
[-0.442608 -1.02476858 0.80456681 0.82980694 -0.19971545]]
[[-0.53218701 0.01919642 -0.11137362 -1.12956127 0.62067279]
[ 1.73229849 -0.62611687 -1.22763138 -0.33650792 -1.21686687]
[ 0.87374842 -0.35686779 -0.4134618 0.34616903 -2.0698592 ]]]
previous: (2, 3) now: (2, 3, 5)
np.concatenate用法
將幾個矩陣沿着不同的軸堆起來 Join a sequence of arrays along an existing axis.
print "axis=0"
print np.concatenate(arr,axis=0)
print "previous: "+str(arr[0].shape)+" now: "+str(np.concatenate(arr,axis=0).shape)
print "axis=1"
print np.concatenate(arr,axis=1)
print "previous: "+str(arr[0].shape)+" now: "+str(np.concatenate(arr,axis=1).shape)
axis=0
[[-0.80612405 0.31055887 -0.442608 ]
[-0.53218701 1.73229849 0.87374842]
[ 2.30505457 0.2061082 -1.02476858]
[ 0.01919642 -0.62611687 -0.35686779]
[ 1.8560166 0.61116091 0.80456681]
[-0.11137362 -1.22763138 -0.4134618 ]
[-0.6849199 0.09698213 0.82980694]
[-1.12956127 -0.33650792 0.34616903]
[ 1.78424831 0.09591247 -0.19971545]
[ 0.62067279 -1.21686687 -2.0698592 ]]
previous: (2, 3) now: (10, 3)
axis=1
[[-0.80612405 0.31055887 -0.442608 2.30505457 0.2061082 -1.02476858
1.8560166 0.61116091 0.80456681 -0.6849199 0.09698213 0.82980694
1.78424831 0.09591247 -0.19971545]
[-0.53218701 1.73229849 0.87374842 0.01919642 -0.62611687 -0.35686779
-0.11137362 -1.22763138 -0.4134618 -1.12956127 -0.33650792 0.34616903
0.62067279 -1.21686687 -2.0698592 ]]
previous: (2, 3) now: (2, 15)