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

来源:互联网 发布:如何设置网络交换机 编辑:程序博客网 时间:2024/06/08 01:04

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 arrprint 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)
0 0
原创粉丝点击