深入理解numpy.array一些函数运算中的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)

print
在这里插入图片描述
这里shape的意思:

  1. 两个数字:二维
  2. 第一个数3:第0个维度有三个元素(三行)
  3. 第二个数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)

print
在这里插入图片描述
这里求最大值时,axis=0 表示第0维,也就是多维张量的最外层,0-n依次从外向内。-1代表最内层。下面分析输出:

  1. axis=0:针对第0维操作,第0维有三个元素,所以每次从3个元素中选出最大值,将总维数-1.(针对行操作,选取出每行的最大值)
  2. 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在这里插入图片描述
print解释:

  1. a_1.shape:一维向量的shape形状
  2. a_1.shape只有一个数字:一维,数字8代表这个维度有8个元素
  3. p_2 = a_1[:, np.newaxis]:在第1维增加一个维度,变为第0维8个元素,第一维1个元素。
  4. 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)

print
在这里插入图片描述
解释:

  1. axis=0时,从(8,1)这个二维向量中,针对第0维找最大值,第0维含有8个元素,所以每次从8个中寻找一个最大值。结束后降一维。
  2. 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)

print
在这里插入图片描述
在这里插入图片描述
第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)


print
在这里插入图片描述

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参数即可理解完毕

欢迎交流机器学习、深度学习问题。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

plus_left

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值