Numpy is the most used Python module in the field of data science. You can do many things using Numpy like creating arrays, matrices, doing complex computational work. For beginners learning NumPy is hard and that why I have bring this tutorial on Numpy User Guide. In this entire tutorial, you will learn all about the NumPy basics and advanced tutorials with the best examples. You will learn the following things in Numpy Tutorial.
[toc]I am doing most of the code in Pycharm. Therefore you should also install it in your Personal Computer so that you learn fast and see the same code as I have in my Pycharm. There is a full tutorial that will guide you to install NumPy in Pycharm
Method: array()
Python array is different from the Numpy array. You can easily create a Numpy array from Lists using the np.array() method.
list = [1,2,3,4,5,6] list
numpy_array = np.array(list) numpy_array
To get the size of the Numpy array you can use the method size this will print out all the elements in the array. You can also use the len() method but in this case, it will return the size for the first dimension of the array that is the number of rows.
array_4x3 = np.arange(12).reshape(4,3) array_4x3 array_4x3.size #Total number of elements len(array_4x3) #Total number of Rows
Method: append()
You can add some more values to the existing Numpy array using the append() method. There are different ways to add elements to NumPy and it depends upon the dimension of the existing array. You have to use the axis parameter inside the append() method. How to Append Numpy Array and Insert Elements? is the entire tutorial for adding or appending values to the existing Numpy array.
Methods: max(),min()
To find the maximum and minimum element in the Numpy array you have to use np.max() and np.min(). You can use in one dimension and multi-dimension you will get the element. In one dimension you can easily find index or position for the max or min value but in two dimensions it’s not easy. Find Max and Min Value of Numpy Array with its index ( 1D & 2D) will guide you to entirely on this topic.
Method: sort()
You can sort the NumPy array using the sort() method. This method works for both the one dimensional array as well as a 2D array. Let’s look at the given example.
1D – Numpy Array
array_1d = np.array([10,30,20,5]) np.sort(array_1d)
2D – Numpy Array
array_2d = np.array([[10,30,40],[60,5,20]]) np.sort(array_2d)
Method: rand()
Sometimes We want to get quick values for the NumPy array. Rather than creating numbers manually, you can use NumPy rand() method. For example, I want to create a random number of size 4 then I will pass the size =4 as arguments inside the rand() method.
random_array = np.random.rand(4) random_array
You can also create 2D- Random array. Here you have to pass two values one for the row and second for the columns.
random_array_2d = np.random.rand(3,5) random_array_2d
Method: split()
You can split both the 1D or 2D Array using the split() method. Inside the method arguments that required are the array you want to split, size ( parts you want to split ), and axis value. Axis value can be 0(row-wise) and 1(column-wise). By default, it is 0. Let’s create a simple array and split it.
array_1d = np.array([10,20,30,40,50,60]) #split the array into 3 parts np.array_split(array_1d,3) #split the array into 4 parts np.array_split(array_1d,4)
You can read the dedicated article to split a numpy array to know more about splitting array for both one and two dimensions.
Method: linalg.norm()
There are various methods to normalize NumPy array and one of them is linalg.norm() method. It easily normalizes any array from 0 to 1 and returns it in a matrix form. To demonstrate it lets create a random NumPy array. Copy the code below.
array = np.random.rand(50) * 5
Now let’s normalize the array.
normalize1 = array / np.linalg.norm(array) print(normalize1)
You can see the array has been normalized and the values of the array are changed between 0 to 1.
The other methods are mentioned in the Normalize a Numpy Array Post.