0 votes
1 view
ago in Data Science by (11.1k 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
ago by (22.5k points)

You have a builtin for that:


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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !