目前在计算机视觉中应用的数组维度最多有四维,可以表示为 (Batch_size, Row, Column, Channel)
以下将要从二维数组到四维数组进行代码的简单说明:
Tips:
1) 在numpy中所有的index都是从0开始。
2) axis = 0 对Cloumn(Width)操作; axis = 1 对Row(Height)操作; axis = 2 or -1 对Channel(Depth)操作
1. 二维数组 (Row, Column)
import numpy as np
# Set a matrix with (2*3)
array = np.array([ [1,2,3], [4,5,6] ]) print(array) [[1 2 3] [4 5 6]] print(array.shape) # (Row, Column) (2, 3) print(array[0,1]) 2
2. 三维数组 (Row, Column, Channel)
import numpy as np # Set a matrix with (2*3*4) array = 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(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(array.shape) (2, 3, 4) #(Row, Column, Channel) print(array[0,1,2]) 7
3. 四维数组(Batch_size, Row, Column, Channel)
import numpy as np # Set a matrix with (2*2*3*4) array = 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]]], [[[21,22,23,24],[17,18,19,20],[13,14,15,16]],[[9,10,11,12],[5,6,7,8],[1,2,3,4]]] ]) print(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]]] [[[21 22 23 24] [17 18 19 20] [13 14 15 16]] [[ 9 10 11 12] [ 5 6 7 8] [ 1 2 3 4]]]] print(array.shape) #(Batch_size, Row, Column, Channel) (2, 2, 3, 4) print(array[1,0,1,2]) 19 print(array[1]) # Choice Batch_size 1 [[[21 22 23 24] [17 18 19 20] [13 14 15 16]] [[ 9 10 11 12] [ 5 6 7 8] [ 1 2 3 4]]]
以上。
原文地址:https://www.cnblogs.com/godislight/p/10789642.html
时间: 2024-11-09 03:23:30