Introduction to NumPyNovember 24, 2020 2021-02-26 21:49
Introduction to NumPy
Introduction to NumPy
Your first lesson on NumPy is here! In this lesson, you will be learning about NumPy including how to install it as well as how to import it into Python.
If you follow the steps mentioned in this lesson properly, you will not face any difficulty later down the course when working on concrete NumPy-related problems.
What is NumPy?
NumPy, or Numerical Python, is an open-source Python library that helps you perform simple as well as complex computations on numerical data. It is the go-to scientific computation library for beginners as well as advanced Python programmers and it is used mostly by statisticians, data scientists, and engineers.
The popularity behind NumPy is credited to its in-built capability of working with arrays and matrix-like data structures. On top of that, the library provides a large set of functions that are optimized to work on multi-dimensional arrays of data, also known as, n-dimensional arrays.
The first stable version of NumPy was released by Travis Oliphant in 2005 as an effort to unify the Python community around a single package to work with arrays.
Why is NumPy so fast at performing numerical computations?
Traditionally, Python programmers wrote explicit for-loops in a nested format to work on nested arrays. This was slow as well as inefficient and thus, NumPy addressed this problem by working on making these operations much faster.
As a result, NumPy started using vectorized forms of arrays (termed as, ‘vectorization’) and over the years, the library has been further improved and optimized to perform numerical operations on vectors. The benefits of vectorization in NumPy are as follows:
- Vectorized code is clear, concise, and easy to read.
- It removes the need for explicit for-loops to work on arrays. This makes the code feel more ‘Pythonic’.
- The code resembles standard mathematical notation.
- The number of potential (bug) encounters decreases as only a few lines of code are needed to perform numerical computation.
Install NumPy using the Python Package Manager
NumPy can be installed using the Python Package Manager, called ‘pip’. Therefore, please make sure to install Python before installing NumPy in your system.
Using pip, you can run the following command in your command line/terminal to install NumPy:
$ pip install numpy
This will install the latest stable version of NumPy for you to import and work with.
Note: If you use Anaconda, you can refer to NumPy’s official documentation to install NumPy: Read the documentation.
Import NumPy in Python
Once you’ve installed NumPy, you can use your favorite IDE (PyCharm, Jupyter Notebook, etc.) or the Python shell to import the library and use it.
It is a general convention to import NumPy as
np in your Python code and you will find that a lot of Python programmers do the same. The following block of code illustrates how to import NumPy in Python:
# Importing the NumPy library as np import numpy as np
If running this line of code doesn’t give you an error, then, you’ve successfully installed and imported NumPy in Python.
Next, you can check the version of the installed NumPy library by printing out the
__version__ attribute off of the NumPy package.
# Importing the NumPy library as np import numpy as np # Checking the version of the NumPy library print(np.__version__)
If your installed version is 1.18.0 or newer, then, you are good to go with this course!
That is it for your introduction to NumPy. In the next lesson, we will dive deep into NumPy arrays and start coding using real NumPy-based examples.