3

I think I'm missing something obvious. I want to find a cartesian product of arr1 (a 1d numpy array), and the ROWS of arr2 (a 2d numpy array). So, if arr1 has 4 elements and arr2 has shape (5,2), the output should have shape (20,3). (see below)

import numpy as np

arr1 = np.array([1, 4, 7, 3])

arr2 = np.array([[0, 1],
                 [2, 3], 
                 [4, 5],
                 [4, 0],
                 [9, 9]])

The desired output is:

arr3 = np.array([[1, 0, 1],
                 [1, 2, 3], 
                 [1, 4, 5],
                 [1, 4, 0],
                 [1, 9, 9],
              
                 [4, 0, 1],
                 [4, 2, 3], 
                 [4, 4, 5],
                 [4, 4, 0],
                 [4, 9, 9],
               
                 [7, 0, 1],
                 [7, 2, 3], 
                 [7, 4, 5],
                 [7, 4, 0],
                 [7, 9, 9],
              
                 [3, 0, 1],
                 [3, 2, 3], 
                 [3, 4, 5],
                 [3, 4, 0],
                 [3, 9, 9]])

I've been trying to use transpose and reshape with code like np.array(np.meshgrid(arr1,arr2)), but no success yet.

I'm hoping the solution can be generalized because I also need to deal with situations like this: Get all combinations of the ROWS of a 2d (10,2) array and the ROWS of a 2d array (20, 5) to get an output array (200,7).

3 Answers 3

1

Here is a vectorized solution that works for your general case as well:

arr1 = np.array([[1, 4], 
                 [7, 3]])

arr2 = np.array([[0, 1],
                 [2, 3], 
                 [4, 5],
                 [4, 0],
                 [9, 9]])

np.hstack((np.repeat(arr1,len(arr2),0),np.stack((arr2,)*len(arr1)).reshape(-1,arr2.shape[1])))

output of shape (2,2)*(5,2)->(10,4):

[[1 4 0 1]
 [1 4 2 3]
 [1 4 4 5]
 [1 4 4 0]
 [1 4 9 9]
 [7 3 0 1]
 [7 3 2 3]
 [7 3 4 5]
 [7 3 4 0]
 [7 3 9 9]]
Sign up to request clarification or add additional context in comments.

1 Comment

*len(arr1) uses a non-vectorized tuple repetition. Why?
1

You can use hstack to add columns to arr2, and vstack to get the final array.

np.vstack(np.apply_along_axis(lambda x: np.hstack([np.repeat(x[0], arr2.shape[0]).reshape(-1, 1), 
                                                   arr2]), 
                              1, 
                              arr1[:, None]))

Comments

0

I think this should do it:

import numpy as np

arr0 = np.array([1, 4, 7, 3])
arr1 = np.reshape(arr0, (len(arr0),1))
arr2 = np.array([[0, 1],
                 [2, 3], 
                 [4, 5],
                 [4, 0],
                 [9, 9]])

r1,c1 = arr1.shape
r2,c2 = arr2.shape
arrOut = np.zeros((r1,r2,c1+c2), dtype=arr1.dtype)
arrOut[:,:,:c1] = arr1[:,None,:]
arrOut[:,:,c1:] = arr2
arrOut.reshape(-1,c1+c2)

The output is:

array([[1, 0, 1],
       [1, 2, 3],
       [1, 4, 5],
       [1, 4, 0],
       [1, 9, 9],
       [4, 0, 1],
       [4, 2, 3],
       [4, 4, 5],
       [4, 4, 0],
       [4, 9, 9],
       [7, 0, 1],
       [7, 2, 3],
       [7, 4, 5],
       [7, 4, 0],
       [7, 9, 9],
       [3, 0, 1],
       [3, 2, 3],
       [3, 4, 5],
       [3, 4, 0],
       [3, 9, 9]])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.