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.
1 2 3 |
import numpy as np arr = np.array([1,2,3]) print("Data Type : ",arr.dtype) |
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.
1 2 3 4 5 6 7 8 9 10 |
import numpy as np # create a two dimensional array arr1 = np.array([(4,5,6),(7,8,9)]) print("Dimension Of Array1 : ",arr1.ndim) # create an one dimensional array arr2 = np.array([4,5,6]) print("Dimension Of Array2 : ",arr2.ndim) |
- 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.
1 2 3 4 5 6 7 8 9 |
import numpy as np arr1 = np.array([(4,5,6),(7,8,9)]) print("Itemsize Of Array1 : ",arr1.itemsize, "bytes") arr2 = np.array([4,5,6]) print("Itemsize Of Array2 : ",arr2.itemsize, "bytes") |
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 –
1 2 3 4 5 6 7 8 |
import numpy as np arr1 = np.array([(4,5,6),(7,8,9)]) print("Size Of Array1 : ",arr1.size, "Elements") arr2 = np.array([4,5,6]) print("Size Of Array2 : ",arr2.size, "Elements") |
- 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 –
1 2 3 4 |
import numpy as np arr1 = np.array([(4,5,6),(7,8,9)]) print("Shape Of Array : ",arr.shape) |
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 –
1 2 3 4 5 |
import numpy as np numpy_array = np.array([(4,5,6),(7,8,9)]) print("Minimum Of Array : ", numpy_array.min()) |
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 –
1 2 3 4 5 |
import numpy as np numpy_array = np.array([(4,5,6),(7,8,9)]) print("Maximum Of Array : ",numpy_array.max()) |
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 –
1 2 3 4 5 |
import numpy as np numpy_array = np.array([(4,5,6),(7,8,9)]) print("Sum Of Array : ",numpy_array.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.
1 2 3 4 5 6 7 8 9 10 11 12 |
import numpy as np numpy_array1 = np.array([1,2,8,9,6,5]) # reshaping 1-D array into 2-D print("1-D reshaped into 2-D\n", numpy_array1.reshape(2,3)) numpy_array = np.array([(4,5,6),(7,8,9)]) print("\nReshaped Array with 4 rows and 2 columns\n", numpy_array.reshape(4,2)) #reshaping 2-D array into 3-D array print("\nReshaped in 3D\n", numpy_array.reshape(2,2,2)) |
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 –
1 |
numpy_array = np.array([(4,5,6),(7,8,9)]) |
Now you have to extract the 3rd index from both of rows. So let’s see how to do that –
1 2 3 4 5 |
import numpy as np numpy_array = np.array([(4,5,6),(7,8,9)]) print("Element of 3rd index from both rows :\n",numpy_array[0:,3]) |
- 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,
1 |
numpy_array = np.array([(4,5,6),(7,8,9),(8,2,3,1)]) |
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 –
1 2 3 4 5 |
import numpy as np numpy_array = np.array([(4,5,6),(7,8,9),(8,2,3,1)]) print(numpy_array[0:,3]) |
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 –
1 2 3 4 5 |
import numpy as np numpy_array = np.array([(4,5,6),(7,8,9),(8,2,3,1)]) print("Element of 3rd index from first and second rows\n",numpy_array[0:2,3]) |
- [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 square–root of an array, element-wise.
- array is the values whose square–roots are required.
So write the following code snippets –
1 2 3 4 5 6 7 |
import numpy as np arr1 = np.array([(4,5,6),(7,8,9)]) print("Square Root :\n ",np.sqrt(arr1)) print("\nStandard Deviation :\n ",np.std(arr1)) |
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 –
1 2 3 4 5 6 7 8 9 10 |
import numpy as np arr1 = np.array([(4,5,6),(7,8,9)]) arr2 = np.array([(1,2,6),(2,4,5)]) # Arithmetic Operations print("Addition :\n ",arr1+arr2) print("\nSubtraction :\n ",arr1-arr2) print("\nMultiplication :\n ",arr1*arr2) print("\nDivision :\n ",arr1/arr2) |
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 –
Related Articles :
- Python Template Class Tutorial For Beginners
- Python Binary To Decimal Tutorial With Examples
- Best Online Python Compilers – Learn Coding On Online Compilers
- Python Regular Expression Example : Learn RegEx with Python
- 6 Best Python IDEs for Windows to Make You More Productive
So this was all about Python NumPy Operations Tutorial. If you have any doubt regarding this tutorial then please leave your comment. And please share this with others and help them. Thanks Everyone
Leave a Reply