NumPy is a famous Python library used for working with arrays. One of the important functions of this library is stack().
Important points:
- stack() is used for joining multiple NumPy arrays. Unlike, concatenate(), it joins arrays along a new axis. It returns a NumPy array.
- to join 2 arrays, they must have the same shape and dimensions. (e.g. both (2,3)–> 2 rows,3 columns)
- stack() creates a new array which has 1 more dimension than the input arrays. If we stack 2 1-D arrays, the resultant array will have 2 dimensions.
Syntax: numpy.stack(arrays, axis=0, out=None)
Parameters:
- arrays: Sequence of input arrays (required)
- axis: Along this axis, in the new array, input arrays are stacked. Possible values are 0 to (n-1) positive integer for n-dimensional output array. For example, in the case of a resultant 2-D array, there are 2 possible axis options :0 and 1. axis=0 means 1D input arrays will be stacked row-wise. axis=1 means 1D input arrays will be stacked column-wise. We shall see the example later in detail. -1 means last dimension. e.g. for 2D arrays axis 1 and -1 are same. (optional)
- out: The destination to place the resultant array.
Example #1 : stacking two 1d arrays
Python
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.stack((a, b),axis=0)
print(c)
|
output –
array([[1, 2, 3],
[4, 5, 6]])
Notice, output is a 2-D array. They are stacked row-wise. Now, let’s change the axis to 1.
output –
array([[1, 4],
[2, 5],
[3, 6]])
Here, stack() takes 2 1-D arrays and stacks them one after another as if it fills elements in new array column-wise.
output –
array([[1, 4],
[2, 5],
[3, 6]])
-1 represents ‘last dimension-wise’. Here 2 axis are possible. 0 and 1. So, -1 is same as 1.
Example #2 : stacking two 2d arrays
Python3
x=np.array([[1,2,3],
[4,5,6]])
y=np.array([[7,8,9],
[10,11,12]])
|
1. stacking with axis=0
output –
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
Imagine as if they are stacked one after another and made a 3-D array.
2. stacking with axis=1
Output – 3D array. 1st dimension has 1st rows. 2nd dimension has 2nd rows. [Row-wise stacking]
array([[[ 1, 2, 3],
[ 7, 8, 9]],
[[ 4, 5, 6],
[10, 11, 12]]])
3. stacking with axis =2
Output – 3D array. 1st dimension has 1st rows. 2nd dimension has 2nd rows. [Column-wise stacking]
array([[[ 1, 7],
[ 2, 8],
[ 3, 9]],
[[ 4, 10],
[ 5, 11],
[ 6, 12]]])
Example #2 : stacking more than two 2d arrays
1. with axis=0 : Just stacking.
Python3
x=np.array([[1,2,3],
[4,5,6]])
y=np.array([[7,8,9],
[10,11,12]])
z=np.array([[13,14,15],
[16,17,18]])
np.stack((x,y,z),axis=0)
|
output –
array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]],
[[13, 14, 15],
[16, 17, 18]]])
2. with axis =1 (row-wise stacking)
output –
array([[[ 1, 2, 3],
[ 7, 8, 9],
[13, 14, 15]],
[[ 4, 5, 6],
[10, 11, 12],
[16, 17, 18]]])
3. with axis =2 (column-wise stacking)
output-
array([[[ 1, 7, 13],
[ 2, 8, 14],
[ 3, 9, 15]],
[[ 4, 10, 16],
[ 5, 11, 17],
[ 6, 12, 18]]])
Example #3 : stacking two 3d arrays
1. axis=0. Just stacking
Python3
m=np.array([[[1,2,3],
[4,5,6],
[7,8,9]],
[[10,11,12],
[13,14,15],
[16,17,18]]])
n=np.array([[[51,52,53],
[54,55,56],
[57,58,59]],
[[110,111,112],
[113,114,115],
[116,117,118]]])
np.stack((m,n),axis=0)
|
output –
array([[[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[ 10, 11, 12],
[ 13, 14, 15],
[ 16, 17, 18]]],
[[[ 51, 52, 53],
[ 54, 55, 56],
[ 57, 58, 59]],
[[110, 111, 112],
[113, 114, 115],
[116, 117, 118]]]])
2. with axis=1
output – Imagine as if the resultant array takes 1st plane of each array for 1st dimension and so on.
array([[[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[ 51, 52, 53],
[ 54, 55, 56],
[ 57, 58, 59]]],
[[[ 10, 11, 12],
[ 13, 14, 15],
[ 16, 17, 18]],
[[110, 111, 112],
[113, 114, 115],
[116, 117, 118]]]])
3. with axis = 2
output –
array([[[[ 1, 2, 3],
[ 51, 52, 53]],
[[ 4, 5, 6],
[ 54, 55, 56]],
[[ 7, 8, 9],
[ 57, 58, 59]]],
[[[ 10, 11, 12],
[110, 111, 112]],
[[ 13, 14, 15],
[113, 114, 115]],
[[ 16, 17, 18],
[116, 117, 118]]]])
4. with axis = 3
output –
array([[[[ 1, 51],
[ 2, 52],
[ 3, 53]],
[[ 4, 54],
[ 5, 55],
[ 6, 56]],
[[ 7, 57],
[ 8, 58],
[ 9, 59]]],
[[[ 10, 110],
[ 11, 111],
[ 12, 112]],
[[ 13, 113],
[ 14, 114],
[ 15, 115]],
[[ 16, 116],
[ 17, 117],
[ 18, 118]]]])
Similar Reads
numpy.stack() in Python
NumPy is a famous Python library used for working with arrays. One of the important functions of this library is stack(). Important points:stack() is used for joining multiple NumPy arrays. Unlike, concatenate(), it joins arrays along a new axis. It returns a NumPy array.to join 2 arrays, they must
5 min read
numpy.vstack() in python
numpy.vstack() function is used to stack the sequence of input arrays vertically to make a single array. Syntax : numpy.vstack(tup) Parameters : tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same shape along all but the first axis. Return : [stacked nda
2 min read
numpy.column_stack() in Python
numpy.column_stack() function is used to stack 1-D arrays as columns into a 2-D array.It takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array. 2-D arrays are stacked as-is, just like with hstack function. Syntax : numpy.column_stack(tup) Parameters : tup : [sequence of
2 min read
numpy.ma.row_stack() in Python
numpy.ma.row_stack() : This function helps stacking arrays row wise in sequence vertically manner. Parameters : tup : sequence of ndarrays. 1D arrays must have same length, arrays must have the same shape along with all the axis. Result : Row-wise stacked arrays Code #1: Explaining row_stack() # imp
1 min read
numpy.sum() in Python
This function returns the sum of array elements over the specified axis. Syntax: numpy.sum(arr, axis, dtype, out): Parameters: arr: Input array. axis: The axis along which we want to calculate the sum value. Otherwise, it will consider arr to be flattened(works on all the axes). axis = 0 means along
3 min read
Stack in Python
A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner. In stack, a new element is added at one end and an element is removed from that end only. The insert and delete operations are often called push and pop. The functions associated wi
8 min read
Monotonic Stack in Python
A Monotonic Stack is a data structure used to maintain elements in a monotonically increasing or decreasing order. It's particularly useful in problems like finding the next or previous greater or smaller elements. In this article, we'll learn how to implement and use a monotonic stack in Python wit
3 min read
Python Numpy
Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, Numpy can also be used as an efficient
7 min read
Python | StackLayout in Kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy with Examples. StackLayout:
3 min read
numpy.hstack() in Python
numpy.hstack() function is used to stack the sequence of input arrays horizontally (i.e. column wise) to make a single array. Syntax : numpy.hstack(tup) Parameters : tup : [sequence of ndarrays] Tuple containing arrays to be stacked. The arrays must have the same shape along all but the second axis.
2 min read
numpy.cumsum() in Python
numpy.cumsum() function is used to compute the cumulative sum of elements in an array. Cumulative sum refers to a sequence where each element is the sum of all previous elements plus itself. For example, given an array [1, 2, 3, 4, 5], the cumulative sum would be [1, 3, 6, 10, 15]. Let's implement t
3 min read
numpy.base_repr() in Python
numpy.base_repr(number, base=2, padding=0) function is used to return a string representation of a number in the given base system. For example, decimal number 10 is represented as 1010 in binary whereas it is represented as 12 in octal. Syntax : numpy.base_repr(number, base=2, padding=0) Parameters
3 min read
Stack and Queues in Python
Prerequisites : list and Deque in Python. Unlike C++ STL and Java Collections, Python does have specific classes/interfaces for Stack and Queue. Following are different ways to implement in Python 1) Using list Stack works on the principle of "Last-in, first-out". Also, the inbuilt functions in Pyth
3 min read
NumPy Tutorial - Python Library
NumPy is a powerful library for numerical computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. NumPy’s array objects are more memory-efficient and perform better than Python lists, whi
7 min read
NumPy Tutorial - Python Library
NumPy is a powerful library for numerical computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. NumPy’s array objects are more memory-efficient and perform better than Python lists, whi
7 min read
numpy.asscalar() in Python
numpy.asscalar() function is used when we want to convert an array of size 1 to its scalar equivalent. Syntax : numpy.asscalar(arr) Parameters : arr : [ndarray] Input array of size 1. Return : Scalar representation of arr. The output data type is the same type returned by the input’s item method. Co
1 min read
Matplotlib.pyplot.stackplot() in Python
Matplotlib is a visualization library available in Python. Pyplot contains various functions that help matplotlib behave like MATLAB. It is used as matplotlib.pyplot for plotting figures, creating areas, lines, etc. Stackplot Among so many functions provided by pyplot one is stackplot which will be
3 min read
numpy.nancumsum() in Python
numpy.nancumsum() function is used when we want to compute the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. Zeros are returned for slices that are all-NaN
3 min read
numpy.pad() function in Python
numpy.pad() function is used to pad the Numpy arrays. Sometimes there is a need to perform padding in Numpy arrays, then numPy.pad() function is used. The function returns the padded array of rank equal to the given array and the shape will increase according to pad_width. Syntax: numpy.pad(array, p
2 min read