Home > other >  Concatenate fails in simple example
Concatenate fails in simple example

Time:11-22

I am trying the simple examples of this page

In it it says:

arr=np.array([4,7,12])
arr1=np.array([5,9,15])
np.concatenate((arr,arr1))
# Must give array([ 4,  7, 12,  5,  9, 15])
np.concatenate((arr,arr1),axis=1)
#Must give 
#[[4,5],[7,9],[12,15]]
# but it gives *** numpy.AxisError: axis 1 is out of bounds for array of dimension 1

Why is this example not working?

CodePudding user response:

np.vstack is what you're looking for. Note the transpose at the end, this converts vstack's 2x3 result to a 3x2 array.

import numpy as np

arr = np.array([4,7,12])
arr1 = np.array([5,9,15])

a = np.vstack((arr,arr1)).T
print(a)

Output:

[[ 4  5]
 [ 7  9]
 [12 15]]
  • Related