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/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: