深入了解ny.array中的axis參數
- 一、二維矩陣下的一些運算執行個體
-
- 1.建立一個二維矩陣
- 2.二維矩陣的一些運算
- 3.将一維數組轉換成二維矩陣、n維張量
-
- 此時再看最大值函數中的axis參數
- 二、推廣到n維張量
- 三、合并運算 vstack,hstack和concatenate
-
- 1. vstack、hstack
- 2.concatenate合并
- 3.多元以此類推 axis參數即可了解完畢
一、二維矩陣下的一些運算執行個體
1.建立一個二維矩陣
# create by plusleft ############
# 2020/11/30 ############
# 建立一個二維矩陣
# a_2:a是2維
a_2 = np.array([[1, 2],
[4, 3],
[5, 0]])
# 檢視矩陣以及shape形狀
print(a_2)
print('a_2.shape:', a_2.shape)

這裡shape的意思:
- 兩個數字:二維
- 第一個數3:第0個次元有三個元素(三行)
- 第二個數2:第1個次元有兩個元素(兩列)
2.二維矩陣的一些運算
# create by plusleft ############
# 2020/11/30 ############
# 建立一個二維矩陣
a_2 = np.array([[1, 2],
[4, 3],
[5, 0]])
# 檢視矩陣以及shape形狀
print(a_2)
print('a_2.shape:', a_2.shape)
# a_2_max0:axis=0時的二維向量a的最大值
a_2_max0 = np.max(a_2, axis=0)
a_2_max1 = np.max(a_2, axis=1)
print('第0維的最大值:', a_2_max0)
print('第1維的最大值:', a_2_max1)
這裡求最大值時,axis=0 表示第0維,也就是多元張量的最外層,0-n依次從外向内。-1代表最内層。下面分析輸出:
- axis=0:針對第0維操作,第0維有三個元素,是以每次從3個元素中選出最大值,将總維數-1.(針對行操作,選取出每行的最大值)
- axis=1:針對第1維操作,第1維有兩個個元素,是以每次從2個元素中選出最大值,将總維數-1.(針對列操作,選取出每列的最大值)
3.将一維數組轉換成二維矩陣、n維張量
# create by plusleft ############
# 2020/11/30 ############
a_1 = np.array([1, 2, 3, 4, 0, 7, 5, 9])
print(a_1)
print('a_1.shape:', a_1.shape)
p_2 = a_1[:, np.newaxis]
q_2 = a_1[np.newaxis, :]
print(p_2)
print('p_2.shape:', p_2.shape)
print(q_2)
print('q_2.shape:', q_2.shape)
print解釋:
- a_1.shape:一維向量的shape形狀
- a_1.shape隻有一個數字:一維,數字8代表這個次元有8個元素
- p_2 = a_1[:, np.newaxis]:在第1維增加一個次元,變為第0維8個元素,第一維1個元素。
- q_2 = a_1[np.newaxis, :]:在第0維增加一個次元,變為第0維1個元素,原第0維變為第一維,一共8個元素。
此時再看最大值函數中的axis參數
# create by plusleft ############
# 2020/11/30 ############
a_1 = np.array([1, 2, 3, 4, 0, 7, 5, 9])
print(a_1)
print('a_1.shape:', a_1.shape)
p_2 = a_1[:, np.newaxis]
# q_2 = a_1[np.newaxis, :]
print(p_2)
print('p_2.shape:', p_2.shape)
# print(q_2)
# print('q_2.shape:', q_2.shape)
p_2_max0 = np.max(p_2, axis=0)
p_2_max1 = np.max(p_2, axis=1)
print('p_2_max0:', p_2_max0)
print('p_2_max1:', p_2_max1)
解釋:
- axis=0時,從(8,1)這個二維向量中,針對第0維找最大值,第0維含有8個元素,是以每次從8個中尋找一個最大值。結束後降一維。
- axis=1時,從(8,1)這個二維向量中,針對第1維找最大值,第1維含有1個元素,是以每次從1個中尋找一個最大值。結束後降一維。
二、推廣到n維張量
# create by plusleft ############
# 2020/11/30 ############
# a_n: n維向量a 這裡舉例取4
a_n = np.arange(1, 25).reshape(1, 2, 3, 4)
print('a_n:', a_n)
print('a_n.shape:', a_n.shape)
a_n_max0 = np.max(a_n, axis=0)
a_n_max1 = np.max(a_n, axis=1)
a_n_max2 = np.max(a_n, axis=2)
a_n_max3 = np.max(a_n, axis=3)
print('a_n_max0: ', a_n_max0)
print('a_n_max1: ', a_n_max1)
print('a_n_max2: ', a_n_max2)
print('a_n_max3: ', a_n_max3)
第3維同理
三、合并運算 vstack,hstack和concatenate
1. vstack、hstack
# create by plusleft ############
# 2020/11/30 ############
# numpy array 合并
a = np.array([1, 2, 3])
b = np.array([4, 5, 6]) # 一維ab
c = np.vstack((a, b)) # vertical stack
d = np.hstack((a, b)) # horizontal stack
print('a b vstack:\n', c)
print('a b hstack:\n', d)
2.concatenate合并
# create by plusleft ############
# 2020/11/30 ############
# numpy array 合并
a = np.array([1, 2, 3])
b = np.array([4, 5, 6]) # 一維ab
a = a[:, np.newaxis] # 變成2維 第1次元增加
b = b[:, np.newaxis] # 變成2維 第1次元增加
print(a, a.shape)
print(b, b.shape)
c = np.concatenate((a, b, b), axis=1) # 第1次元元素疊加
d = np.concatenate((a, b, b), axis=0) # 第0次元元素疊加
print('axis=1:\n', c)
print('axis=0:\n', d)
3.多元以此類推 axis參數即可了解完畢
歡迎交流機器學習、深度學習問題。