numpy 选取数组某一维度上的切片 numpy.compress

numpy.compress(condition, a, axis=None, out=None)

官方文档

Return selected slices of an array along given axis.
When working along a given axis, a slice along that axis is returned in output for each index where condition evaluates to True. When working on a 1-D array, compress is equivalent to extract.

Parameter
condition [1-D array of bools]
Array that selects which entries to return. If len(condition) is less than the size of a along the given axis, then output is truncated to the length of the condition array.

沿着相应的axis进行切片,并提取condition=1对应位置上的切片重组成新的数组

示例:

a = np.array([[1, 2], [3, 4], [5, 6]])

提取矩阵的第0行和第1行

condition=[1,1,0]
np.compress(condition,a,axis=0)

array([[1, 2],
       [3, 4]])

矩阵有3行,但如果condition的长度不到3行,那么condition自动补0

condition=[1,1]#自动补充成[1,1,0]
np.compress(condition,a,axis=0)

array([[1, 2],
       [3, 4]])

用True,False代替0,1也可

np.compress([True, True, False], a, axis=0)

array([[1, 2],
       [3, 4]])
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值