Ad

# Cartesian Product Of Rows Of A Very Big Array

## 31 May 2019 - 1 answer

I have an array of size (100, 50). I need to generate an output array which represents a cartesian product of input array rows.

For simplification purposes, let's have an input array:

``````array([[2, 6, 5],
[7, 3, 6]])
``````

As output I would like to have:

``````array([[2, 7],
[2, 3],
[2, 6],
[6, 7],
[6, 3],
[6, 6],
[5, 7],
[5, 3],
[5, 6]])
``````

Note: itertools.product doesn't work here, because of the size of the input vector. Also all another similar answers, assumes number of rows smaller than 32, what is not the case here

Ad

## Answer

This question has been asked many times, for example here.
The array of a size (100, 50) is too big and can't be handled by numpy. However, smaller array size might be solved. Anyway, I prefer to use `itertools` for this kind of stuff:

``````import itertools

a = np.array([[2, 6, 5], [7, 3, 6]])

np.array(list(itertools.product(*a)))
array([[2, 7],
[2, 3],
[2, 6],
[6, 7],
[6, 3],
[6, 6],
[5, 7],
[5, 3],
[5, 6]])
``````
Ad
source: stackoverflow.com
Ad