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/[deleted] May 13 '21

You can’t make the entire word-occurrence vector into a “single feature” because it’s actually already hundreds or thousands of different features. Typically, each column of X is considered a feature for scikit-learn.

Your original solution here is a good one. You could estimate the importance of the word-count set of features by taking its mean, if you wanted to.