1-NumPy-Arrays




*Copyright Pierian Data 2017*
*For more information, visit us at www.pieriandata.com*

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:

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 [1]:
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

We can create an array by directly converting a list or list of lists:


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


Out[2]:
[1, 2, 3]

In [3]:
np.array(my_list)


Out[3]:
array([1, 2, 3])

In [4]:
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
my_matrix


Out[4]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [5]:
np.array(my_matrix)


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

Built-in Methods

There are lots of built-in ways to generate Arrays

arange

Return evenly spaced values within a given interval.


In [12]:
np.arange(0,10)


Out[12]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [23]:
np.arange(0,11,2)


Out[23]:
array([ 0,  2,  4,  6,  8, 10])

zeros and ones

Generate arrays of zeros or ones


In [24]:
np.zeros(3)


Out[24]:
array([ 0.,  0.,  0.])

In [13]:
np.zeros((5,5,5))


Out[13]:
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.]],

       [[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.]],

       [[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.]],

       [[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.]],

       [[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 [27]:
np.ones(3)


Out[27]:
array([ 1.,  1.,  1.])

In [28]:
np.ones((3,3))


Out[28]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

linspace

Return evenly spaced numbers over a specified interval.


In [18]:
np.linspace(0,10,10)


Out[18]:
array([ 0.        ,  1.11111111,  2.22222222,  3.33333333,  4.44444444,
        5.55555556,  6.66666667,  7.77777778,  8.88888889, 10.        ])

In [19]:
np.linspace(0,10,50)


Out[19]:
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 [20]:
np.eye(8)


Out[20]:
array([[1., 0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0., 0., 0.],
       [0., 0., 0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 0., 0., 1., 0.],
       [0., 0., 0., 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 [28]:
np.random.rand(2)


Out[28]:
array([0.21589564, 0.94175496])

In [34]:
np.random.rand(5,5)


Out[34]:
array([[0.77066949, 0.90225307, 0.34476343, 0.26144856, 0.48176571],
       [0.1365046 , 0.69934283, 0.73300177, 0.07508835, 0.84406945],
       [0.06769905, 0.84890882, 0.60312278, 0.65319654, 0.76926603],
       [0.32477421, 0.06107923, 0.75472663, 0.09285217, 0.89230858],
       [0.88681012, 0.11509476, 0.81308108, 0.01592466, 0.6391925 ]])

randn

Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform:


In [48]:
np.random.randn(2)


Out[48]:
array([-0.27954018,  0.90078368])

In [35]:
np.random.randn(5,5)


Out[35]:
array([[-2.1476588 , -0.20744682, -0.4151857 , -0.52208823, -1.40996739],
       [ 2.04825225, -1.1412211 ,  0.2296005 ,  0.09160092,  0.4537829 ],
       [ 0.92415688, -0.41397491,  0.05145198, -1.64688033,  0.04449543],
       [-1.14738693, -0.5012143 ,  1.28032748, -1.15230653,  0.4570357 ],
       [ 1.13571982,  0.3591823 ,  0.14742394, -0.37607002,  0.26339737]])

randint

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


In [36]:
np.random.randint(1,100)


Out[36]:
81

In [37]:
np.random.randint(1,100,3)


Out[37]:
array([54, 85, 75])

Array Attributes and Methods

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


In [51]:
arr = np.arange(30)
ranarr = np.random.randint(0,50,10)

In [40]:
arr


Out[40]:
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 [49]:
ranarr


Out[49]:
array([36, 10, 10, 26, 36, 11, 46, 48,  4, 33])

Reshape

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


In [53]:
arr.reshape(3,10)


Out[53]:
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, 25, 26, 27, 28, 29]])

max,min,argmax,argmin

These are useful methods for finding max or min values. Or to find their index locations using argmin or argmax


In [54]:
ranarr


Out[54]:
array([22,  2, 20,  8, 46, 49, 37, 44,  9, 14])

In [55]:
ranarr.max()


Out[55]:
49

In [56]:
ranarr.argmax()


Out[56]:
5

In [58]:
ranarr.min()


Out[58]:
2

In [59]:
ranarr.argmin()


Out[59]:
1

Shape

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


In [60]:
# Vector
arr.shape


Out[60]:
(30,)

In [62]:
# Notice the two sets of brackets
arr.reshape(1,30)


Out[62]:
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, 25, 26, 27, 28, 29]])

In [63]:
arr.reshape(1,30).shape


Out[63]:
(1, 30)

In [64]:
arr.reshape(30,1)


Out[64]:
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],
       [25],
       [26],
       [27],
       [28],
       [29]])

In [65]:
arr.reshape(30,1).shape


Out[65]:
(30, 1)

dtype

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


In [66]:
arr.dtype


Out[66]:
dtype('int32')

Great Job!