numpy.stack() in Python

numpy.stack() function is used to join a sequence of same dimension arrays along a new axis.The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

Syntax : numpy.stack(arrays, axis)

Parameters :
arrays : [array_like] Sequence of arrays of the same shape.
axis : [int] Axis in the resultant array along which the input arrays are stacked.



Return : [stacked ndarray] The stacked array of the input arrays which has one more dimension than the input arrays.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# stack() function
  
import numpy as geek
  
# input array
in_arr1 = geek.array([ 1, 2, 3] )
print ("1st Input array : \n", in_arr1) 
  
in_arr2 = geek.array([ 4, 5, 6] )
print ("2nd Input array : \n", in_arr2) 
  
# Stacking the two arrays along axis 0
out_arr1 = geek.stack((in_arr1, in_arr2), axis = 0)
print ("Output stacked array along axis 0:\n ", out_arr1)
  
# Stacking the two arrays along axis 1
out_arr2 = geek.stack((in_arr1, in_arr2), axis = 1)
print ("Output stacked array along axis 1:\n ", out_arr2)

chevron_right


Output:

1st Input array : 
 [1 2 3]
2nd Input array : 
 [4 5 6]
Output stacked array along axis 0:
  [[1 2 3]
 [4 5 6]]
Output stacked array along axis 1:
  [[1 4]
 [2 5]
 [3 6]]

 
Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# stack() function
  
import numpy as geek
  
# input array
in_arr1 = geek.array([[ 1, 2, 3], [ -1, -2, -3]] )
print ("1st Input array : \n", in_arr1) 
  
in_arr2 = geek.array([[ 4, 5, 6], [ -4, -5, -6]] )
print ("2nd Input array : \n", in_arr2) 
  
# Stacking the two arrays along axis 0
out_arr1 = geek.stack((in_arr1, in_arr2), axis = 0)
print ("Output stacked array along axis 0:\n ", out_arr1)
  
# Stacking the two arrays along axis 1
out_arr2 = geek.stack((in_arr1, in_arr2), axis = 1)
print ("Output stacked array along axis 1:\n ", out_arr2)
  
# Stacking the two arrays along last axis 
out_arr3 = geek.stack((in_arr1, in_arr2), axis = -1)
print ("Output stacked array along last axis :\n ", out_arr3)

chevron_right


Output:

1st Input array : 
 [[ 1  2  3]
 [-1 -2 -3]]
2nd Input array : 
 [[ 4  5  6]
 [-4 -5 -6]]
Output stacked array along axis 0:
  [[[ 1  2  3]
  [-1 -2 -3]]

 [[ 4  5  6]
  [-4 -5 -6]]]
Output stacked array along axis 1:
  [[[ 1  2  3]
  [ 4  5  6]]

 [[-1 -2 -3]
  [-4 -5 -6]]]
Output stacked array along last axis :
  [[[ 1  4]
  [ 2  5]
  [ 3  6]]

 [[-1 -4]
  [-2 -5]
  [-3 -6]]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.