Stacking and Joining in NumPy

Free NumPy course with real-time projects Start Now!!

Stacking is the concept of joining arrays in NumPy. Arrays having the same dimensions can be stacked. The stacking is done along a new axis. Stacking leads to increased customization of arrays. We can combine the stack function with other functions to further increase its capabilities.

Stacking and Joining in NumPy

NumPy implements the function of stacking. We can perform stacking along three dimensions:

  • vstack() – it performs vertical stacking along the rows.
  • hstack()– it performs horizontal stacking along with the columns.
  • dstack()– it performs in-depth stacking along a new third axis.

NumPy stack function takes two input parameters – the arrays for stacking and the axis for the resultant array.

Syntax:

import numpy as np
np.stack(array,axis)

Joining in NumPy

We use joining functions to append and concatenate elements and arrays. The concatenate ()function is for joining arrays along a new axis.

import numpy as np
 arr1 = np.array([[1, 2], [5, 6]])
arr2 = np.array([[7, 8], [3,4]])
a = np.concatenate((arr1, arr2))
print(a)
 

Output

[[1 2]
[5 6]
[7 8]
[3 4]]

1. NumPy append() Function

The append()function is to create a new array along a specified axis. It creates a copy of the original array.

Syntax:

np.append(arr, values, axis=None)

Array is the original array. The values are an array-like structure that appends to the original array. The axis defines the axis along which the array appends. Both the arrays will be flat if we don’t specify the axis.

import numpy as np
arr1 = np.array([[1, 2], [5, 6]])
arr2 = np.array([[7, 8], [3,4]])
a = np.append(arr1,arr2, axis=1)
print(a)

Output

[[1 2 7 8]
[5 6 3 4]]

2. NumPy vstack() Function

We use the vstack()function to sequentially stack arrays in a vertical order i.e. along the rows. This is a more specific concept of array stacking. It is very useful for arrays with up to 3 dimensions. The vsplit() function splits the array into a list of multiple sub-arrays vertically.

The vstack()then rebuilds these arrays split by the vsplit()function. Vertical stacking gives error when the number of columns is not equal in both the arrays.

a = np.array([1, 2, 3])
b = np.array([5,6,7])
np.vstack((a,b))

Output

array([[1, 2, 3],
[5, 6, 7]])

3. NumPy hstack() Function

We use this function to sequentially stack arrays in horizontal order i.e. along with the columns. This is a specific concept for column vice concatenation. We cannot perform horizontal stacking if the number of rows is not equal in both the arrays.

a = np.array([[1, 2],[3,4]])
b = np.array([[5,6],[7,8]])
np.hstack((a,b))

Output

array([[1, 2, 5, 6],
[3, 4, 7, 8]])

4. NumPy dstack() Function

This function is for stacking the arrays along depth. The resultant array is along a third new dimension.

a = np.array([[1, 2],[3,4]])
b = np.array([[5,6],[7,8]])
np.dstack((a,b))

Output

array([[[1, 5],
[2, 6]],[[3, 7],
[4, 8]]])

5. NumPy reshape() Function

Reshaping is for changing the shape of the array without changing the array data. When the shape of the array is not according to the requirements, we use this function. For unequal shaped arrays, we perform reshape prior to stacking.

a. Reshape with vertical stacking

a = np.array([[1,2],[3,4]])
b = np.array([[1],[2]]).reshape(1,2) 
print(b)
np.vstack((a,b))

Output

[[1 2]]
array([[1, 2],
[3, 4],
[1, 2]])

b. Reshape with horizontal stacking

a = np.array([[1,2],[3,4]])
b = np.array((10,10)).reshape(2,1) 
print(b)
np.hstack((a,b))

Output

[[10]
[10]]
array([[ 1, 2, 10],
[ 3, 4, 10]])

Summary

Stacking and joining functions in NumPy are very useful for giving new dimensions to an array. The stacking function along with the reshape function is to avoid unequal shape errors. Concatenate, stack, and append are general functions. The vertical, horizontal, and depth stacking are more specific.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *