Numpy Tutorial For Beginners [With Examples]

If you want to do machine learning, then you can not avoid numpy. It is such an essential library in Python.

In this tutorial, we are going to learn the basics of numpy so that we can move forward to do machine learning and all the crazy things out there.

Numpy is a Python library that is useful in data science and machine learning. What does a library do? Well, a Python library helps you to code less and reduces your headache.

Now, let’s see what does the numpy library do, and how it can help you.

What does Numpy stand for?

Numpy stands for Numerical Python. Basically, it is a Python library that was developed by some geniuses to make you feel comfortable and easy while doing numerical operations with Python.

We have understood that numpy stands for numerical python. Now, let’s see what it is and what is the actual use of numpy.

What is Numpy?

Numpy is Python’s library to help you in linear algebra. Linear algebra is nothing but a branch of mathematics dealing with matrices.

You might have heard about Matrices and their operations. That is exactly what linear algebra is. Matrices are also known as arrays or lists in programming terms.

Linear algebra is critical for machine learning because of the huge amounts of data available. To work with these large data sets, you need to leverage matrices for sure. To make the work easy, Python has numpy for you.

Numpy is the basic package for scientific computing using Python.

Machine learning uses several libraries to do the works easily, and numpy is the most important among them. You will be using numpy for almost all the machine learning projects.

Numpy is incredibly fast as well since it has got its bindings with C libraries.

Now, you have a basic overview of numpy. Let’s see how we can install numpy in our system.

How to Install Numpy

I already have an article for setting up the machine learning environment. If you have set up your system like that, then you are good to go further.

But, if you haven’t done that, then you can install numpy using the following methods.

  • If you have anaconda, then you already have numpy in your base environment. You can use it. No need to install anything else.
  • If you want to install it in another environment, then you can go to the anaconda navigator and install numpy with the help of the GUI.
  • If you have miniconda, you can type in the following command on your command line or Anaconda prompt.
conda install numpy

This command will automatically download and install numpy on your system.

  • If you don’t have any condas, you can still do it using the command pip install numpy in your Windows command prompt. For Mac, you can use pip3 install numpy.

We have downloaded and installed numpy on our system. Now, let’s see how we can use numpy in our code.

Importing Numpy

We can import numpy into our code using the import keyword.

import numpy as np

It is a common practice among programmers to use numpy as np. What it means is, from now on, we can use np instead of using numpy in our code.

You can code in your favorite editor. I use the jupyter notebook, and I recommend that to you as well.

Creating Arrays with Numpy

With numpy, we can convert any Python lists into either 1-dimensional or 2-dimensional arrays. Most of the time, we will be dealing with 2-dimensional arrays.

1-Dimensional Array

So, let’s see how we can convert a list to an array. First of all, we initialize a list called coolList. Then, we will convert the list into an array using the array() method of numpy.

numpy cheat sheet for data science andmachine learning

2-Dimensional Array

Now, let’s see how we can have 2-dimensional arrays. So, in this case, we need to pass a multi-list to the array method. 

This code will create a 2-dimensional array or matrix.

numpy arrays

Array Within a Range

We can also create an array with values within a range using the arange method. Let’s see how to do this.

Let’s create an array with values starting from 0 and ending before 5.

numpy cheat sheet machine learning

You can also add a step to this method as the third argument. If the step is 2, then the array will skip every second number.

numpy cheat sheet data science

Array of Zeroes

If you want a matrix of all zero values, then you can create it very easily with a method in numpy called zeros. Also, if you pass in one value, it will create a 1-dimensional matrix. If you pass in a tuple of values, it will create a multi-dimensional matrix.

Let’s see an example.

numpy chaet sheet data science

Identity Matrix

You may remember the identity matrix from your maths classes, don’t you? An identity matrix is a matrix having 1 value at diagonal positions and 0 elsewhere. The identity matrix can only be a square matrix, which means the number of rows and columns should be equal.

Luckily, numpy has an in-built method called identity() to create identity matrices

numpy identity matrix

Linear Spacing in Numpy

If you want a certain number of points between two numbers, then you can easily get that using the linspace method in numpy.

For example, let’s say we want 10 points between 3 and 8. This is how we do it in bumpy:

numpy cheat sheet for data science

You can see that we got 10 points between 3 and 8 in the output.

Converting 1-D arrays into 2-D arrays

We can convert a one-dimensional array into a 2-dimensional array by using a method called reshape. We have to pass in the number of rows and columns needed for our 2D array to the reshape method.

Let’s see an example.

converting 1D array into 2D

Note that we have 12 elements in the oneDArray, and we have 12 elements(3*4) in the twoDArray. Make sure that we use the correct arguments for the reshape method.

If the arguments are not correct, then it will give a value error.

Indexing and Slicing an Array

Indexing an array is just similar to what we do to index a list. Let’s see one example.

numpy indexing an array

Here, we got exactly the element at index 3 as the output.

Let’s say we want to slice the array. That is also similar to lists. 

Let’s see an example.

sliced numpy array

Note that if we change the values of this slicedArray, then the values of the actual array, which is myArray, will also change.

Let’s see an example.

np array sliced

In the above code, don’t get worried about the first line. The first line of code means that, for all the values of slicedArray, we are replacing it with 9. Then, the values in slicedArray changes as well as the corresponding values in the myArray.

So, if you don’t want to lose your original array, then you can make a copy of the array before making any changes in the slicedArray. We can use the copy() method for this purpose.

numpy copy array

Selecting 2D Arrays

For two dimensional array, we can select elements just like we do in the case of a 1-dimensional array. 

The only difference is that we need to use two indices, the first one representing the row of the element and the second one for the column.

numpy two dimensional array

Selecting a SubMatrix

Let’s say we want to select a submatrix from the given 2D array. For example, from the above-given 2D matrix, we need to select this shaded part as a submatrix.

numpy selecting a sub matrix

For that, we need to select the values from the 0th row and 1st row ([0:2]) and also from the 1st column and second column([1:]). This how we can do it.

numpy 2-dimensional array selection

Conditional Returns in Numpy

If we want only the elements in an array that are satisfying a certain condition, then we can select those values as well, just like we do in if-else conditions.

Let’s see an example of that.

numpy conditional returns

You can see that it returned only the values in the array that are greater than 7.

Numpy Array Operations

Just like we do with lists, we can do operations like addition, subtraction, multiplication, etc. with numpy arrays as well.

Let’s look at some example operations.

numpy array operations

We can do any operations on our array like this.

Cross Product and Dot Product

We can find the cross product of two matrices using the cross() method in numpy. Also, we can find the dot product of two matrices using the dot() method.

Let’s see an example.

numpy cross and dot product

Transpose of a Matrix

We can find the transpose of a matrix pretty easily using the transpose() method.

See this example.

numpy transpose

Mean, Standard Deviation and Covariance

We can find the mean, standard deviation and covariance of elements in an array using the methods mean(), std() and cov() respectively.

Let’s see some examples.

numpy mean

Just like this, there are a bunch of built-in methods available in numpy, and hence, you can do a lot of things with this powerful library.a

Conclusion

Numpy is a Python library that helps us to do numerical operations like linear algebra. Using numpy, we can create arrays or matrices and work with them.

Numpy is very important for doing machine learning and data science since we have to deal with a lot of data. It makes all the complex matrix operations simple to us using their in-built methods.

If you want to learn more about numpy, you can refer to the official numpy documentation.

If you have any doubts, comment down below. I’ll be happy to help you. Share this article if you find it useful.

Happy Coding!

Ashwin Joy

I'm the face behind Pythonista Planet. I learned my first programming language back in 2015. Ever since then, I've been learning programming and immersing myself in technology. On this site, I share everything that I've learned about computer programming.

4 thoughts on “Numpy Tutorial For Beginners [With Examples]

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Content