Concatenation, Splitting, and Stacking Arrays in NumPy

NumPy is a powerful Python library used for scientific computing. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently. One of the key features of NumPy is the ability to manipulate arrays through concatenation, splitting, and stacking operations. This article will explore these operations and demonstrate their usage with examples.

Concatenation

Concatenation is the process of combining two or more arrays into a single array. NumPy provides several functions to concatenate arrays, such as np.concatenate(), np.vstack(), and np.hstack().

np.concatenate()

The np.concatenate() function takes a sequence of arrays as input and concatenates them along a specified axis. The default axis is 0, which represents vertical concatenation.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.concatenate((a, b))
print(c)  # Output: [1 2 3 4 5 6]

np.vstack() and np.hstack()

The np.vstack() function stacks arrays vertically, i.e., concatenates arrays along their rows. On the other hand, the np.hstack() function stacks arrays horizontally, i.e., concatenates arrays along their columns.

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
c = np.vstack((a, b))
print(c)
# Output:
# [[1 2]
#  [3 4]
#  [5 6]]

d = np.array([[7], [8]])
e = np.hstack((c, d))
print(e)
# Output:
# [[1 2 7]
#  [3 4 8]
#  [5 6]]

Splitting

Splitting is the opposite operation of concatenation. It divides a single array into multiple sub-arrays. NumPy provides the np.split(), np.hsplit(), and np.vsplit() functions to split arrays.

np.split()

The np.split() function splits an array into multiple sub-arrays along a specified axis.

import numpy as np

a = np.arange(9)
b = np.split(a, 3)
print(b)  # Output: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]

np.hsplit() and np.vsplit()

The np.hsplit() function splits an array horizontally, i.e., divides an array into columns. The np.vsplit() function splits an array vertically, i.e., divides an array into rows.

import numpy as np

a = np.arange(16).reshape(4, 4)
b = np.hsplit(a, 2)
print(b)
# Output:
# [array([[0, 1],
#        [4, 5],
#        [8, 9],
#        [12, 13]]),
#  array([[ 2,  3],
#        [ 6,  7],
#        [10, 11],
#        [14, 15]])]

c = np.vsplit(a, 2)
print(c)
# Output:
# [array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7]]),
#  array([[ 8,  9, 10, 11],
#        [12, 13, 14, 15]])]

Stacking

Stacking is the process of combining arrays along a new axis. It is different from concatenation because it creates new dimensions in the resulting array. NumPy provides the np.stack() function for stacking arrays.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.stack((a, b))
print(c)
# Output:
# [[1 2 3]
#  [4 5 6]]

The np.stack() function can also stack arrays along an existing axis by specifying the axis parameter.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.stack((a, b), axis=1)
print(c)
# Output:
# [[1 4]
#  [2 5]
#  [3 6]]

In conclusion, NumPy provides a range of convenient functions to concatenate, split, and stack arrays. These operations are useful for data manipulation and can help in various scientific computing tasks. Understanding these operations and their usage will enhance your capability to work with multi-dimensional arrays efficiently using NumPy.


noob to master © copyleft