Compare a numpy array to each element of another one - python

A = np.array([5,1,5,8])
B = np.array([2,5])
I want to compare the A array to each element of B. In other words I'm lookin for a function which do the following computations :
A>2
A>5
(array([ True, False, True, True]), array([False, False, False, True]))

Not particularly fancy but a list comprehension will work:
[A > b for b in B]
[array([ True, False, True, True], dtype=bool),
array([False, False, False, True], dtype=bool)]
You can also use np.greater(), which requires the dimension-adding trick that Brenlla uses in the comments:
np.greater(A, B[:,np.newaxis])
array([[ True, False, True, True],
[False, False, False, True]], dtype=bool)

Related

How to intersect boolean subarrays for True values?

I know that Numpy provides logical_and() which allows us to intersect two boolean arrays for True values only (True and True would yield True while True and False would yield False). For example,
a = np.array([True, False, False, True, False], dtype=bool)
b = np.array([False, True, True, True, False], dtype=bool)
np.logical_and(a, b)
> array([False, False, False, True, False], dtype=bool)
However, I'm wondering how I can apply this to two subarrays in an overall array? For example, consider the array:
[[[ True, True], [ True, False]], [[ True, False], [False, True]]]
The two subarrays I'm looking to intersect are:
[[ True, True], [ True, False]]
and
[[ True, False], [False, True]]
which should yield:
[[ True, False], [False, False]]
Is there a way to specify that I want to apply logical_and() to the outermost subarrays to combine the two?
You can use .reduce() along the first axis:
>>> a = np.array([[[ True, True], [ True, False]], [[ True, False], [False, True]]])
>>> np.logical_and.reduce(a, axis=0)
array([[ True, False],
[False, False]])
This works even when you have more than two "sub-arrays" in your outer array. I prefer this over the unpacking approach because it allows you to apply your function (np.logical_and) over any axis of your array.
If I understand your question correctly, you are looking to do:
import numpy as np
output = np.logical_and(a[:, 0], a[:, 1])
This simply slices your arrays so that you can use logical_and the way your results suggest.

Comparing and flipping adjacent values in numpy

I want to compare adjacent values in a (potentially multi-dimensional) bool numpy array such that if there are adjacent True values in a row, only the leftmost would be kept while the rest would be flipped to False. For example:
Input: [True, False, False, True]
Output: [True, False, False, True]
Input: [True, True, False, True]
Output: [True, False, False, True]
Input: [True, True, True, True]
Output: [True, False, False, False]
Is there an efficient (i.e. vectorized) way of achieving this in NumPy, SciPy, or TensorFlow?
You can calculate the logical_and of the array with its shifted version, if both true, flip the values:
a[np.concatenate(([False], a[:-1])) & a] = False
Testing:
a = np.array([True, True, True, True])
a[np.concatenate(([False], a[:-1])) & a] = False
a
# array([ True, False, False, False], dtype=bool)
a = np.array([True, True, False, True])
a[np.concatenate(([False], a[:-1])) & a] = False
a
# array([ True, False, False, True], dtype=bool)
a = np.array([True, False, False, True])
a[np.concatenate(([False], a[:-1])) & a] = False
a
# array([ True, False, False, True], dtype=bool)
For a 1-d array:
a = np.array([True, True, False, True])
b = np.diff(a)
a[1:] = np.logical_and(a[1:], b)
>>> a
array([ True, False, False, True], dtype=bool)
>>>

Intersect two boolean arrays for True

Having the numpy arrays
a = np.array([ True, False, False, True, False], dtype=bool)
b = np.array([False, True, True, True, False], dtype=bool)
how can I make the intersection of the two so that only the True values match? I can do something like:
a == b
array([False, False, False, True, True], dtype=bool)
but the last item is True (understandably because both are False), whereas I would like the result array to be True only in the 4th element, something like:
array([False, False, False, True, False], dtype=bool)
Numpy provides logical_and() for that purpose:
a = np.array([ True, False, False, True, False], dtype=bool)
b = np.array([False, True, True, True, False], dtype=bool)
c = np.logical_and(a, b)
# array([False, False, False, True, False], dtype=bool)
More at Numpy Logical operations.

