NumPy - Array Manipulation



Several routines are available in NumPy package for manipulation of elements in ndarray object. They can be classified into the following types −

Changing Shape

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

Transpose Operations

The NumPy transpose operations swap rows and columns in 2D arrays or rearrange axes in higher-dimensional arrays −

Sr.No. Operation & Description
1 transpose

Permutes the dimensions of an array

2 ndarray.T

Same as self.transpose()

3 rollaxis

Rolls the specified axis backwards

4 swapaxes

Interchanges the two axes of an array

5 moveaxis()

Move axes of an array to new positions

Changing Dimensions

Changing dimensions of arrays in NumPy involves reshaping or restructuring arrays to fit specific requirements without altering the data −

Sr.No. Dimension & Description
1 broadcast

Produces an object that mimics broadcasting

2 broadcast_to

Broadcasts an array to a new shape

3 expand_dims

Expands the shape of an array

4 squeeze

Removes single-dimensional entries from the shape of an array

Joining Arrays

Joining arrays in NumPy concatenate multiple arrays along specified axes −

Sr.No. Array & Description
1 concatenate

Joins a sequence of arrays along an existing axis

2 stack

Joins a sequence of arrays along a new axis

3 hstack

Stacks arrays in sequence horizontally (column wise)

4 vstack

Stacks 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

Splitting arrays in NumPy splits arrays into smaller arrays along specified axes −

Sr.No. Array & Description
1 split

Splits an array into multiple sub-arrays

2 hsplit

Splits an array into multiple sub-arrays horizontally (column-wise)

3 vsplit

Splits an array into multiple sub-arrays vertically (row-wise)

4 dsplit()

Split array into multiple sub-arrays along the 3rd axis (depth)

5 array_split

Split an array into multiple sub-arrays

Adding / Removing Elements

Adding or removing elements in NumPy append elements to arrays or remove elements −

Sr.No. Element & Description
1 resize

Returns a new array with the specified shape

2 append

Appends the values to the end of an array

3 insert

Inserts the values along the given axis before the given indices

4 delete

Returns a new array with sub-arrays along an axis deleted

5 unique

Finds the unique elements of an array

Repeating and Tiling Arrays

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

Rearranging Elements

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

Sorting and Searching

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

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.

Other Arrays Operations

Following are the a=other arryas opertions 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

Advertisements