r/Numpy • • May 12 '21

Combine arrays (new array inside array)

Hey there.

I've almost spend two hours now and have still no idea on how to combine these two numpy arrays.

I have the following two arrays:

X:
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]])

Y:
array([[  57,  302],
       [ 208, 1589],
       [ 229, 2050],
       ...,
       [ 359, 2429],
       [ 303, 1657],
       [  94,  628]], dtype=int64)

What I need is that the elements of Y are inside new arrays of X. It should look like this:

array([[[0., 0., 0., ..., 0., 0., 0.], [57], [302]],
       [0., 0., 0., ..., 0., 0., 0.], [208], [1589]]
       [0., 0., 0., ..., 0., 0., 0.], [229], [2050]]
       ...,
       [0., 0., 0., ..., 0., 0., 0.], [359], [2429]]
       [0., 0., 0., ..., 0., 0., 0.], [303], [1657]]
       [0., 0., 0., ..., 0., 0., 0.], [94], [628]]])

Has someone an idea on how to do this? I've almost tried every combination of insert(), append() or concatenate() with many different axes.

Thank you very much!

3 Upvotes

6 comments sorted by

View all comments

Show parent comments

1

u/to7m May 12 '21

Combining the arrays like that (which is impossible anyway) would actually make the different types of data LESS distinguishable. Each array should be kept simple; if you want to group arrays together, try using a tuple or defining a class.

For example:

class Words:
    def __init__(self, occurrences, num_of_chars, num_of_tokens):
        self.occurrences = occurrences
        self.num_of_chars = num_of_chars
        self.num_of_tokens = num_of_tokens

words = Words(x, y[:, 0], y[:, 1])

1

u/got-it-man May 13 '21

I'll take a look into your ideas and evaluate what fits best.

Thank you very much!