I have 3 numpy arrays and you need to generate cartesian products between them. The sizes of the arrays are not fixed, so they can take different values, one example can be A = (10000, 50), B = (40, 50), C = (10000.50).
Then I do some processing (e.g. a + bc). Below is the function that I use for the product.
def cartesian_2d(arrays, out=None):
arrays = [np.asarray(x) for x in arrays]
dtype = arrays[0].dtype
n = np.prod([x.shape[0] for x in arrays])
if out is None:
out = np.empty([n, len(arrays), arrays[0].shape[1]], dtype=dtype)
m = n // arrays[0].shape[0]
out[:, 0] = np.repeat(arrays[0], m, axis=0)
if arrays[1:]:
cartesian_2d(arrays[1:], out=out[0:m, 1:, :])
for j in range(1, arrays[0].shape[0]):
out[j * m:(j + 1) * m, 1:] = out[0:m, 1:]
return out
a = [[ 0, -0.02], [1, -0.15]]
b = [[0, 0.03]]
result = cartesian_2d([a,b,a])
// array([[[ 0. , -0.02],
[ 0. , 0.03],
[ 0. , -0.02]],
[[ 0. , -0.02],
[ 0. , 0.03],
[ 1. , -0.15]],
[[ 1. , -0.15],
[ 0. , 0.03],
[ 0. , -0.02]],
[[ 1. , -0.15],
[ 0. , 0.03],
[ 1. , -0.15]]])
The output is the same as with itertools.product. However, I use my custom function to use numpy vectorized functions that work fine compared to itertools.product in my case.
After that i do
result[:, 0, :] + result[:, 1, :] - result[:, 2, :]
[-1. , 0.16],
[ 1. , -0.1 ],
[ 0. , 0.03]])
So this is the final expected result.
, . usecase , MemoryError np.empty(), .
20 , .
float, int. , , sparse .
, . memmap/h5py , .
, .
, , , , - , . , .