Skip to content
Related Articles

# numpy.stack() in Python

• Last Updated : 06 Jan, 2019

`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 :

 `# 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)`
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 :

 `# 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)`
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]]]
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up