Working with objects using NumPy

Working with objects using NumPy

2020, Sep 21    

Much like the previous post, I will keep things very simple in this first tutorial using Python. To be honest, this is also a tutorial for me since it’s my first time using JupyterLab and GitHub together. For those who are also new to Python, be sure to visit this overview of JupyterLab. Additionally, this is a good tutorial on using Markdown with Jupyter Noteboks.

Part 1: Basic Calculations

In this section we will use Python as a simple calculator. I have found JupyterLab much more friendly to use and its flow reminds me a bit of RStudio. We will also introduce a few basic built-in functions:

#first we will perform basic math:
2+2
4
6-1
5
4*8
32
#Next we will do a more complex operation: 
((81/9)**2)+20
101.0
#for the square root of 64 we first import the math module: 
#then we call the function:
import math
math.sqrt(64)
#note that in Python 3 the syntax is module.function(argument)
8.0
#suppose we wanted to change the base to find log(36), i.e. base 10:
math.log(36,10)
1.556302500767287
#for ln(36) we do:
math.log(36)
3.58351893845611

Part 2: Working with Vectors

In this section we will use Python to create and manipulate vectors, using different functions. The basis for any array manipulation is NumPy. For a much more thorough tutorial on arrays and using NumPy please consult this Introduction to NumPy.

#suppose we wanted to create a vector x 
#x will contain the numbers 45. 31, 60, 80, 32
#first we have to import numpy:
import numpy as np
#now we create the vector using the array function:
x = np.array([45,31,60,80,32])
#when we call for x we can use the function print():
print(x)
[45 31 60 80 32]
#suppose we wanted to find the mean of x, we simply do:
x.mean()
49.6
#for the standard deviation we perform:
x.std()
18.48891559827131
#now suppose we want to create another vector called y:
y = np.array([28,90,45,60,70])
#we can add the elements of these vectors together by performing:
x+y 
array([ 73, 121, 105, 140, 102])
#we can also multiply the elements of these vectors by doing:
x*y
array([1260, 2790, 2700, 4800, 2240])
#finally, we can create the vector z which adds x and y and get its mean:
z=x+y
z.mean()
108.2

Part 3: Working with Matrices

For this last section we will work with matrices using Python. The logic is quite similar as to the previous section since we are also operating with arrays using NumPy. The idea here is to perform basic linear algebra which can serve as a foundation for deeper analysis:

#Suppose we want to create a 2x3 matrix called and call it a:
a = np.array([[1., 2., 3.], [4., 5., 6.]])
#we manually separated each row to create this 2x3 matrix:
a
array([[1., 2., 3.],
       [4., 5., 6.]])
#now we can create a 3x2 matrix and call it b:
b = np.array([[6., 23.], [-1, 7], [8, 9]])
b
array([[ 6., 23.],
       [-1.,  7.],
       [ 8.,  9.]])
#to multiply axb we use the dot function and shorten our code by doing:
a.dot(b)
array([[ 28.,  64.],
       [ 67., 181.]])
#to use more matrix functions we can create a 3x3 matrix and call it c:
c= np.array([[1., 8., 10.], [2., 5., 11.],[9.,4.,12.]])
#to access these matrix functions we will use linalg which comes with NumPy:
from numpy.linalg import inv,det
#to find the inverse of c we use inv():
inv(c)
array([[ 0.06504065, -0.22764228,  0.15447154],
       [ 0.30487805, -0.31707317,  0.03658537],
       [-0.1504065 ,  0.27642276, -0.04471545]])
#to find the determinant of c we use det():
det(c)
246.00000000000014

Conclusions

I hope this simple tutorial was useful if you are unfamiliar with Python and NumPy. There are subtle differences with how R and Python read the matrix imputs but nothing major. Thank you for coming along!