numpy中改变数组维度的几种方法

在进行深度学习或强化学习时经常需要对数据的维度进行变换,本文总结了numpy中几种常用的变换数据维度的方法

增加一个维度

在多维数组的最后一维再增加一个维度可以使用numpy.reshape或numpy.expand_dims或numpy.newaxis,示例如下:

import numpy as np
import matplotlib.pyplot as plt


# 生成一个二维数据
x = np.array(range(12))
x = np.reshape(x, (3,4))
print(x)
# 输出为:
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]


# 在多维数组的最后一维再增加一个维度
y1 = np.expand_dims(x, axis=x.ndim)
y2 = np.expand_dims(x, axis=-1)
y3 = x[:,:,np.newaxis]
y4 = np.reshape(x, (*x.shape,1))
# 上述四种方法的结果完全一致
assert(np.all(y1==y2))
assert(np.all(y2==y3))
assert(np.all(y3==y4))
print(y4)
# 输出为:
# [[[ 0]
#   [ 1]
#   [ 2]
#   [ 3]]

#  [[ 4]
#   [ 5]
#   [ 6]
#   [ 7]]

#  [[ 8]
#   [ 9]
#   [10]
#   [11]]]

减小一个维度

如果多维数组的最后一维的长度为1,可以将该维去掉,去掉的方法可以使用numpy.reshape或numpy.squeeze,示例如下:

# 假设欲将刚才增加一维生成的多维数组y4的最后一维去掉
y = y4
x1 = np.squeeze(y, axis=(y.ndim-1))
x2 = np.squeeze(y)
x3 = np.squeeze(y, axis=-1)
x4 = np.reshape(y, y.shape[:-1])
# 上述四种方法的结果完全一致
assert(np.all(x1==x2))
assert(np.all(x2==x3))
assert(np.all(x3==x4))
print(x4)
# 输出为:
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

将多维数组压缩为一维数组

将多维数组压缩为一维数组,可使用flatten或ravel以及reshape方法,示例如下:

z1 = y.flatten()
z2 = y.ravel()
z3 = y.reshape(y.size)
# 上述三种方法结果完全一致
assert(np.all(z1==z2))
assert(np.all(z2==z3))
print(z3)
# 输出为:
# [ 0  1  2  3  4  5  6  7  8  9 10 11]

将多维数组压缩为二维数组,0轴保持不变

在深度学习或强化学习中,有时需要将shape为(batches, d1, d2, d3,...)的多维数组转化为shape为(batches, d1*d2*d3...)的数组,此时可以使用reshape进行转化,示例如下:

#生成多维数据
d0 = np.expand_dims(x, axis=0)
d1 = np.repeat(d0, 3, axis=0)
print(d1)
# 输出为
# [[[ 0  1  2  3]
#   [ 4  5  6  7]
#   [ 8  9 10 11]]

#  [[ 0  1  2  3]
#   [ 4  5  6  7]
#   [ 8  9 10 11]]

#  [[ 0  1  2  3]
#   [ 4  5  6  7]
#   [ 8  9 10 11]]]


#转化为二维数组
d2 = np.reshape(d1, (d1.shape[0], d1[0].size))
print(d2)
# 输出为:
# [[ 0  1  2  3  4  5  6  7  8  9 10 11]
#  [ 0  1  2  3  4  5  6  7  8  9 10 11]
#  [ 0  1  2  3  4  5  6  7  8  9 10 11]]

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在PythonNumPy添加第三个维度,可以使用reshape()函数或expand_dims()函数。 使用reshape()函数,可以将原数组重新调整为具有三个维度的新数组。例如,如果有一个二维数组a,可以使用a.reshape((n, m, 1))将其转换为一个具有三个维度数组。其n和m分别是原数组的行数和列数,1表示新的第三个维度。 示例: import numpy as np a = np.zeros((n, m)) b = a.reshape((n, m, 1)) print(b.shape) 另一种方法是使用expand_dims()函数,该函数可以在指定的轴上添加一个新的维度。例如,如果有一个二维数组a,可以使用np.expand_dims(a, axis=2)将其转换为一个具有三个维度数组。其axis=2表示在第三个维度上添加新的维度。 示例: import numpy as np a = np.zeros((n, m)) b = np.expand_dims(a, axis=2) print(b.shape) 无论使用哪种方法,都可以在NumPy添加第三个维度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Python numpy维度增减、通道叠加](https://blog.csdn.net/qq_39450134/article/details/122545049)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Numpy 改变数组维度几种方法小结](https://download.csdn.net/download/weixin_38743119/14864352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值