Concatenate fails in simple example

Question:

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?

Asked By: KansaiRobot

||

Answers:

np.vstack is what you’re looking for. Note the transpose at the end, this converts vstack‘s 2×3 result to a 3×2 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]]
Answered By: Alex P
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.