【Python】numpy方法合辑-数组重塑

(一)reshape

numpy.reshape(a, newshape, order='C')
#在不更改数据的情况下为数组提供新形状
#注意:根据order决定返回视图 or 副本,order 与原数组一致,则返回视图,否则返回副本

# 参数
"""
newshape:新形状的定义,int或int的元组 
如果是整数,则结果将是该长度的一维数组。一个形状维度可以是-1。在这种情况下,将根据数组的长度和剩余维度推断该值。
order :读取元素的顺序 , ‘C’ 按照行方向读取,‘F’按照列方向读取
"""

# demo
x = np.array([[3.0, 5.0, np.nan], [7.0, 5.0, 6.0], [1.0, -9.0, 11.0]])
x_C = np.reshape(x, (9, -1), order='C')
x_C[0] = 10
print(x_C.base is x) # True
print(x)

x_F = np.reshape(x, (9, -1), order='F')
x_F[0] = -10
print(x_F.base is x) # False
print(x)

(二)resize

ndarray.resize(new_shape, refcheck=True)
#更改原数组自身的形状和大小
#注意:过程分为两步
#1,按照order,将数组被展平成一维。
#2,按照new_shape调整大小和形状,缺失值用 0 补齐。

# 参数
# new_shape:tuple of ints, or n ints 调整大小的数组的形状

# demo
x = np.array([[3.0, 5.0, np.nan], [7.0, 5.0, 6.0], [1.0, -9.0, 11.0]])
x.resize((4, 4))
print(x)
"""
[[ 3.  5. nan  7.]
 [ 5.  6.  1. -9.]
 [11.  0.  0.  0.]
 [ 0.  0.  0.  0.]]   # 0 补齐缺失数据
"""

(三)transpose

ndarray.transpose(*axes)
#交换数组轴的位置,并返回原数组视图
#参数
# axes:如果给定axes,则其顺序指示轴的排列方式,否则,反转轴的顺序,相当于ndarray.T。

# demo
x = np.arange(16).reshape((2, 2, 4))
print(x[0, 1, 3])  # 7 第一个维度为0,第二个维度为1,第三个维度3
x_t = x.transpose()  # 反转轴的顺序 2->0,1->1,0->2  维度交换,相当于ndarray.T
print(x_t[3, 1, 0]) # 7  第一个维度为3,第二个维度为1,第三个维度0

x_t = x.transpose((1, 2, 0))  # 轴交换 :1->0,2->1,0->2 
print(x_t[1, 3, 0]) # 7  第一个维度为1,第二个维度为3,第三个维度0

(四)swapaxes

numpy.swapaxes(a, axis1, axis2)
#交换axis1和axis2两个轴,该方法与transpose类似,返回原数组的视图

# demo
x = np.arange(16).reshape((2, 2, 4))
x_t = np.swapaxes(x, 1, 2)  # 交换1轴和2轴  同 x.transpose(0,2,1)
print(x[0, 1, 3])  # 7
print(x_t[0, 3, 1])  # 7
x_t[0, 3, 1] = 10   # 因为是视图,会修改原来的数据
print(x[0, 1, 3])  # 10 

(五)flatten

ndarray.flatten(order='C')
# 返回展平成一维的原数组副本,无论order是否相同,都是返回副本
# 参数
# order:展开的顺序,默认C顺序,按行展开

# demo
x = np.array([[ 1,  1, -1],
       [ 0, -3,  6]])
# flatten
x_F = x.flatten('F')
print(x_F.base is x)  # False  返回副本
x_C = x.flatten('C')
print(x_C.base is x)  # False 返回副本

(六)ravel

numpy.ravel(a, order='C')
#返回一个连续的扁平数组。
#注意:与flatten类似,都是扁平数组,但是flatten中无论order是否相同,都是返回副本。而ravel根据order决定返回视图 or 副本,order与原数组一致,则返回视图,否则返回副本

#demo
x = np.array([[1, 1, -1],
              [0, -3, 6]])
x_C = np.ravel(x, order='C')
print(x_C.base is x)  # True   order与原数组一致,则返回视图
x_F = np.ravel(x, order='F')
print(x_F.base is x)  # False order与原数组不一致,则返回副本

(七)squeeze

numpy.squeeze(a, axis=None)
#从数组的形状中删除单维条目,即把shape中为1的维度去掉,返回原数组视图
#如果axis=None,默认删除shape中为1的维度,若删除shape中不为1的维度,则报错

# demo
x = np.array([[[0], [1], [2]]])
print(x)
"""
x=

[[[0]
  [1]
  [2]]]
"""
print(x.shape)  # (1, 3, 1)
x1 = np.squeeze(x)  # 从数组的形状中删除单维条目,即把shape中为1的维度去掉
print(x1)  # [0 1 2]
print(x1.shape)  # (3,)
print(x1.base is x) # True   #返回原数组视图

x = np.array([[[0], [1], [2]]])
x1 = np.squeeze(x,axis=1)
# ValueError: cannot select an axis to squeeze out which has size not equal to one
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值