NumPy

NumPy (or Numpy) is a Linear Algebra Library for Python, the reason it is so important for Finance with Python is that almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks. Plus we will use it to generate data for our analysis examples later on!

Numpy is also incredibly fast, as it has bindings to C libraries. For more info on why you would want to use Arrays instead of lists, check out this great StackOverflow post.

We will only learn the basics of NumPy, to get started we need to install it!

Installation Instructions

NumPy is already included in your environment! You are good to go if you are using pyfinance env!


For those not using the provided environment:

It is highly recommended you install Python using the Anaconda distribution to make sure all underlying dependencies (such as Linear Algebra libraries) all sync up with the use of a conda install. If you have Anaconda, install NumPy by going to your terminal or command prompt and typing:

In [1]:
# conda install numpy

If you do not have Anaconda and can not install it, please refer to Numpy's official documentation on various installation instructions.


Using NumPy

Once you've installed NumPy you can import it as a library:

In [2]:
import numpy as np

Numpy has many built-in functions and capabilities. We won't cover them all but instead we will focus on some of the most important aspects of Numpy: vectors,arrays,matrices, and number generation. Let's start by discussing arrays.

Numpy Arrays

NumPy arrays are the main way we will use Numpy throughout the course. Numpy arrays essentially come in two flavors: vectors and matrices. Vectors are strictly 1-d arrays and matrices are 2-d (but you should note a matrix can still have only one row or one column).

Let's begin our introduction by exploring how to create NumPy arrays.

Creating NumPy Arrays

From a Python List

In [3]:
my_list = [1,2,3]
my_list
Out[3]:
[1, 2, 3]

Use array() method to create an array by directly converting a list:

In [4]:
np.array(my_list)
Out[4]:
array([1, 2, 3])
In [5]:
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
my_matrix
Out[5]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Use array() method to create an array by directly converting a list of lists:

In [6]:
np.array(my_matrix)
Out[6]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Built-in Methods

There are lots of built-in ways to generate Arrays

arange()

Numpy version of range(). Think of it as array range. Return evenly spaced values within a given interval.

In [7]:
np.arange(0,10)
Out[7]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [8]:
np.arange(0,11,2)
Out[8]:
array([ 0,  2,  4,  6,  8, 10])

zeros() and ones()

Generate arrays of zeros or ones

In [9]:
np.zeros(3)
Out[9]:
array([0., 0., 0.])
In [10]:
np.zeros((5,5))
Out[10]:
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
In [11]:
np.ones(3)
Out[11]:
array([1., 1., 1.])
In [12]:
np.ones((3,3))
Out[12]:
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

linspace()

Return evenly (linear) spaced numbers over a specified interval.

In [13]:
np.linspace(0,10,3)
Out[13]:
array([ 0.,  5., 10.])
In [14]:
>>> np.linspace(0,10,50)
Out[14]:
array([ 0.        ,  0.20408163,  0.40816327,  0.6122449 ,  0.81632653,
        1.02040816,  1.2244898 ,  1.42857143,  1.63265306,  1.83673469,
        2.04081633,  2.24489796,  2.44897959,  2.65306122,  2.85714286,
        3.06122449,  3.26530612,  3.46938776,  3.67346939,  3.87755102,
        4.08163265,  4.28571429,  4.48979592,  4.69387755,  4.89795918,
        5.10204082,  5.30612245,  5.51020408,  5.71428571,  5.91836735,
        6.12244898,  6.32653061,  6.53061224,  6.73469388,  6.93877551,
        7.14285714,  7.34693878,  7.55102041,  7.75510204,  7.95918367,
        8.16326531,  8.36734694,  8.57142857,  8.7755102 ,  8.97959184,
        9.18367347,  9.3877551 ,  9.59183673,  9.79591837, 10.        ])

eye()

Creates an identity matrix

In [15]:
np.eye(4)
Out[15]:
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])

Random

Numpy also has lots of ways to create random number arrays:

rand()

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1].

In [16]:
np.random.rand(2)
Out[16]:
array([0.87916704, 0.1385669 ])
In [17]:
np.random.rand(5,5)
Out[17]:
array([[0.20476655, 0.37505409, 0.06339566, 0.80715355, 0.52858737],
       [0.50625976, 0.90220903, 0.58676641, 0.78831746, 0.07844761],
       [0.81255959, 0.70052867, 0.88542988, 0.73104945, 0.43232209],
       [0.29527599, 0.46081208, 0.52639946, 0.81086551, 0.86657184],
       [0.34028967, 0.91884004, 0.00112206, 0.2038752 , 0.22878538]])

randn()

Return a sample (or samples) from the standard normal distribution

In [18]:
np.random.randn(2)
Out[18]:
array([ 0.05439298, -1.77768474])
In [19]:
np.random.randn(5,5)
Out[19]:
array([[-0.25832542,  0.38821718, -0.82816739, -1.43573817, -0.88542781],
       [-0.01434023,  0.36709688, -0.10680847,  0.94734935, -1.37454764],
       [-0.35541926, -0.41637894, -0.4480385 , -0.83249892,  0.41875537],
       [-1.21037948, -0.17307763, -0.92942544, -1.77174491, -0.46739996],
       [-1.9756769 , -2.13419484,  0.82412021, -0.25584598,  0.64582968]])

randint()

Return random integers from low (inclusive) to high (exclusive).

In [20]:
np.random.randint(1,100)
Out[20]:
93
In [21]:
np.random.randint(1,100,10)
Out[21]:
array([57, 34,  2, 88, 92, 59, 97, 19, 80,  7])

Array Attributes and Methods

Let's discuss some useful attributes and methods or an array:

In [22]:
arr = np.arange(25)
arr
Out[22]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24])
In [23]:
ranarr = np.random.randint(0,50,10)
ranarr
Out[23]:
array([ 8, 34, 41, 20, 20, 13,  2,  1, 45, 10])

Reshape()

Returns an array containing the same data with a new shape.

Change arr from one dimensional to 5X5 matrices

In [24]:
arr.reshape(5,5)
Out[24]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

max(), min(), argmax(), argmin()

Use max() and min() methods to find max or min values of an numpy array

In [25]:
ranarr
Out[25]:
array([ 8, 34, 41, 20, 20, 13,  2,  1, 45, 10])
In [26]:
ranarr.max()
Out[26]:
45
In [27]:
ranarr.min()
Out[27]:
1

Use argmax() and argmin() methods to find index locations of an numpy array

In [28]:
ranarr.argmax()
Out[28]:
8
In [29]:
ranarr.argmin()
Out[29]:
7

.Shape, .Reshape

Shape is an attribute that arrays have (not a method):

Vector

In [30]:
arr.shape
Out[30]:
(25,)

Notice the two sets of brackets

In [31]:
arr.reshape(1,25)
Out[31]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
        16, 17, 18, 19, 20, 21, 22, 23, 24]])
In [32]:
arr.reshape(1,25).shape
Out[32]:
(1, 25)
In [33]:
arr.reshape(25,1)
Out[33]:
array([[ 0],
       [ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11],
       [12],
       [13],
       [14],
       [15],
       [16],
       [17],
       [18],
       [19],
       [20],
       [21],
       [22],
       [23],
       [24]])
In [34]:
arr.reshape(25,1).shape
Out[34]:
(25, 1)

dtype()

You can also grab the data type of the object in the array:

In [35]:
arr.dtype
Out[35]:
dtype('int32')