NumPy - Array Manipulation
NumPy - Array Manipulation Techniques
Section titled “NumPy - Array Manipulation Techniques”NumPy provides a comprehensive set of routines for manipulating the shape, structure, and content of ndarray objects. These can be grouped into several categories:
Changing Shape
Section titled “Changing Shape”These functions alter the dimensions or layout of an array without changing the data elements themselves.
| Function/Attribute | Description |
|---|---|
| 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.flat | A 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(). |
Transpose-like Operations
Section titled “Transpose-like Operations”These functions rearrange the axes (dimensions) of an array.
| Function/Attribute | Description |
|---|---|
| transpose() | Permutes the dimensions of an array according to specified axes. If no axes are given, reverses the order of axes. |
| ndarray.T | Attribute 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. |
Changing Dimensions
Section titled “Changing Dimensions”Functions to modify the number of dimensions or prepare arrays for broadcasting.
| Function | Description |
|---|---|
| broadcast | Produces 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. |
Joining Arrays
Section titled “Joining Arrays”Functions to combine multiple arrays into a single larger array. concatenate is the most general function.
| Function | Description |
|---|---|
| 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. |
Splitting Arrays
Section titled “Splitting Arrays”Functions to divide a single array into multiple smaller arrays.
| Function | Description |
|---|---|
| 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. |
Adding / Removing Elements
Section titled “Adding / Removing Elements”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.
| Function | Description |
|---|---|
| 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. |