NumPy is a popular Python library in the data science community, used for numerical computations. The wide popularity in its usage can be attributed to many reasons. One of which is the fact that many other libraries used for other mathematical operations are directly or indirectly built on top of NumPy. Examples of such libraries include pandas, SciPy, sci-kit learn, etc.
The mathematical operations are usually done by converting lists to arrays of n-dimensions. In this series, you will have all the knowledge of NumPy you need to jumpstart your data science career. However, this tutorial will give a gentle introduction to Numpy and Why Numpy is useful.
What is NumPy in Python
NumPy is an open-source Python library that does powerful mathematical operations. The name NumPy is a conglomeration of two words, Numerical Python. Since NumPy’s birth, the library has not only been useful for data scientists but other STEM fields as well. The data structure NumPy works with a data structure called an n-dimensional array (or array for short). This is simply a matrix of n-dimensions. Although NumPy is made for Python, it can also be used in other languages such as C and C++.
When dealing with large computations in matrix or array, NumPy is the best tool for the job. NumPy can also work with random numbers, Fourier transforms, linear algebra, and so on.
What are the Benefits of NumPy
- NumPy is way faster in operations: Because the language was written in C, tasks run faster. Typically to the tune of nanoseconds
- NumPy has a myriad of methods and functions that can do tough mathematical operations in one line. This makes your code cleaner and clearer.
- Numpy is user-friendly and has a wide community: The library has an open-source project, has many contributors who work tirelessly to improve the operations in NumPy. Thus, the library is not just fast, but also bug-free and user-friendly.
Next, we will discuss how to install NumPy on your machine.
How to Install NumPy on your machine
There are numerous ways to install NumPy on your PC. One of the ways is by using the pip command. Pip is a popular package manager that is used to install Python packages easily.
How to install NumPy with pip
When using pip, you need to specify the virtual environment you want it installed in and before installing. If you don’t have a virtual environment, learn how to create one.
How to create a virtual environment in Python
To create a virtual environment, go to a directory you want the environment installed and open the command prompt from that directory.
Once you are in that directory, type in the following command.
python -m venv name_of_virtual_enviroment
Replace name_of_virtual_enviroment
with any name you wish to call your virtual environment. After running the code, a folder with that name will be created. Inside the folder, there are 3 subfolders which are the Include, Lib, and Scripts folder as well as a CFG file.
The last step is to activate the virtual environment. Since you are already in the virtual environment directory, type in the following commands.
name_of_virtual_enviroment\Scripts\activate
Once done, the name of the virtual environment is encapsulated in brackets. This indicates the virtual environment is activated.
Now, we can install NumPy using pip in this virtual environment.
Navigate to the directory of the virtual environment using
cd name_of_virtual_enviroment
Finally, type the following command to install NumPy with pip
pip install numpy
And that’s it. You have NumPy installed on your PC.
Installing NumPy through Anaconda
Using Anaconda distributions is a way to install the most important packages needed in data science, without worrying about managing virtual environments. NumPy has a foundational library that comes preinstalled when you install Anaconda on your machine. To install Anaconda on your machine, refer to this article where we gave a rundown of the process.