Introduction to NumPy

Greetings! Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission at no extra cost to you. We greatly appreciate your support!

Get introduced to the NumPy library and learn how to install and import NumPy in Python!

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.

Introduction to numpy

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:

  1. Vectorized code is clear, concise, and easy to read.
  2. It removes the need for explicit for-loops to work on arrays. This makes the code feel more ‘Pythonic’.
  3. The code resembles standard mathematical notation.
  4. 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 (pip)

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

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.

Leave a Comment