I am bit confused between both the methods : concatenate and stack
The concatenate and stack provides exactly same output , what is the difference between both of them ?
Using : concatenate
import numpy as np
my_arr_1 = np.array([ [1,4] , [2,7] ])
my_arr_2 = np.array([ [0,5] , [3,8] ])
join_array=np.concatenate((my_arr_1,my_arr_2),axis=0)
print(join_array)
Using : stack
import numpy as np
my_arr_1 = np.array([ [1,4] , [2,7] ])
my_arr_2 = np.array([ [0,5] , [3,8] ])
join1_array=np.stack((my_arr_1,my_arr_2),axis=0)
print(join1_array)
Output for both is same :
[[[1 4]
[2 7]]
[[0 5]
[3 8]]]
CodePudding user response:
In [160]: my_arr_1 = np.array([ [1,4] , [2,7] ])
...: my_arr_2 = np.array([ [0,5] , [3,8] ])
...:
...: join_array=np.concatenate((my_arr_1,my_arr_2),axis=0)
In [161]: join_array
Out[161]:
array([[1, 4],
[2, 7],
[0, 5],
[3, 8]])
In [162]: _.shape
Out[162]: (4, 2)
concatenate
joined the 2 arrays on an existing axis, so the (2,2) become (4,2).
In [163]: join1_array=np.stack((my_arr_1,my_arr_2),axis=0)
In [164]: join1_array
Out[164]:
array([[[1, 4],
[2, 7]],
[[0, 5],
[3, 8]]])
In [165]: _.shape
Out[165]: (2, 2, 2)
stack
joined them on a new axis. It actually made them both (1,2,2) shape, and then used concatenate
.
The respective docs should make this clear.