Python - 如何在两个二进制numpy数组上不,OR和使用其他逻辑运算符

时间:2022-02-27 08:40:42

I have two arrays which I use to denote whether an element is present or not, (1 = True or present and 0 = not present). Both are the same size and element one in array one corresponds to the same element in array 2. I need to do some logic calculations on the two numpy binary arrays as listed below:

我有两个数组,我用它来表示元素是否存在,(1 =真或现在,0 =不存在)。两者都是相同的大小,数组1中的元素对应于数组2中的相同元素。我需要对两个numpy二进制数组进行一些逻辑计算,如下所示:

[0,0,0,0,1,1,0]   Array 1
[0,0,0,0,1,0,1]   Array 2

I have AND working which gives me this:

我有AND工作给了我这个:

[0 0 0 0 1 0 0]   Array after AND operation 

Which tells me where for each element it is the same or present in both arrays.

这告诉我每个元素在两个数组中的位置是相同的还是存在的。

What I need is to output an array where there is a 1 in array 1 but not in array 2 (A1 & ! A2). Such as:

我需要的是输出一个数组,其中数组1中有1但数组2中没有(A1和!A2)。如:

[0,0,0,0,0,1,0]

Another where There is not a 1 in Array 1 but there is a 1 in Array 2 (! A1 &A2), Such as:

另一个数组1中没有1,但数组2中有1(!A1和A2),例如:

[0,0,0,0,0,0,1]

and where there is not a 1 in either Aray 1 or Array 2 (! A1 & ! A2)

并且在Aray 1或Array 2中没有1的地方(!A1和!A2)

[1,1,1,1,0,0,0]

Hope that makes sense, Thanks in advance.

希望有道理,先谢谢。

3 个解决方案

#1


1  

For logical not you can use:

对于逻辑而言,您可以使用:

not_array = np.logical_not(arr) + [0 for i in xrange(len(arr))]

and after that you can use the AND operation which you state you have figured out already.

然后你就可以使用你已经知道你已经弄清楚的AND操作。

P.S: np is numpy

P.S:np是numpy

#2


1  

If you have boolean arrays, you can use the ordinary bitwise operators ~ (not), | (or), & (and). They also work with ordinary integer arrays of course, but then ~0 gives -1, for example.

如果你有布尔数组,你可以使用普通的按位运算符〜(不是),| (或),&(和)。它们当然也适用于普通的整数数组,但是例如〜0给出了-1。

To create a boolean array, give the argument dtype=bool to np.array (or another Numpy function that returns arrays). Get a cast copy of an existing array by using ndarray.astype(bool).

要创建布尔数组,请将参数dtype = bool提供给np.array(或另一个返回数组的Numpy函数)。使用ndarray.astype(bool)获取现有数组的强制转换副本。

#3


0  

#What I need is to output an array where there is a 1 in array 1 but not in array 2 (A1 & ! A2)
a*(a-b)
Out[626]: array([0, 0, 0, 0, 0, 1, 0])

#Another where There is not a 1 in Array 1 but there is a 1 in Array 2 (! A1 &A2)
b*(b-a)
Out[648]: array([0, 0, 0, 0, 0, 0, 1])

#and where there is not a 1 in either Aray 1 or Array 2 (! A1 & ! A2)
1-(a|b)
Out[639]: array([1, 1, 1, 1, 0, 0, 0])

#1


1  

For logical not you can use:

对于逻辑而言,您可以使用:

not_array = np.logical_not(arr) + [0 for i in xrange(len(arr))]

and after that you can use the AND operation which you state you have figured out already.

然后你就可以使用你已经知道你已经弄清楚的AND操作。

P.S: np is numpy

P.S:np是numpy

#2


1  

If you have boolean arrays, you can use the ordinary bitwise operators ~ (not), | (or), & (and). They also work with ordinary integer arrays of course, but then ~0 gives -1, for example.

如果你有布尔数组,你可以使用普通的按位运算符〜(不是),| (或),&(和)。它们当然也适用于普通的整数数组,但是例如〜0给出了-1。

To create a boolean array, give the argument dtype=bool to np.array (or another Numpy function that returns arrays). Get a cast copy of an existing array by using ndarray.astype(bool).

要创建布尔数组,请将参数dtype = bool提供给np.array(或另一个返回数组的Numpy函数)。使用ndarray.astype(bool)获取现有数组的强制转换副本。

#3


0  

#What I need is to output an array where there is a 1 in array 1 but not in array 2 (A1 & ! A2)
a*(a-b)
Out[626]: array([0, 0, 0, 0, 0, 1, 0])

#Another where There is not a 1 in Array 1 but there is a 1 in Array 2 (! A1 &A2)
b*(b-a)
Out[648]: array([0, 0, 0, 0, 0, 0, 1])

#and where there is not a 1 in either Aray 1 or Array 2 (! A1 & ! A2)
1-(a|b)
Out[639]: array([1, 1, 1, 1, 0, 0, 0])