I have a data like this,
mydata = [np.array([8,6]),
np.array([9]),
np.array([6]),
np.array([5]),
np.array([10,3])]
And an array representing the group of the data above.
path = np.array([1,2,1,2,2])
# It can be read as index 0,2 is in group1 while index 1,3,4 is in group2 for mydata.
I want to get the cartesian product with resprect to the groups at the right format.
Here is my trial with meshgrid method of the numpy .
import numpy as np
from itertools import compress
out = []
for i in np.unique(path):
out.append(list(compress(mydata, np.isin(path,i))))
[*np.array(np.meshgrid(*out)).T.reshape(-1,len(out))]
# [array([array([8, 6]), array([9])], dtype=object),
# array([array([8, 6]), array([5])], dtype=object),
# array([array([8, 6]), array([10, 3])], dtype=object),
# array([array([6]), array([9])], dtype=object),
# array([array([6]), array([5])], dtype=object),
# array([array([6]), array([10, 3])], dtype=object)]
It gives array in array together with dtype. However my expected output is the simpler version of the above:
desired = [np.array([8,6,9]),
np.array([8,6,5]),
np.array([8,6,10,3]),
np.array([6,9]),
np.array([6,5]),
np.array([6,10,3])]
By the way, I am also open to less complicated solutions. Honestly, I did not like my approach.
Thanks in advance.