Table of Contents
ToggleSeveral routines are available in NumPy package for manipulation of elements in ndarray object. They can be classified into the following types −
In NumPy, to change shape is to alter the shape of arrays without changing their data −
| Sr.No. | Shape & Description |
|---|---|
| 1 | reshape()Gives a new shape to an array without changing its data |
| 2 | flat()A 1-D iterator over the array |
| 3 | flatten()Returns a copy of the array collapsed into one dimension |
| 4 | ravel()Returns a contiguous flattened array |
| 5 | pad()Returns a padded array with shape increased according to pad_width |
The NumPy transpose operations swap rows and columns in 2D arrays or rearrange axes in higher-dimensional arrays −
| Sr.No. | Operation & Description |
|---|---|
| 1 | transposePermutes the dimensions of an array |
| 2 | ndarray.TSame as self.transpose() |
| 3 | rollaxisRolls the specified axis backwards |
| 4 | swapaxesInterchanges the two axes of an array |
| 5 | moveaxis()Move axes of an array to new positions |
Changing dimensions of arrays in NumPy involves reshaping or restructuring arrays to fit specific requirements without altering the data −
| Sr.No. | Dimension & Description |
|---|---|
| 1 | broadcastProduces an object that mimics broadcasting |
| 2 | broadcast_toBroadcasts an array to a new shape |
| 3 | expand_dimsExpands the shape of an array |
| 4 | squeezeRemoves single-dimensional entries from the shape of an array |
Joining arrays in NumPy concatenate multiple arrays along specified axes −
| Sr.No. | Array & Description |
|---|---|
| 1 | concatenateJoins a sequence of arrays along an existing axis |
| 2 | stackJoins a sequence of arrays along a new axis |
| 3 | hstackStacks arrays in sequence horizontally (column wise) |
| 4 | vstackStacks arrays in sequence vertically (row wise) |
| 5 | dstack()Stack arrays in sequence depth wise (along third axis) |
| 6 | column_stack()Stacks arrays in sequence vertically (row wise) |
| 7 | row_stack()Stacks arrays in sequence vertically (row wise) |
Splitting arrays in NumPy splits arrays into smaller arrays along specified axes −
| Sr.No. | Array & Description |
|---|---|
| 1 | splitSplits an array into multiple sub-arrays |
| 2 | hsplitSplits an array into multiple sub-arrays horizontally (column-wise) |
| 3 | vsplitSplits an array into multiple sub-arrays vertically (row-wise) |
| 4 | dsplit()Split array into multiple sub-arrays along the 3rd axis (depth) |
| 5 | array_splitSplit an array into multiple sub-arrays |
Adding or removing elements in NumPy append elements to arrays or remove elements −
| Sr.No. | Element & Description |
|---|---|
| 1 | resizeReturns a new array with the specified shape |
| 2 | appendAppends the values to the end of an array |
| 3 | insertInserts the values along the given axis before the given indices |
| 4 | deleteReturns a new array with sub-arrays along an axis deleted |
| 5 | uniqueFinds the unique elements of an array |
In NumPy, repeating and tiling arrays are techniques used to create larger arrays by duplicating the elements of an existing array in various patterns −
| Sr.No. | Array & Description |
|---|---|
| 1 | repeat()Repeat each element of an array after themselves |
| 2 | tile()Construct an array by repeating A the number of times given by reps |
In NumPy, elements of an array can be rearranged using various methods to achieve the desired order or structure. Following are the common operations −
| Sr.No. | Array & Description |
|---|---|
| 1 | flip()Reverse the order of elements in an array along the given axis |
| 2 | fliplr()Reverse the order of elements along axis 1 (left/right) |
| 3 | flipud()Reverse the order of elements along axis 0 (up/down) |
| 4 | roll()Roll array elements along a given axis |
NumPy offers powerful tools for sorting and searching within arrays, enabling efficient data manipulation and analysis −
| Sr.No. | Array & Description |
|---|---|
| 1 | sort()Return a sorted copy of an array |
| 2 | argsort()Returns the indices that would sort an array |
| 3 | lexsort()Perform an indirect stable sort using a sequence of keys |
| 4 | searchsorted()Find indices where elements should be inserted to maintain order |
| 5 | argmax()Returns the indices of the maximum values along an axis |
| 6 | argmin()Returns the indices of the minimum values along an axis |
| 7 | nonzero()Return the indices of the elements that are non-zero |
| 8 | where()Return elements chosen from x or y depending on condition |
Set operations in NumPy involve performing mathematical set operations on arrays, such as union, intersection, difference, and checking for unique elements. These operations are particularly useful for handling and analyzing distinct values within datasets −
| Sr.No. | Array & Description |
|---|---|
| 1 | in1d()Test whether each element of a 1-D array is also present in a second array |
| 2 | intersect1d()Find the intersection of two arrays |
| 3 | setdiff1d()Find the set difference of two arrays and returns the unique values in ar1 that are not in ar2 |
| 4 | setxor1d()Find the set exclusive-or of two arrays and returns the sorted, unique values that are in only one (not both) of the input arrays |
| 5 | union1d()Find the union of two arrays and returns the unique, sorted array of values that are in either of the two input arrays |
Following are the other array operations in NumPy −
| Sr.No. | Array & Description |
|---|---|
| 1 | clip()Clip (limit) the values in an array |
| 2 | round()Evenly round to the given number of decimals |
| 3 | diagonal()Return specified diagonals |
| 4 | trace()Return the sum along diagonals of the array |
| 5 | take()Take elements from an array along an axis |
| 6 | put()Replaces specified elements of an array with given values |
| 7 | choose()Construct an array from an index array and a list of arrays to choose from |
Key Takeaway: Master NumPy array manipulation techniques at Vista Academy!