Elementwise comparison of numpy arrays with different lengths

I want to compare the elements of two 3D numpy arrays of different lengths. The goal is, to find overlapping elements in the two arrays.
All functions I found so far, rely on the two arrays being of the same lengths.
Is there an efficient way to do compare the 2D-elements (for loops will be very inefficient, since each array has tens of thousands of elements)?
Here a few ways of comparing 2 1d arrays:
In [325]: n=np.arange(0,10)
In [326]: m=np.arange(3,9)
In [327]: np.in1d(n,m)
Out[327]: array([False, False, False, True, True, True, True, True, True, False], dtype=bool)
In [328]: np.in1d(m,n)
Out[328]: array([ True, True, True, True, True, True], dtype=bool)
In [329]: n[:,None]==m[None,:]
Out[329]:
array([[False, False, False, False, False, False],
[False, False, False, False, False, False],
[False, False, False, False, False, False],
[ True, False, False, False, False, False],
[False, True, False, False, False, False],
[False, False, True, False, False, False],
[False, False, False, True, False, False],
[False, False, False, False, True, False],
[False, False, False, False, False, True],
[False, False, False, False, False, False]], dtype=bool)
and farenorths suggestion
In [330]: np.intersect1d(n,m)
Out[330]: array([3, 4, 5, 6, 7, 8])
In [331]: np.where(np.in1d(n,m))
Out[331]: (array([3, 4, 5, 6, 7, 8], dtype=int64),)
Is intersect1d what you want? For example, if your arrays are a and b, you could simply do:
duplicates = np.intersect1d(a, b)

Populate numpy matrix dynamically from array values?

I'm trying to dynamically construct a 2-D matrix with numpy based on the values of an array, like this:
In [113]: A = np.zeros((5,5),dtype=bool)
In [114]: A
Out[114]: array([[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False],
[False, False, False, False, False]], dtype=bool)
In [116]: B = np.array([0,1,3,0,2])
In [117]: B
Out[117]: array([0, 1, 3, 0, 2])
Now, I'd like to use the values of B to assign the first n values of each row to A to True. For this A and B, the correct output would be:
In [118]: A
Out[118]: array([[False, False, False, False, False],
[ True, False, False, False, False],
[ True, True, True, False, False],
[False, False, False, False, False],
[ True, True, False, False, False]], dtype=bool)
The length of B will always equal the number of rows of A, and the the values of B will always be less than or equal to the number of columns of A. The size of A and the values of B are constantly changing, so I need to build these on the fly.
I'm certain that this has a simple(-ish) solution in numpy, but I've spent the last hour banging my head against variations of repeat, tile, and anything else I can think of. Can anyone help me out before I give myself a concussion? :)
EDIT: I'm going to need to do this a lot, so speed will be an issue. The only version that I can come up with for now is something like:
np.vstack([ [True]*x + [False]*(500-x) for x in B ])
but I expect that this will be slow due to the for loop (I would time it if I had anything to compare it to).
How about:
>>> A = np.zeros((5, 7),dtype=bool)
>>> B = np.array([0,1,3,0,2])
>>> (np.arange(len(A[0])) < B[:,None])
array([[False, False, False, False, False, False, False],
[ True, False, False, False, False, False, False],
[ True, True, True, False, False, False, False],
[False, False, False, False, False, False, False],
[ True, True, False, False, False, False, False]], dtype=bool)
(I changed the shape from (5,5) because I was getting confused about which axis was which, and I wanted to make sure I was using the right one.)
[Simplified from (np.arange(len(A[0]))[:,None] < B).T -- if we expand B and not A, there's no need for the transpose.]

Categories