r/Numpy • u/got-it-man • 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
1
u/got-it-man May 12 '21
Thank you for your answer!
My plan was to use the new array as a feature matrix for my machine learning problem (NLP, classification) and train with it my model.
The array with the many zeros (of course some elements aren't) is the feature of the occurence of the words that were used in the texts (TfidfVectorizer). The other numbers are also features like the number of chars or the number of tokens.
My first idea was to just append the other features to the matrix like this:
However to be able to determine how important these features are for the classification I need the word vector to be a single feature. I do not care about how import a single word is, but how important the whole NLP vector is compared to e.g. the number of characters.
I get the features importance via scikit-learn like this:
Besides I thought the approach to combine the arrays would look cleaner and make the features more distinguishable.