Numpy Array Manipulation
Numpy contains some functions for processing arrays, which can be roughly divided into the following categories:
* * *
## Reshaping Arrays
| Function | Description |
| --- | --- |
| `reshape` | Changes the shape of an array without changing its data |
| `flat` | A 1-D iterator over the array |
| `flatten` | Returns a copy of the array collapsed into one dimension |
| `ravel` | Returns a contiguous flattened array |
### numpy.reshape
The numpy.reshape function changes the shape of an array without changing its data. The format is as follows:
numpy.reshape(arr, newshape, order='C')
* `arr`: The array to be reshaped.
* `newshape`: The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
* order: 'C' -- means to read/write the elements using C-like index order, 'F' -- means to read/write the elements using Fortran-like index order, 'A' -- means to read/write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise, 'K' -- means to read/write the elements in the order they occur in memory.
## Example
import numpy as np a = np.arange(8)print('Original array:')print(a)print('n')b = a.reshape(4,2)print('Reshaped array:')print(b)
The output is as follows:
Original array:Reshaped array:[ ]
### numpy.ndarray.flat
numpy.ndarray.flat is a 1-D iterator over the array. Example:
## Example
import numpy as np a = np.arange(9).reshape(3,3)print('Original array:')for row in a: print(row)print('Iterating over the array:')for element in a.flat: print(element)
The output is as follows:
Original array:Iterating over the array:012345678
### numpy.ndarray.flatten
numpy.ndarray.flatten returns a copy of the array collapsed into one dimension. The format is as follows:
ndarray.flatten(order='C')
Parameter description:
* order: 'C' -- means to flatten in row-major (C-style) order, 'F' -- means to flatten in column-major (Fortran-style) order, 'A' -- means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise, 'K' -- means to flatten the array in the order the elements occur in memory.
## Example
import numpy as np a = np.arange(8).reshape(2,4)print('Original array:')print(a)print('n')print('Flattened array:')print(a.flatten())print('n')print('Flattened array in F-style order:')print(a.flatten(order = 'F'))
The output is as follows:
Original array:[ ]Flattened array:Flattened array in F-style order:
### numpy.ravel
numpy.ravel() returns a contiguous flattened array. The order is typically "C-style". It returns a view of the original array (similar to a C/C++ reference), so modifications will affect the original array.
The function takes two parameters:
numpy.ravel(a, order='C')
Parameter description:
* order: 'C' -- means to flatten in row-major (C-style) order, 'F' -- means to flatten in column-major (Fortran-style) order, 'A' -- means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise, 'K' -- means to flatten the array in the order the elements occur in memory.
## Example
import numpy as np a = np.arange(8).reshape(2,4)print('Original array:')print(a)print('n')print('After calling ravel function:')print(a.ravel())print('n')print('After calling ravel function in F-style order:')print(a.ravel(order = 'F'))
The output is as follows:
Original array:[ ]After calling ravel function:After calling ravel function in F-style order:
* * *
## Flipping Arrays
| Function | Description |
| --- | --- |
| `transpose` | Permute the dimensions of an array |
| `ndarray.T` | Same as `self.transpose()` |
| `rollaxis` | Roll the specified axis backwards |
| `swapaxes` | Interchange two axes of an array |
### numpy.transpose
The numpy.transpose function permutes the dimensions of an array. The format is as follows:
numpy.transpose(arr, axes)
Parameter description:
* `arr`: The array to be transposed.
* `axes`: A list of integers corresponding to the dimensions. By default, all dimensions are reversed.
## Example
import numpy as np a = np.arange(12).reshape(3,4)print('Original array:')print(a)print('n')print('Transposed array:')print(np.transpose(a))
The output is as follows:
Original array:[ ]Transposed array:[ ]
numpy.ndarray.T is similar to numpy.transpose:
## Example
import numpy as np a = np.arange(12).reshape(3,4)print('Original array:')print(a)print('n')print('Transposed array:')print(a.T)
The output is as follows:
Original array:[ ]Transposed array:[ ]
### numpy.rollaxis
The numpy.rollaxis function rolls the specified axis backwards, until it lies in a given position. The format is as follows:
numpy.rollaxis(arr, axis, start)
Parameter description:
* `arr`: The input array.
* `axis`: The axis to roll backwards. The positions of the other axes do not change relative to one another.
* `start`: The default is zero. The roll is complete before this position.
## Example
import numpy as np a = np.arange(8).reshape(2,2,2)print('Original array:')print(a)print('Get a value from the array:')print(np.where(a==6))print(a[1,1,0])print('n')print('Calling rollaxis function:')b = np.rollaxis(a,2,0)print(b)print(np.where(b==6))print('n')print('Calling rollaxis function:')c = np.rollaxis(a,2,1)print(c)print(np.where(c==6))print('n')
The output is as follows:
Original array:[[ ] [ ]]Get a value from the array:(array(), array(), array())6Calling rollaxis function:[[ ] [ ]](array(), array(), array())Calling rollaxis function:[[ ] [ ]](array(), array(), array())
### numpy.swapaxes
The numpy.swapaxes function interchanges two axes of an array. The format is as follows:
numpy.swapaxes(arr, axis1, axis2)
* `arr`: The input array.
* `axis1`: The first axis.
* `axis2`: The second axis.
## Example
import numpy as np a = np.arange(8).reshape(2,2,2)print('Original array:')print(a)print('n')print('Array after calling swapaxes function:')print(np.swapaxes(a, 2, 0))
The output is as follows:
Original array:[[ ] [ ]]Array after calling swapaxes function:[[ ] [ ]]
* * *
## Modifying Array Dimensions
| Dimension | Description |
| --- | --- |
| `broadcast` | An object that mimics broadcasting |
| `broadcast_to` | Broadcast an array to a new shape |
| `expand_dims` | Expand the shape of an array |
| `squeeze` | Remove single-dimensional entries from the shape of an array |
### numpy.broadcast
numpy.broadcast is used to mimic broadcasting. It returns an object that encapsulates the result of broadcasting one array against another.
This function takes two arrays as input parameters, as shown in the following example:
## Example
import numpy as np x = np.array([, , ])y = np.array([4, 5, 6])b = np.broadcast(x,y)print('Broadcasting x over y:')r,c = b.iters print(next(r), next(c))print(next(r), next(c))print('n')print('Shape of the broadcast object:')print(b.shape)print('n')b = np.broadcast(x,y)c = np.empty(b.shape)print('Manually adding x and y using broadcast:')print(c.shape)print('n')c.flat = [u + v for(u,v)in b]print('Calling flat function:')print(c)print('n')print('Sum of x and y:')print(x + y)
The output is as follows:
Broadcasting x over y:1 41 5Shape of the broadcast object:(3, 3)Manually adding x and y using broadcast:(3, 3)Calling flat function:[[5. 6. 7.] [6. 7. 8.] [7. 8. 9.]] Sum of x and y:[ ]
### numpy.broadcast_to
The numpy.broadcast_to function broadcasts an array to a new shape. It returns a read-only view of the original array. It is usually not contiguous. If the new shape does not conform to NumPy's broadcasting rules, this function may raise a ValueError.
numpy.broadcast_to(array, shape, subok)
## Example
import numpy as np a = np.arange(4).reshape(1,4)print('Original array:')print(a)print('n')print('After calling broadcast_to function:')print(np.broadcast_to(a,(4,4)))
The output is as follows:
Original array:[]After calling broadcast_to function:[ ]
### numpy.expand_dims
The numpy.expand_dims function expands the shape of an array by inserting a new axis at a specified position. The function format is as follows:
numpy.expand_dims(arr, axis)
Parameter description:
* `arr`: The input array.
* `axis`: The position where the new axis is inserted.
## Example
import numpy as np x = np.array(([1,2],[3,4]))print('Array x:')print(x)print('n')y = np.expand_dims(x, axis = 0)print('Array y:')print(y)print('n')print('Shape of array x and y:')print(x.shape, y.shape)print('n')y = np.expand_dims(x, axis = 1)print('Array y after inserting axis at position 1:')print(y)print('n')print('x.ndim and y.ndim:')print(x.ndim,y.ndim)print('n')print('x.shape and y.shape:')print(x.shape, y.shape)
The output is as follows:
Array x:[ ]Array y:[[ ]]Shape of array x and y:(2, 2) (1, 2, 2)Array y after inserting axis at position 1:[[] []] x.ndim and y.ndim:2 3 x.shape and y.shape:(2, 2) (2, 1, 2)
### numpy.squeeze
The numpy.squeeze function removes single-dimensional entries from the shape of an array. The function format is as follows:
numpy.squeeze(arr, axis)
Parameter description:
* `arr`: The input array.
* `axis`: An integer or tuple of integers, selecting a subset of the single-dimensional entries in the shape.
## Example
import numpy as np x = np.arange(9).reshape(1,3,3)print('Array x:')print(x)print('n')y = np.squeeze(x)print('Array y:')print(y)print('n')print('Shape of array x and y:')print(x.shape, y.shape)
The output is as follows:
Array x:[[ ]]Array y:[ ]Shape of array x and y:(1, 3, 3) (3, 3)
* * *
## Joining Arrays
| Function | Description |
| --- | --- |
| `concatenate` | Join a sequence of arrays along an existing axis |
| `stack` | Join a sequence of arrays along a new axis |
| `hstack` | Stack arrays in sequence horizontally (column wise) |
| `vstack` | Stack arrays in sequence vertically (row wise) |
### numpy.concatenate
The numpy.concatenate function joins two or more arrays along an existing axis. The format is as follows:
numpy.concatenate((a1, a2, ...), axis)
Parameter description:
* `a1, a2, ...`: The arrays must have the same shape, except in the dimension corresponding to axis.
* `axis`: The axis along which the arrays will be joined. Default is 0.
## Example
import numpy as np a = np.array([[1,2],[3,4]])print('First array:')print(a)print('n')b = np.array([[5,6],[7,8]])print('Second array:')print(b)print('n')print('Joining the two arrays along axis 0:')print(np.concatenate((a,b)))print('n')print('Joining the two arrays along axis 1:')print(np.concatenate((a,b),axis = 1))
The output is as follows:
First array:[ ]Second array:[ ]Joining the two arrays along axis 0:[ ]Joining the two arrays along axis 1:[ ]
### numpy.stack
The numpy.stack function joins a sequence of arrays along a new axis. The format is as follows:
numpy.stack(arrays, axis)
Parameter description:
* `arrays`: The arrays must have the same shape.
* `axis`: The axis in the result array along which the input arrays are stacked.
## Example
import numpy as np a = np.array([[1,2],[3,4]])print('First array:')print(a)print('n')b = np.array([[5,6],[7,8]])print('Second array:')print(b)print('n')print('Stacking the two arrays along axis 0:')print(np.stack((a,b),0))print('n')print('Stacking the two arrays along axis 1:')print(np.stack((a,b),1))
The output is as follows:
First array:[ ]Second array:[ ]Stacking the two arrays along axis 0:[[ ] [ ]]Stacking the two arrays along axis 1:[[ ] [ ]]
### numpy.hstack
numpy.hstack is a variant of numpy.stack. It stacks arrays horizontally.
## Example
import numpy as np a = np.array([[1,2],[3,4]])print('First array:')print(a)print('n')b = np.array([[5,6],[7,8]])print('Second array:')print(b)print('n')print('Horizontal stacking:')c = np.hstack((a,b))print(c)print('n')
The output is as follows:
First array:[ ]Second array:[ ]Horizontal stacking:[ ]
### numpy.vstack
numpy.vstack is a variant of numpy.stack. It stacks arrays vertically.
## Example
import numpy as np a = np.array([[1,2],[3,4]])print('First array:')print(a)print('n')b = np.array([[5,6],[7,8]])print('Second array:')print(b)print('n')print('Vertical stacking:')c = np.vstack((a,b))print(c)
The output is as follows:
First array:[ ]Second array:[ ]Vertical stacking:[ ]
* * *
## Splitting Arrays
| Function | Array and Operation |
| --- | --- |
| `split` | Split an array into multiple sub-arrays |
| `hsplit` | Split an array horizontally into multiple sub-arrays (column-wise) |
| `vsplit` | Split an array vertically into multiple sub-arrays (row-wise) |
### numpy.split
The numpy.split function splits an array into sub-arrays along a specific axis. The format is as follows:
numpy.split(ary, indices_or_sections, axis)
Parameter description:
* `ary`: The array to be split.
* `indices_or_sections`: If an integer, the array is split into that many equal arrays along the axis. If a 1-D array of sorted integers, the entries indicate where along the axis the array is split.
* `axis`: The axis along which to split. Default is 0, which is horizontal splitting (i.e., along the columns). If 1, it is vertical splitting (i.e., along the rows).
## Example
import numpy as np a = np.arange(9)print('First array:')print(a)print('n')print('Splitting the array into three equal sub-arrays:')b = np.split(a,3)print(b)print('n')print('Splitting the array at the positions indicated by a 1-D array:')b = np.split(a,[4,7])print(b)
The output is as follows:
First array:Splitting the array into three equal sub-arrays:[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]Splitting the array at the positions indicated by a 1-D array:[array([0, 1, 2, 3]), array([4, 5, 6]), array([7, 8])]
When axis is 0, it splits horizontally; when axis is 1, it splits vertically:
## Example
import numpy as np
a = np.arange(16).reshape(4,4)
print('First array:')
print(a)
print('n')
print('Default split (axis 0):')
b = np.split(a,2)
print(b)
print('n')
print('Splitting horizontally:')
c = np.split(a,2,1)
print(c)
print('n')
print('Splitting horizontally:')
d= np.hsplit(a,2)
print(d)
The output is as follows:
First array:[ ]Default split (axis 0):[array([[0, 1, 2, 3], [4, 5, 6, 7]]), array([[ 8, 9, 10, 11], [12, 13, 14, 15]])]Splitting horizontally:[array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]), array([[ 2, 3], [ 6, 7], [10, 11], [14, 15]])]Splitting horizontally:[array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]), array([[ 2, 3], [ 6, 7], [10, 11], [14, 15]])]
### numpy.hsplit
The numpy.hsplit function is used to split an array horizontally. It splits the original array by specifying the number of arrays of the same shape to return.
## Example
import numpy as np harr = np.floor(10 * np.random.random((2, 6)))print('Original array:')print(harr)print('After splitting:')print(np.hsplit(harr, 3))
The output is as follows:
Original array:[[4. 7. 6. 3. 2. 6.] [6. 3. 6. 7. 9. 7.]]After splitting:[array([[4., 7.], [6., 3.]]), array([[6., 3.], [6., 7.]]), array([[2., 6.], [9., 7.]])]
### numpy.vsplit
numpy.vsplit splits vertically along the vertical axis. Its usage is the same as hsplit.
## Example
import numpy as np a = np.arange(16).reshape(4,4)print('First array:')print(a)print('n')print('Vertical splitting:')b = np.vsplit(a,2)print(b)
The output is as follows:
First array:[ ]Vertical splitting:[array([[0, 1, 2, 3], [4, 5, 6, 7]]), array([[ 8, 9, 10, 11], [12, 13, 14, 15]])]
* * *
## Adding and Deleting Array Elements
| Function | Element and Description |
| --- | --- |
| `resize` | Return a new array with the specified shape |
| `append` | Append values to the end of an array |
| `insert` | Insert values along the given axis before the given indices |
| `delete` | Delete sub-arrays along an axis and return a new array with the remaining elements |
| `unique` | Find the unique elements of an array |
### numpy.resize
The numpy.resize function returns a new array with the specified shape.
If the new array is larger than the original array, then the new array is filled with copies of the original array.
numpy.resize(arr, shape)
Parameter description:
* `arr`: The array to be resized.
* `shape`: The new shape of the returned array.
## Example
import numpy as np a = np.array([[1,2,3],[4,5,6]])print('First array:')print(a)print('n')print('Shape of the first array:')print(a.shape)print('n')b = np.resize(a, (3,2))print('Second array:')print(b)print('n')print('Shape of the second array:')print(b.shape)print('n')print('Resizing the second array:')b = np.resize(a,(3,3))print(b)
The output is as follows:
First array:[ ]Shape of the first array:(2, 3)Second array:[ ]Shape of the second array:(3, 2)Resizing the second array:[ ]
### numpy.append
The numpy.append function appends values to the end of an array. The append operation allocates the entire new array and copies the original array into it. Furthermore, the dimensions of the input arrays must match, otherwise a ValueError will be generated.
The append function always returns a one-dimensional array.
numpy.append(arr, values, axis=None)
Parameter description:
* `arr`: The input array.
* `values`: The values to be appended to `arr`. It must have the same shape as `arr` (except along the axis to be appended).
* `axis`: The default is None. When axis is not defined, the values are appended to a flattened version of the array, and the result is always a one-dimensional array! When axis is defined, it can be 0 or 1. When axis is 0, the arrays must have the same number of columns. When axis is 1, the arrays must have the same number of rows.
## Example
import numpy as np a = np.array([[1,2,3],[4,5,6]])print('First array:')print(a)print('n')print('Appending elements to the array:')print(np.append(a, [[7,8,9]],axis = 0))print('n')print('Appending elements along axis 1:')print(np.append(a, [[5,5,5],[7,8,9]],axis = 1))
The output is as follows:
First array:[ ]Appending elements to the array:[ ]Appending elements along axis 1:[ ]
### numpy.insert
The numpy.insert function inserts values along the given axis before the given indices. The function format is as follows:
numpy.insert(arr, obj, values, axis)
Parameter description:
* `arr`: The input array.
* `obj`: The index or indices before which `values` is inserted.
* `values`: The values to be inserted.
* `axis`: The axis along which to insert `values`. If axis is None, then arr is flattened first.
## Example
import numpy as np a = np.array([[1,2],[3,4],[5,6]])print('First array:')print(a)print('n')print('Insert elements at position 2:')print(np.insert(a,2,[11,12]))print('n')print('Insert elements along axis 0:')print(np.insert(a,1,,axis = 0))print('n')print('Insert elements along axis 1:')print(np.insert(a,1,,axis = 1))
The output is as follows:
First array:[ ]Insert elements at position 2:Insert elements along axis 0:[ ]Insert elements along axis 1:[ ]
### numpy.delete
The numpy.delete function returns a new array with sub-arrays along an axis deleted. The function format is as follows:
numpy.delete(arr, obj, axis)
Parameter description:
* `arr`: The input array.
* `obj`: A slice, an integer, or a tuple of integers indicating the sub-arrays to be deleted from `arr`.
* `axis`: The axis along which to delete the given sub-arrays.
## Example
import numpy as np a = np.array([[1,2],[3,4],[5,6]])print('First array:')print(a)print('n')print('Delete elements at position 2:')print(np.delete(a, 2))print('n')print('Delete elements along axis 0:')print(np.delete(a, 1, axis = 0))print('n')print('Delete elements along axis 1:')print(np.delete(a, 1, axis = 1))
The output is as follows:
First array:[ ]Delete elements at position 2:Delete elements along axis 0:[ ]Delete elements along axis 1:[ ]
### numpy.unique
The numpy.unique function finds the unique elements of an array. The function format is as follows:
numpy.unique(arr, return_index=False, return_inverse=False, return_counts=False)
Parameter description:
* `arr`: The input array.
* `return_index`: If True, the indices of the first occurrences of the unique values are returned.
* `return_inverse`: If True, the indices of the unique array that can be used to reconstruct `arr` are returned.
* `return_counts`: If True, the number of times each unique value appears in the original array is returned.
## Example
import numpy as np a = np.array([5,2,6,2,7,5,6,8,2,9,2])print('First array:')print(a)print('n')print('Unique values of the first array:')b = np.unique(a)print(b)print('n')print('Return the unique values and the indices:')b, indices = np.unique(a, return_index = True)print(b)print(indices)print('n')print('Return the unique values and the inverse:')b, indices = np.unique(a, return_inverse = True)print(b)print(indices)print('n')print('Reconstruct the original array from the unique values and inverse:')print(b)print('n')print('Return the unique values and counts:')b, indices = np.unique(a, return_counts = True)print(b)print(indices)
The output is as follows:
First array:Unique values of the first array:Return the unique values and the indices:Return the unique values and the inverse:Reconstruct the original array from the unique values and inverse:Return the unique values and counts:
YouTip