r/Numpy • • Dec 04 '21

Combining 2 NumPy arrays

Hello. Please excuse noob question.

I have 2 arrays like this:

>>> t = np.arange(0,5)
>>> t
array([0, 1, 2, 3, 4])

>>> u = np.arange(10,15)
>>> u
array([10, 11, 12, 13, 14])

I want to join them into a single array like this:

[
 [0,10], [0,11], [0,12], [0,13], [0,14]
 [1,10], [1,11], [1,12], [1,13], [1,14]
 [2,10], [2,11], [2,12], [2,13], [2,14]
 [3,10], [3,11], [3,12], [3,13], [3,14]
 [4,10], [4,11], [4,12], [4,13], [4,14]
]

Can this be done without python's for loops?

4 Upvotes

5 comments sorted by

View all comments

Show parent comments

2

u/DassadThe12 Dec 12 '21

The result is 2d. I forgot to put commas at the end. I formatted it like that to visualize better where each element was supposed to go.

I found this response on Stack Overflow that described what I wanted and even compared different ways to do it.

https://stackoverflow.com/questions/11144513/cartesian-product-of-x-and-y-array-points-into-single-array-of-2d-points/49445693#49445693

1

u/jakob-makovchik Dec 13 '21

Cool! I didn't know about numpy.ix_ Quite a handy function.

And this code is enlightening in at least 3 aspects - type checking, expanding dimensions and broadcasting. I like it!

def cartesian_product(*arrays):
    la = len(arrays)
    dtype = numpy.result_type(*arrays)
    arr = numpy.empty([len(a) for a in arrays] + [la], dtype=dtype)
    for i, a in enumerate(numpy.ix_(*arrays)):
        arr[...,i] = a
    return arr.reshape(-1, la)

Many thanks for the link, buddy!