Element wise comparison between 1D and 2D array

2024/9/21 2:40:16

Want to perform an element wise comparison between an 1D and 2D array. Each element of the 1D array need to be compared (e.g. greater) against the corresponding row of 2D and a mask will be created. Here is an example:

A = np.random.choice(np.arange(0, 10), (4,100)).astype(np.float)
B = np.array([5., 4.,  8.,  2. ])

I want to do

A<B 

so that first row of A will be compared against B[0] which is 5. and the result will be an boolean array.

If I try this I get:

operands could not be broadcast together with shapes (4,100) (4,)

Any ideas?

Answer

You need to insert an extra dimension into array B:

A < B[:, None]

This allows NumPy to properly match up the two shapes for broadcasting; B now has shape (4, 1) and the dimensions can be paired up:

(4, 100)
(4,   1)

The rule is that either the dimensions have the same length, or one of the lengths needs to be 1; here 100 can be paired with 1, and 4 can be paired with 4. Before the new dimension was inserted, NumPy tried to pair 100 with 4 which raised the error.

https://en.xdnf.cn/q/72098.html

Related Q&A

Jquery ajax post request not working

I have a simple form submission with ajax, but it keeps giving me an error. All the error says is "error". No code, no description. No nothing, when I alert it when it fails.Javascript with …

Why is the main() function not defined inside the if __main__?

You can often see this (variation a):def main():do_something()do_sth_else()if __name__ == __main__:main()And I am now wondering why not this (variation b):if __name__ == __main__:do_something()do_sth_e…

Using selenium inside gitlab CI/CD

Ive desperetaly tried to set a pytest pipeline CI/CD for my personal projet hosted by gitlab. I tried to set up a simple project with two basic files: file test_core.py, witout any other dependencies f…

VSCode issue with Python versions and environments from Jupyter Notebooks

Issue: I am having issues with the environment and version of Python not matching the settings in VSCode, and causing issues with the packages I am trying to use in Jupyter notebooks. I am using a Wind…

weighted covariance matrix in numpy

I want to compute the covariance C of n measurements of p quantities, where each individual quantity measurement is given its own weight. That is, my weight array W has the same shape as my quantity ar…

How to implement biased random function?

My question is about random.choice function. As we know, when we run random.choice([apple,banana]), it will return either apple or banana with equal probabilities, what if I want to return biased resul…

Finding minimum value for each level of a multi-index dataframe

I have a DataFrame that looks like this:data a b 1 1 0.12 0.23 0.3 2 1 0.52 0.63 0.7and I want to find the minimum value for each level of a ignoring the b level, so as an output Im l…

Count occurrences of a list of substrings in a pyspark df column

I want to count the occurrences of list of substrings and create a column based on a column in the pyspark df which contains a long string.Input: ID History1 USA|UK|IND|DEN|MAL|SWE|AUS2…

What are screen units in tkinter?

I was reading the response in the link below and ran into screen units but I couldnt find what exactly was referred to by screen units in Jim Denneys response. I know they are not pixels. How do I use …

Python SUMPRODUCT of elements in nested list

I have two nested lists: a = [[1,2,3],[2,4,2]] b = [[5,5,5],[1,1,1]]I want to multiply and SUMPRODUCT each group of elements to get c = [[30],[8]]Which result from = [[1*5+2*5+3*5],[2*1,4*1,2*1]] Ive t…