numpy学习笔记-将条件逻辑表述为数组运算

numpy.where函数是三元表达式x if condition else y的矢量化版本。假设我们有一个布尔数组和两个值数组。

xarr = np.array([1.1,1.2,1.3,1.4,1.5])
yarr = np.array([2.1,2.2,2.3,2.4,2.5])
cond = np.array([True,False,True,True,False])

假设我们想要根据cond中的值选取xarr和yarr的值:当cond中的值为true时,选取xarr的值,否则从yarr中选取。列表推导式的写法应该如下所示:

result = [(x if c else y) for x,y,c in zip(xarr,yarr,cond)]
print result
输出结果
[1.1000000000000001, 2.2000000000000002, 1.3, 1.3999999999999999, 2.5]

但是这样有几个问题。
一、它对大数组的处理速度不是很快
二、它无法用于多维数组。
若使用np.where,则可以将该功能写的特别简洁:

result = np.where(cond,xarr,yarr)
print result
输出结果
[ 1.1  2.2  1.3  1.4  2.5]

np.where第二个和第三个参数不必是数组,也可以是标量。

如下为随机数据组成的矩阵,将所有正值替换为2,所有负值替换为-2

arr = np.random.randn(4,4)
print arr
print np.where(arr>0,2,-2)
输出结果
[[ 1.80262171  0.7143772   2.28177789  0.43296688]
 [-0.71345291  0.70720256  0.09209334 -1.12625402]
 [ 1.45065299  0.55110371 -0.659351   -0.41175648]
 [ 0.4328763   0.67499992 -0.53531592 -0.38604227]]
[[ 2  2  2  2]
 [-2  2  2 -2]
 [ 2  2 -2 -2]
 [ 2  2 -2 -2]]

只将正值设置为2

print np.where(arr>0,2,arr)
输出结果
[[-1.05460459  2.          2.          2.        ]
 [ 2.          2.          2.          2.        ]
 [-0.52191645 -0.98692719  2.          2.        ]
 [-2.11448246  2.         -0.25533101 -1.07167209]]

还可以用where实现更复杂的逻辑
比如

np.where(cond1&cond2,0,
            np.where(cond1,1,
                        np.where(cond2,2,3)))

等价于

result = []
for i in range(n):
    if cond1[i] and cond2[i]:
        result.append(0)
    elif cond1[i]:
        result.append(1)
    elif cond2[i]:
        result.append(2)
    else:
        result.append(3)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值