赞
踩
Numpy的array数组补0操作 #二维数组row=3,col=2(3,2) data1 = np.array([[1,2],[3,4],[5,6]]) #补0模式,一维位置前后补(一层)0,二维位置前后补(一层)0 pad_width1 = ((1,1),(1,1)) data1_p = np.pad(data1, pad_width=pad_width1, mode='constant', constant_values=0) print(data1_p) >>>[[0 0 0 0] [0 1 2 0] [0 3 4 0] [0 5 6 0] [0 0 0 0]] #更好理解可体现在索引的操作上面,在(3,2)索引上(3+2,2+2)=(5,4) print(np.shape(data1_p)) >>>(5,4) #三维数组heights=2,weights=2,channels=2 data2 = np.array([[[1,2],[3,4]],[[5,6],[7,8]]]) print(np.shape(data2)) >>>(2,2,2) print(data2) >>>[[[1 2] [3 4]] [[5 6] [7 8]]] #分别在height,weights,channels的前后补(加一层)0 #直接看结果有点难理解,结合补0前后索引的变化和三维空间立方体更容易理解! pad_width2 = ((1, 1), (0, 0),(0, 0)) data2_p = np.pad(data2, pad_width=pad_width2, mode='constant', constant_values=0) print(np.shape(data2_p)) print(data2_p) >>>(4, 2, 2) >>>[[[0 0] [0 0]] [[1 2] [3 4]] [[5 6] [7 8]] [[0 0] [0 0]]]
pad_width3 = ((0, 0), (1, 1),(0, 0)) data3_p = np.pad(data2, pad_width=pad_width3, mode='constant', constant_values=0) print(np.shape(data3_p)) print(data3_p) >>>(2, 4, 2) >>>[[[0 0] [1 2] [3 4] [0 0]] [[0 0] [5 6] [7 8] [0 0]]] pad_width4 = ((0, 0), (0, 0),(1, 1)) data4_p = np.pad(data2, pad_width=pad_width4, mode='constant', constant_values=0) print(np.shape(data4_p)) print(data4_p) >>>(2, 2, 4) >>>[[[0 1 2 0] [0 3 4 0]] [[0 5 6 0] [0 7 8 0]]]
重点理解比较下面两种padding位置的区别 data3 = np.array([[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], [[[13,14,15], [16,17,18]],[[19,20,21], [22,23,24]]]]) print(np.shape(data3)) print(data3) >>>(2, 2, 2, 3) >>>[[[[ 1 2 3] [ 4 5 6]] [[ 7 8 9] [10 11 12]]] [[[13 14 15] [16 17 18]] [[19 20 21] [22 23 24]]]] #height位置前后补(加一层)0,weight位置前后补(加一层)0, pad_width5 = ((0, 0), (1, 1),(1, 1),(0, 0)) data5_p = np.pad(data3, pad_width=pad_width5, mode='constant', constant_values=0) print(np.shape(data5_p)) print(data5_p) >>>(2, 4, 4, 3) >>>[[[[ 0 0 0] [ 0 0 0] [ 0 0 0] [ 0 0 0]] [[ 0 0 0] [ 1 2 3] [ 4 5 6] [ 0 0 0]] [[ 0 0 0] [ 7 8 9] [10 11 12] [ 0 0 0]] [[ 0 0 0] [ 0 0 0] [ 0 0 0] [ 0 0 0]]] [[[ 0 0 0] [ 0 0 0] [ 0 0 0] [ 0 0 0]] [[ 0 0 0] [13 14 15] [16 17 18] [ 0 0 0]] [[ 0 0 0] [19 20 21] [22 23 24] [ 0 0 0]] [[ 0 0 0] [ 0 0 0] [ 0 0 0] [ 0 0 0]]]] data4 = np.array([[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]], [[13,14,15], [16,17,18]],[[19,20,21], [22,23,24]]]]) print(np.shape(data4)) print(data4) >>>(1, 4, 2, 3) >>>[[[[ 1 2 3] [ 4 5 6]] [[ 7 8 9] [10 11 12]] [[13 14 15] [16 17 18]] [[19 20 21] [22 23 24]]]] pad_width6 = ((0, 0), (1, 1),(1, 1),(0, 0)) #height位置前后补(加一层)0,weight位置前后补(加一层)0, data6_P = np.pad(data4, pad_width=pad_width6, mode='constant', constant_values=0) print(np.shape(data6_P)) print(data6_P) >>>(1, 6, 4, 3) >>>[[[[ 0 0 0] [ 0 0 0] [ 0 0 0] [ 0 0 0]] [[ 0 0 0] [ 1 2 3] [ 4 5 6] [ 0 0 0]] [[ 0 0 0] [ 7 8 9] [10 11 12] [ 0 0 0]] [[ 0 0 0] [13 14 15] [16 17 18] [ 0 0 0]] [[ 0 0 0] [19 20 21] [22 23 24] [ 0 0 0]] [[ 0 0 0] [ 0 0 0] [ 0 0 0] [ 0 0 0]]]]
- #channel横切面channel = 0的位置
- print((data3[:,:,:,0]))
- >>>[[[ 0 0 0 0]
- [ 0 1 4 0]
- [ 0 7 10 0]
- [ 0 13 16 0]
- [ 0 19 22 0]
- [ 0 0 0 0]]]
0 0 0 0 0 1 4 0 0 7 10 0 0 13 16 0 0 19 23 0 0 0 0 0 #weight位置前后补(加一层)0,channel位置前后补(加一层)0, pad_width7 = ((0, 0), (0, 0),(1, 1),(1, 1)) data7_P = np.pad(data4, pad_width=pad_width7, mode='constant', constant_values=0) print(np.shape(data6_P)) print(data7_P) >>>(1, 4, 4, 5) >>>[[[[ 0 0 0 0 0] [ 0 1 2 3 0] [ 0 4 5 6 0] [ 0 0 0 0 0]] [[ 0 0 0 0 0] [ 0 7 8 9 0] [ 0 10 11 12 0] [ 0 0 0 0 0]] [[ 0 0 0 0 0] [ 0 13 14 15 0] [ 0 16 17 18 0] [ 0 0 0 0 0]] [[ 0 0 0 0 0] [ 0 19 20 21 0] [ 0 22 23 24 0] [ 0 0 0 0 0]]]]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。