Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining multiple 1D arrays returned from a function into a 2D array python

I m a little new to python. I have a function named featureExtraction which returns a 1-D array for an image. I need to stack all such 1-d arrays row wise to form a 2-d array. I have the following equivalent code in MATLAB.

    I1=imresize(I,[256 256]);
    Features(k,:) = featureextraction(I1);

featureextraction returns a 1-d row vector which is stacked row-wise to form a 2-d array. What is the equivalent code snippet in python?

Thank You in advance.

like image 622
m_amber Avatar asked Jan 24 '14 15:01

m_amber


2 Answers

Not sure what you're looking for, but maybe vstack or column_stack?

>>> np.vstack((a,a,a))
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> np.column_stack((a,a,a))
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4],
       [5, 5, 5],
       [6, 6, 6],
       [7, 7, 7],
       [8, 8, 8],
       [9, 9, 9]])

Or even just np.array:

>>> np.array([a,a,a])
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
       [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
like image 89
mgilson Avatar answered Oct 13 '22 18:10

mgilson


You can use numpy.vstack():

a = np.array([1,2,3])

np.vstack((a,a,a))
#array([[1, 2, 3],
#       [1, 2, 3],
#       [1, 2, 3]])
like image 23
Saullo G. P. Castro Avatar answered Oct 13 '22 17:10

Saullo G. P. Castro