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 :
# 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) |
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) |
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]]]
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- abs() in Python
- Python Set | pop()
- Any & All in Python
- bin() in Python
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.



