# Python NumPy Operations – Learn Numpy Operations With Examples

Hey python learners, in Python NumPy Operations Tutorial, you will learn various operations that can be performed on numpy array. As you all know numpy is a high-performance multidimensional array library in python. But before proceeding to numpy operations, you must have some basic knowledge of numpy array. So check this tutorial –

Python NumPy Tutorial – Getting Started With NumPy

In numpy array, you can perform various operations like – finding dimension of an array, finding byte size of each element in array, finding the data type of elements and many more.

We will do all of them one by one. So follow this tutorial till the end for learning everything .

## Python NumPy Operations Tutorial – Some Basic Operations

### Finding Data Type Of The Elements

• dtype is a data type object  that describes, how the bytes in the fixed-size block of memory corresponding to an array item should be interpreted.
• So finding data type of an element write the following code.

Result

So the data type of elements of this array is int32.

### Finding Dimension Of  Array

Now we will find dimension of an array. For finding dimension of array we use ndim attribute that returns number of array dimension. So let’s see it by an example.

• Here i have created two arrays, first one is two dimensional and second one is one dimensional.

That code will give the following output –

### Finding Byte Size Of Each Element In Array

Attribute itemsize of numpy array lists the size (in bytes) of each array element. So for finding byte size of elements, write the following code snippets.

Result

In this array each element has occupied 4 bytes.

### Finding Size Of An Array

In numpy array, you can actually find the size of an array. Size of array means total number of elements that are present in the array. So Let’s see how to do that –

• size attribute of numpy array returns the size of array that means total number of elements present in an array.
• And let’s see the result –

### Finding Shape Of An Array

You can even find the shape of a numpy array. Are you thinking, what is shape of an array ?

• Shape of an array is the total numbers of rows and columns of an array.
• shape attribute is used to find the shape of an array.
• It returns the size of each dimension of an array.

So let’s see it practically, write the following code –

Result

So you can see here, array have 2 rows and 3 columns.

Till now, you have seen some basics numpy array operations. Now i will discuss some other operations that can be performed on numpy array.

## Python NumPy Operations Tutorial – Minimum, Maximum And Sum

So in this section, you will learn how to find minimum, maximum and sum of a numpy array.

### Finding Minimum

For finding minimum of numpy array, we have a min() function which returns the minimum elements of an array. So let’s see it practically –

Result

So 4 is the smallest element in this array.

### Finding Maximum

You can find maximum of array by using max() function.

• max() function returns the maximum element of an array
• Write the following code for doing this –

Result

So 9 is the maximum element of this array.

### Finding sum

• sum() method is used for finding the sum of an array.
• It returns the total of array elements.

So write the following code for finding sum –

Result

So you saw these operations, which are very easy and simple. Now it’s time to proceed towards some advanced operations which can be performed on numpy array.

## Python NumPy Operations Tutorial – Reshaping And Slicing

### Reshaping

• Sometimes you may want to change an array from a one-dimensional array into 2-dimensional array or from 2-dimensional array into a 3-dimensional array.
• In such cases reshape property in NumPy comes in handy.
• Reshaping is often used in machine learning where certain algorithms requires data to be in certain dimension.
•  reshape() gives a new shape to an array without changing its data.

The reshape() function takes 3 arguments –

• The first one is used to specify the number of blocks of data that you wants to create.
• The second one is the number of rows you want to have.
• And the third one is number of columns that you want in each of the block of data.

The default blocks of data that will be created is 1, So even if you don’t specify it, it will create one block of data with the specified number of rows and columns.

So let’s see an example for reshaping of numpy array.

Result

### Slicing

• Slicing is basically extracting a particular set of elements from your array.
• Numpy array slicing is pretty much similar to list slicing.
• To slice an array we use the colon (:) operator with a ‘start‘ and ‘end‘ index before and after the column respectively. It follows the format data[start:end]
• For understanding slicing, let’s take an example –

Let’s assume An array –

Now you have to extract the 3rd index from both of rows. So let’s see how to do that –

• 0: says all the rows, including 0 as well.
• [0:,3] prints the element of 3rd index from both of rows.

Result

So you can see, 8 is the element of index 3 of first row and  9 is the element of index 3 of second row.

Now let’s take an another example –

Let’s assume, you have an array like,

This array has 3 rows and you have to extract element of 3rd index of first and second rows. Here you cant do that [0:,3], because it will extract 3rd index’s element from all rows. Let’s see it practically. If you write the following code –

That will give you result as follows –

So you can see that it have extracted 3rd index’s element from all rows. Hence for avoiding this problem what you have to do is following –

• [0:2] will not include the element of third row.

Result

In this way, you can perform slicing in numpy array.

## Python NumPy Operations Tutorial – Square Root And Standard Deviation

Now, you will learn, how to find square root and standard deviation of numpy array. Let me show you practically.

### Standard Deviation

• numpy.std(array) computes the standard deviation along the specified axis.
• It returns the standard deviation, a measure of the spread of a distribution, of the array elements. The standard deviation is computed for the flattened array by default, otherwise over the specified axis.

### Square Root

• numpy.sqrt(array) returns the non-negative squareroot of an array, element-wise.
• array is the values whose squareroots are required.

So write the following code snippets –

Result

## Python NumPy Operations Tutorial – Arithmetic Operations

You can easily do arithmetic operations with numpy array, it is so simple. Let’s see with an example –

• Arithmetic operations take place in numpy array element wise.
• In arithmetic operations, you basically perform addition, subtraction, multiplication and division.

Let’s see it practically –

Result

## Python NumPy Operations Tutorial – Vertical And Horizontal Stacking

If you actually want to concatenate two arrays, and you can say that if my one array is a box then add another array on top of it. That is called stacking.

• Stacking can be horizontal or vertical.

For stacking, you have to do following things –