Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I want to pairwise and compare (with <=) all elements of two NumPy ndarrays A and B, where both arrays can have arbitrary dimensions m and n, such that the result is an array of dimension m + n.

I know how to do it forgiven dimension of B.

  1. The scalar: A <= B
  2. The one-dimensional: A[..., np.newaxis] <= B
  3. The two-dimensional: A[..., np.newaxis, np.newaxis] <= B

I am trying to insert as many as np.newaxis as there are dimensions in the second array.

Do I have any syntax like np.newaxis * B.ndim, or another way?

1 Answer

0 votes
by (36.8k points)

You have a builtin for that:

np.less_equal.outer(A,B)

Another way would be with reshaping to accomodate new axes -

A.reshape(list(A.shape)+[1]*B.ndim) <= B

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Browse Categories

...