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!
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:
# 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.
Once you've installed NumPy you can import it as a library:
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 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.
my_list = [1,2,3]
my_list
Use array()
method to create an array by directly converting a list:
np.array(my_list)
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
my_matrix
Use array()
method to create an array by directly converting a list of lists:
np.array(my_matrix)
np.arange(0,10)
np.arange(0,11,2)
Generate arrays of zeros or ones
np.zeros(3)
np.zeros((5,5))
np.ones(3)
np.ones((3,3))
Return evenly (linear) spaced numbers over a specified interval.
np.linspace(0,10,3)
>>> np.linspace(0,10,50)
Creates an identity matrix
np.eye(4)
np.random.rand(2)
np.random.rand(5,5)
Return a sample (or samples) from the standard normal distribution
np.random.randn(2)
np.random.randn(5,5)
Return random integers from low
(inclusive) to high
(exclusive).
np.random.randint(1,100)
np.random.randint(1,100,10)
Let's discuss some useful attributes and methods or an array:
arr = np.arange(25)
arr
ranarr = np.random.randint(0,50,10)
ranarr
Returns an array containing the same data with a new shape.
Change arr from one dimensional to 5X5 matrices
arr.reshape(5,5)
Use max()
and min()
methods to find max or min values of an numpy array
ranarr
ranarr.max()
ranarr.min()
Use argmax()
and argmin()
methods to find index locations of an numpy array
ranarr.argmax()
ranarr.argmin()
arr.shape
Notice the two sets of brackets
arr.reshape(1,25)
arr.reshape(1,25).shape
arr.reshape(25,1)
arr.reshape(25,1).shape
You can also grab the data type of the object in the array:
arr.dtype