To continue our series of Python libraries, today, I would like to introduce to you Numpy which is also known as one of the famous scientific computing libraries in Python. Some of you may be familiar with MATLAB before and know how efficient it is when performing calculations with matrices.
Numpy helps us do the same by supporting tools to manage and manipulate multi-dimensional arrays such as matrices. It plays a vital role in the field of computing. Most of the time, when I was working with Machine Learning, I often used Numpy (along with Scipy sometimes) to manipulate the data (such as concatenate two set of data or remove outliers) before fitting it into a model. Of course, there are other libraries help you process the data easier. One of the examples of this is Pandas which is one of the emerging stars in financial data analysis. However, I was familiar with MATLAB when I was a student in an engineering school and feel more conventional when using Numpy. Moreover, I assure you that if you are familiar with Numpy, it will not be difficult for you to turn to use another kind of scientific computing libraries such as Pandas mentioned above.
In fact, Numpy is used for not only linear algebra but also signal processing and statistics. However, these two last are beyond the scope of this tutorial, and I will talk about them later in another.
Now, before rolling up your sleeves to explore how miracle Numpy is, we first need to install it.
The version of Python I use in this tutorial is 2.7.11. However, you can also use all syntaxes for others, regardless the version of 3.* or 2.*.
To install Numpy, there are 2 ways:
Using source file
The first is to access the link: https://sourceforge.net/projects/numpy/files/NumPy/. Then download and install the version that is suitable for your Python version (2.* or 3*) and your operating system (Window, Linux, or MacOS).
The second way is to use “pip” command. If you have not yet been familiar with “pip” or don’t know what it is. It is OK. Please refer my tutorial on how to install and use “pip” in Python.
Now, open your command window and run this command:
pip install numpy
for Python version 2.* or this command
pip3 install numpy
for Python version 3.*.
If it is not successful for Linux. Try to use
sudo pip install numpy
sudo pip3 install numpy
for the Python version 2.* or 3.*, respectively.
Basic linear algebra with Numpy
First, try to create some vectors and matrices yourself with Numpy.
Next, use some Numpy built-in methods to create some special matrices.
Then, let’s try to use some operators (such as addition, subtraction, or dot product) between two matrices.
And other performance like concatenate two arrays or reshape one.
That’s all about basic linear algebra with the Numpy library that I would like to talk about, by which, you could do a lot of things in data analysis. Of course, what presented above is not all of Numpy. There is more and more miracle that the Numpy can do such as analyzing statistics or processing a signal. For more details, you could find and read its documentation.
In the next tutorial, you will learn how to visualize data and plot some basic graphs (such as the line or bar chart) by using a popular library named Matplotlib. In this post, I promise we will draw some fun with math functions I found on the Internet. Until then, hope you enjoy my posts.