Python: Concatenate all combinations of numpy array rows

In python need to combine two 2-dimensional numpy arrays, so that the resulting rows are combinations of the rows from the input arrays concatenated together. I need the fastest solution so it can be used in arrays that are very big.

For example:

I got:

import numpy as np
array1 = np.array([[1,2],[3,4]])
array2 = np.array([[5,6],[7,8]])

I want the code to return:

[[1,2,5,6]
 [1,2,7,8]
 [3,4,5,6]
 [3,4,7,8]]

Answer

Solution using numpy’s repeat, tile and hstack

The snippet

result = np.hstack([
    np.repeat(array1, array2.shape[0], axis=0),
    np.tile(array2, (array1.shape[0], 1))
])

Step by step explanation

We start with the two arrays, array1 and array2:

import numpy as np
array1 = np.array([[1,2],[3,4]])
array2 = np.array([[5,6],[7,8]])

First, we duplicate the content of array1 using repeat:

a = np.repeat(array1, array2.shape[0], axis=0)

The content of a is:

array([[1, 2],
       [1, 2],
       [3, 4],
       [3, 4]])

Then we repeat the second array, array2, using tile. In particular, (array1.shape[0],1) replicates array2 in the first direction array1.shape[0] times and just 1 time in the other direction.

b = np.tile(array2, (array1.shape[0],1))

The result is:

array([[5, 6],
       [7, 8],
       [5, 6],
       [7, 8]])

Now we can just proceed to stack the two results, using hstack:

result = np.hstack([a,b])

Achieving the desired output:

array([[1, 2, 5, 6],
       [1, 2, 7, 8],
       [3, 4, 5, 6],
       [3, 4, 7, 8]])

Leave a Reply

Your email address will not be published. Required fields are marked *