python | np.squeeze()函数

参考:https://blog.csdn.net/zenghaitao0128/article/details/78512715

语法:numpy.squeeze(a,axis = None)

 1)a表示输入的数组;
 2)axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
 3)axis的取值可为None 或 int 或 tuple of ints, 可选。若axis为空,则删除所有单维度的条目;
 4)返回值:数组
 5) 不会修改原数组;

作用:从数组的形状中合并单维度条目,即把shape中为1的维度去掉。如果没写axis就合并所有的维度为1的条目,否则只合并axis的那个。

例1:

a  = np.arange(10).reshape(1,10)
print(a.shape)
b = np.squeeze(a)
print(b.shape)

#output
(1, 10)
(10,)

例2:

a  = np.arange(10).reshape(2,5)
b = np.squeeze(a)

#output
无任何变化

例3:

a = np.arange(10).reshape(1,2,5)
print(d.shape)
b = np.squeezy(a)
print(b.shape)
print(a)
print(b)

#output:
(1, 2, 5)

(2, 5)

array([[[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]]])

array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

例4:

a = np.arange(10).reshape(1,10,1)
print(a)
b = np.squeezy(a)
print(b)
c = np.squeezy(a, axis = 0)
print(c)
e = np.squeezy(a, axis = 2)
print(e)
#会报错
d = np.squeezy(a, axis = 1) 
print(d)

#output:
a:
array([[[0],
        [1],
        [2],
        [3],
        [4],
        [5],
        [6],
        [7],
        [8],
        [9]]])

b:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

c:
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])

e:
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])



 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值