Skip to content

NumPy - Array Manipulation

NumPy provides a comprehensive set of routines for manipulating the shape, structure, and content of ndarray objects. These can be grouped into several categories:

These functions alter the dimensions or layout of an array without changing the data elements themselves.

Function/AttributeDescription
reshape()Returns an array with a new shape, without changing data. Fails if shapes are incompatible. Can be a view or copy.
ndarray.reshape()Method version of reshape().
ndarray.flatA 1-D iterator over the array. Useful for looping through all elements regardless of shape.
ndarray.flatten()Returns a copy of the array collapsed into one dimension (1D).
ravel()Returns a flattened 1D array. May return a view if possible, otherwise a copy. Generally faster than flatten().

These functions rearrange the axes (dimensions) of an array.

Function/AttributeDescription
transpose()Permutes the dimensions of an array according to specified axes. If no axes are given, reverses the order of axes.
ndarray.TAttribute providing a convenient way to get the transpose (reverses dimension order). Equivalent to transpose() with no arguments.
rollaxis()Rolls the specified axis backwards until it lies in a given position. (Less common than transpose/swapaxes).
swapaxes()Interchanges two specified axes of an array. Returns a view if possible.

Functions to modify the number of dimensions or prepare arrays for broadcasting.

FunctionDescription
broadcastProduces an object that encapsulates broadcasting rules between arrays.
broadcast_to()Explicitly broadcasts an array to a new shape. Fails if shapes are incompatible for broadcasting. Returns a read-only view.
expand_dims()Expands the shape of an array by inserting a new axis (dimension of size 1) at the specified position.
squeeze()Removes axes of length one from an array. Returns a view if possible.

Functions to combine multiple arrays into a single larger array. concatenate is the most general function.

FunctionDescription
concatenate()Joins a sequence of arrays along an existing axis. Arrays must have the same shape except along the concatenation axis.
stack()Joins a sequence of arrays along a new axis. Input arrays must have the same shape.
hstack()Stacks arrays in sequence horizontally (column-wise). Equivalent to concatenate along axis=1 for 2D arrays.
vstack()Stacks arrays in sequence vertically (row-wise). Equivalent to concatenate along axis=0 for 2D arrays.

Functions to divide a single array into multiple smaller arrays.

FunctionDescription
split()Splits an array into multiple sub-arrays along a specified axis. Can split into N equal parts or at specified indices.
array_split()Similar to split, but allows splitting into non-equal parts if the axis size isn’t divisible by N.
hsplit()Splits an array into multiple sub-arrays horizontally (column-wise). Equivalent to split with axis=1.
vsplit()Splits an array into multiple sub-arrays vertically (row-wise). Equivalent to split with axis=0.

Important: NumPy arrays have a fixed size. Functions like append, insert, and delete return new arrays with the modifications; they do not modify the original array in place.

FunctionDescription
resize()Returns a new array with the specified shape. If the new shape is larger, fills with repeated copies of the original; if smaller, truncates.
append()Appends values to the end of an array (flattened by default). Returns a new array.
insert()Inserts values along a given axis before specified indices. Returns a new array.
delete()Returns a new array with sub-arrays along a specified axis deleted.
unique()Finds the unique elements of an array and returns them in sorted order. Can also return indices and counts.