0% found this document useful (0 votes)
9 views6 pages

NumPy Array Manipulation

NumPy array manipulation involves changing the structure or shape of arrays without altering their data, which is essential for data transformation in numerical tasks. Key functions include reshape() for changing array shape, flatten() for creating a 1-D copy, transpose() for swapping rows and columns, and flip() for reversing element order along specified axes. These operations are crucial for preparing data in various dimensions and layouts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

NumPy Array Manipulation

NumPy array manipulation involves changing the structure or shape of arrays without altering their data, which is essential for data transformation in numerical tasks. Key functions include reshape() for changing array shape, flatten() for creating a 1-D copy, transpose() for swapping rows and columns, and flip() for reversing element order along specified axes. These operations are crucial for preparing data in various dimensions and layouts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

NumPy Array Manipulation

Array manipulation in NumPy refers to changing the structure, shape, or


layout of arrays without changing their actual data. These operations
are crucial for data transformation, reshaping, and preparation in numerical
and data science tasks.

Several routines are available in NumPy package for manipulation of


elements in ndarray object.

1. reshape() Function:

The Numpy reshape() Function is used to change the shape of an array


without altering its data. It returns a new view or array with the specified
dimensions, provided the total number of elements remains constant.

This function takes two main arguments one is the array to reshape and the
other is tuple specifying the new shape. If the new shape is not compatible
with the total number of elements then a 'ValueError' is raised.

SYNTAX

numpy.reshape(arr, newshape)

Parameters

Following are the parameters of the Numpy reshape() Function −

• arr: The input array that has to be reshaped.

• newshape: This parameter can be int or tuple of int. New shape


should be compatible to the original shape.

Example:

Let’s take a 1-D array:


In this example we reshapes a 1D array of 6 elements into a 3X2 2D array.

Note:

Or we can use (3,-1) ,Here -1 is for NumPy to decide about columns it will
depend upon your row value here , because total elements are 6 and we
have passed 3 there as rows so , the number of columns will be
automatically 2, similarly you can fix this Column and Put row as -1.

2. flatten() method:

The Numpy ndarray.flatten()method which is used to return a new 1-D


array that is a copy of the original array which is flattened.
3. transpose() Function:

The NumPy transpose operations swap rows and columns in 2D arrays or


rearrange axes in higher-dimensional arrays –

In NumPy, transpose is a mathematical operation that flips the axes of an


array, typically used to convert rows into columns and columns into rows.
4. Flip() Function

The Numpy flip() function is used to reverse the order of elements in an


array along a specified axis.

This function is helpful when we need to rearrange array elements for data
transformation or analysis. It works for both 1D and multi-dimensional arrays.

• m: The input array to be flipped.


Explanation:

• axis=0: flips the blocks (like flipping along depth).

• axis=1: flips the rows inside each block.

• axis=2: flips the columns inside each row.

You might also like