Home > OS >  adding elements to a numpy array and reshape it
adding elements to a numpy array and reshape it

Time:03-06

I have the following numpy array

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

I have the two elements

b= [2, 2]
c= [3, 3]

I would like to add those elements b and c, so that my output seems like this

a= [[1, 1],
    [2, 2].
    [3, 3]], #shape=(3,2) 

which numpy function should i use? thanks

CodePudding user response:

Create a new numpy array with the three elements

>>> np.array([a,b,c])
array([[1, 1],
   [2, 2],
   [3, 3]])
# shape : (3, 2)

If a had more than 1 dimension, np.append can be used :

>>> a= np.array([[1,1], [4,4]])
>>> a
array([[1, 1],
       [4, 4]])
>>> np.append(a,[b],axis=0)
array([[1, 1],
       [4, 4],
       [2, 2]])
  • Related