Introduction to NumPy

Overview of the course, introduction to NumPy, installation and importing.

15 min read
Beginner

Overview

Welcome to this course on NumPy for Quants!

NumPy is a scientific computing library in Python that provides a high-performance multidimensional array object and tools for working with these arrays. It is one of the most important libraries used in the field of Quantitative Finance and Artificial Intelligence.

This course aims to introduce students to NumPy and its functions and teaches all the basic fundamental concepts needed to use NumPy.

Objectives

The learning objectives of the course are set out as follows:

  • Learn how to download and install NumPy in your system.
  • Learn to create NumPy arrays using built-in functions and Python data structures.
  • Learn about basics of NumPy arrays.
  • Learn about arithmetic, statistical and transformative array operations in NumPy.
  • Learn input/output operations in NumPy.

You can expect to have all of these objectives met by the time you reach the end of this course.

Pre-requisites

For this course, you need to be familiar with the Python Programming Language. If you are new to Python, you can check out our free course on the same.

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 beginner as well as advanced Python programmers and it is used mostly by statisticians, data scientists, and engineers.

NumPy Logo

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.

Benefit of NumPy: Fast 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.

Installing NumPy using the Python Package Manager (pip)

NumPy can be installed using the Python Package Manager, called 'pip'. Using pip, you can run the following command in your command line/terminal to install NumPy:

bash
pip install numpy

To install NumPy while you are in Jupyter Notebook/Lab, you can use the exclamation (!) syntax to execute commands from the underlying operating system.

python
!pip install numpy

This will install the latest stable version of NumPy for you to import and work with.

Importing 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:

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.

python
# Checking the version of the NumPy library
print(np.__version__)

If your installed version is 2.4.2 or newer, then, you are good to go with this course!