Joining Array In Python -


hi want join multiple arrays in python, using numpy form multidimensional arrays, it's inside of loop, pseudocode

import numpy np h = np.zeros(4) x in range(3):    x1 = array of length of 4 returned previous function (3,5,6,7)    h = np.concatenate((h,x1), axis =0) 

the first iteration goes fine, during second iteration on loop following error,

valueerror: input arrays must have same number of dimensions

the output array should this

 [[0,0,0,0],[3,5,6,7],[6,3,6,7]] 

etc

so how can join arrays?

thanks

you need use vstack. allows stack arrays. take sequence of arrays , stack them vertically make single array

 import numpy np    h = np.zeros(4)  x in range(3):      x1 = [3,5,6,7]      h = np.vstack((h,x1))      # not h = np.concatenate((h,x1), axis =0)    print h 

output:

[[ 0.  0.  0.  0.]  [ 3.  5.  6.  7.]  [ 3.  5.  6.  7.]  [ 3.  5.  6.  7.]] 

more edits later. if want use cocatenate only, can following way well:

 import numpy np  h1 = np.zeros(4)   x in range(3):       x1 = np.array([3,5,6,7])       h1= np.concatenate([h1,x1.t], axis =0)   print h1.shape  print h1.reshape(4,4) 

output:

(16,) [[ 0.  0.  0.  0.]  [ 3.  5.  6.  7.]  [ 3.  5.  6.  7.]  [ 3.  5.  6.  7.]] 

both have different applications. can choose according need.


Comments