python - Issue with numpy.concatenation -


i have defined 2 numpy array 2,3 , horizontally concatenate them

a=numpy.array([[1,2,3],[4,5,6]]) b=numpy.array([[7,8,9],[10,11,12]]) c=numpy.concatenate((a,b),axis=0) 

c becomes 4,3 matrix tried same thing 1,3 list as

a=numpy.array([1,2,3]) b=numpy.array([4,5,6]) c=numpy.concatenate((a,b),axis=0) 

now expecting 2,3 matrix instead have 1,6. understand vstack etc work curious why happening? , doing wrong numpy.concatenate?

thanks reply. can result suggested having 1,3 array , concatenation. logic have add rows empty matrix @ each iteration. tried append suggested:

testing=[] in range(3):    testing=testing.append([1,2,3]) 

it gave error testing doesnot have attribute append of none type. further if use logic of 1,3 array using np.array([[1,2,3]]) how can inside loop?

you didn't wrong. numpy.concatenate join sequence of arrays together.which means create integrated array current array's element in 2d array elements nested lists , in 1d array elements variables.

so not concatenate's job, said can use np.vstack :

>>> c=numpy.vstack((a,b)) >>> c array([[1, 2, 3],        [4, 5, 6]]) 

also in code list.append appends , element in-place list can not assign variable.instead can append testing in each iteration.

testing=[] in range(3):    testing.append([1,2,3]) 

also more efficient way can create list using list comprehension list following :

testing=[[1,2,3] _ in xrange(3)] 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -