Concatenating arrays in python using numpy

numpy
python

#1

I am trying to arrange the arrays described as follows in a 2-dimensional array using the concatenate function in numpy. I tried changing the axis argument. I am always getting a 1-d array and not a 2-d array.

>>> import numpy as np
x=np.array([1,2,3])
y = np.array([18,7,6])
z = np.array([1,3,5])
>> np.concatenate((x,y,z))
array([ 1,  2,  3, 18,  7,  6,  1,  3,  5])
>>> np.concatenate((x,y,z),axis=1)
array([ 1,  2,  3, 18,  7,  6,  1,  3,  5])
>>> np.concatenate((x,y,z),axis=2)
array([ 1,  2,  3, 18,  7,  6,  1,  3,  5])
>>> np.concatenate((x,y,z),axis=3)
array([ 1,  2,  3, 18,  7,  6,  1,  3,  5])

I want a dimensional array, how can I achieve this?


#2

Try using this:

np.vstack((x,y,z))

If you really want to use concatenate, you will need to reshape the arrays before concatenating them. But this is a longer and inefficient way of doing this.

Regards,
Kunal