Mastering NumPy Array Manipulation: Reshaping, Concatenation, and Splitting

Array manipulation is a fundamental skill when working with numerical data in NumPy. This article explores essential techniques for transforming array dimensions, combining multiple arrays, and splitting arrays in to smaller segments.

Reshaping Arrays

NumPy provides several methods to modify array dimensions. You can convert one-dimensional arrays to multi-dimensional arrays and vice versa, or directly modify the shape attribute.

The reshape() method creates a new view with a different shape without changing the data. The ravel() and flatten() methods both convert multi-dimensional arrays to one dimension, with ravel returning a view when possible and flaten always creating a copy.

import numpy as np

# Create a one-dimensional array
original = np.arange(32)
print('Original array:', original)
print('Original shape:', original.shape)

# Transform to three-dimensional array
cubic = original.reshape(4, 2, 4)
print('Reshaped array:\n', cubic)
print('Cubic shape:', cubic.shape)

# Transform to two-dimensional array
matrix = original.reshape(4, 8)
print('Matrix shape:', matrix.shape)

# Convert back to one-dimensional using ravel
flat_array = cubic.ravel()
print('Ravel result shape:', flat_array.shape)

# Convert to one-dimensional using flatten
flat_copy = matrix.flatten()
print('Flatten result shape:', flat_copy.shape)

Array Concatenation

Combining multiple arrays is a common operation in data processing. NumPy supports horizontal and vertical concatenation, each serving different purposes depending on how you want to align the arrays.

Horizontal Concatenation

Horizontal concatenation joins arrays column-wise, preserving the number of rows. The arrays being concatenated must have identical row counts to avoid errors.

import numpy as np

left = np.arange(6).reshape(2, 3)
print('Left array:\n', left)

right = np.arange(6, 12).reshape(2, 3)
print('Right array:\n', right)

combined = np.hstack([left, right])
print('Horizontally concatenated:\n', combined)

Vertical Concatenation

Vertical concatenation joins arrays row-wise, preserving the number of columns. All arrays must have identical column counts.

import numpy as np

top = np.arange(6).reshape(2, 3)
print('Top array:\n', top)

bottom = np.arange(6, 12).reshape(2, 3)
print('Bottom array:\n', bottom)

stacked = np.vstack([top, bottom])
print('Vertically concatenated:\n', stacked)

The concatenate Function

The concatenate() function provides more flexibility by allowing you to specify the axis along which to join arrays. The default axis is 0 (vertical concatenation).

import numpy as np

a = np.arange(6).reshape(2, 3)
print('Array A:\n', a)

b = np.arange(6, 12).reshape(2, 3)
print('Array B:\n', b)

default_concat = np.concatenate([a, b])
print('Default concatenation (axis=0):\n', default_concat)

vertical = np.concatenate([a, b], axis=0)
print('Vertical concatenation:\n', vertical)

horizontal = np.concatenate([a, b], axis=1)
print('Horizontal concatenation:\n', horizontal)

Multi-dimensional Array Concatenation

These concatenation methods also work with higher-dimensional arrays, maintaining the same principles regarding dimension compatibility.

import numpy as np

cube_a = np.arange(24).reshape(2, 3, 4)
print('Cube A shape:', cube_a.shape)

cube_b = np.arange(8, 24).reshape(2, 2, 4)
print('Cube B shape:', cube_b.shape)

cube_c = np.arange(16).reshape(2, 2, 4)
print('Cube C shape:', cube_c.shape)

horizontal_stack = np.hstack([cube_a, cube_b])
print('Horizontal stack shape:', horizontal_stack.shape)

vertical_stack = np.vstack([cube_b, cube_c])
print('Vertical stack shape:', vertical_stack.shape)

axis_one_concat = np.concatenate([cube_a, cube_b], axis=1)
print('Axis 1 concatenation shape:', axis_one_concat.shape)

Array Splitting

Splitting is the inverse operation of concatenation. You can divide arrays along specified axes, either evenly or at custom positions.

The split() function requires either an integer (for equal divisions) or indices specifying where to split. The axis parameter determines the splitting direction.

import numpy as np

# Split one-dimensional array
data = np.arange(8)
print('Data:', data)

# Split into equal parts using integer
chunks = np.split(data, 4)
print('Split chunks:', chunks)

# Split at specific positions using array
split_points = np.split(data, [2, 4])
print('Split at indices [2, 4]:', split_points)

import numpy as np

# Split two-dimensional array
matrix = np.arange(12).reshape(4, 3)
print('Matrix:\n', matrix)

# Split vertically (axis=0) into equal parts
vertical_splits = np.split(matrix, 2, axis=0)
print('Vertical split part 0:\n', vertical_splits[0])
print('Vertical split part 1:\n', vertical_splits[1])

# Split horizontally (axis=1) at position
horizontal_splits = np.split(matrix, [1], axis=1)
print('Horizontal split result:', horizontal_splits)

Horizontal Splitting

The hsplit() function specifically handles horizontal splitting, dividing columns into separate arrays.

import numpy as np

data = np.arange(12).reshape(3, 4)
print('Data matrix:\n', data)

parts = np.hsplit(data, 2)
print('First half:\n', parts[0])
print('Second half:\n', parts[1])

Vertical Splitting

The vsplit() function handles vertical splitting, dividing rows into separate arrays.

import numpy as np

data = np.arange(12).reshape(4, 3)
print('Data matrix:\n', data)

parts = np.vsplit(data, [2])
print('Top rows:\n', parts[0])
print('Bottom rows:\n', parts[1])

# Multiple split points
multiple_parts = np.vsplit(data, [1, 3])
print('After first row:', multiple_parts[0])
print('Between rows 2-3:', multiple_parts[1])

Understanding these array manipulation techniques enables efficient data transformation workflows. The key points to remember are maintaining dimension compatibility during concatenation and choosing the appropriate axis for splitting operations.

Tags: Numpy python array-manipulation data-analysis reshape

Posted on Mon, 28 Sep 2026 16:13:04 +0000 by x2fusion