Note 1
Python Basics for Machine Learning
To develop a model represented in abstract math, we need a programming language. If you ask any ML engineer on the language they use the most, I can guarantee you that it’s Python (well most likely). The reason is simple: syntax and libraries. Although all heavy computations in ML is mostly done by C++, Python is way easier to write than C++. You don’t have to deal with segmentation faults and spend more time adding the missing semicolon than actually debugging. Moreover, Python has popular libraries like NumPy, Matplotlib, and PyTorch that will make our lives easier. Therefore, we will discuss about basic syntax, NumPy, and Matplotlib for ML in this note. Don’t worry, we will discuss about PyTorch in latter notes as it requires ML concepts.
1.1 Basic Syntax
As always, let’s get started by being more familiar with the nature of Python. Unlike most other languages, indentation is a vital factor in Python. Two spaces, four spaces, doesn’t really matter, let’s use four spaces as it is more common and readable. Here is the Hello World example.
Please note that this Jupyter Notebook can be
found in jupyter/ directory in GitHub under the root
directory for these notes. Please feel free to download and follow
along! The next example is a list of simple operations that we can do
with Python.
1import math 2import numpy as np 3import matplotlib.pyplot as plt 4%matplotlib inline 5 6print(1 + 2) # Addition 7print(3 - 9) # Subtraction 8print(4 * 3) # Multiplication 9print(5 ** 2) # Power 10print(math.log(5)) # log(a, base), default base is e 11print(math.exp(2)) # e^2 12print(math.e) # Accessing constants
3 -6 12 25 1.6094379124341003 7.38905609893065 2.718281828459045
As you can see in Cell 2, we need module “math” to access constants and functions like \(e\), \(\pi \), and \(\log _a b\).
Definition 1.1.1
We can achieve the same effect with variables, or a place with name that stores a value. A function is a reusable block of code dedicated for specific tasks.
Let’s take a look at an example of how function and variable in addition to the operations above can be used to find the approximation of the derivative of \(f(x) = x^2 + 5\) at \(x = 3\).
1def f(x): 2 return x**2 + 5 3 4a = 3 5h = 0.0001 6der = (f(a + h) - f(a))/h 7print(der)
6.000100000012054
Let’s check if our answer is reasonable. Using the Power Rule, we know that \(f'(x) = 2x\). Because \(f(3) = 2 \cdot 3 = 6\) and \(6.000100000012054\) and \(6\) are somewhat close, our result is reasonable.
Definition 1.1.2
Lists are dynamic and ordered collection of elements of potentially different data types. An index is the position of an element in the list. The index of the first item is \(0\).
Below are examples of what we can do with lists.
1# A list can be empty 2a = [] 3print(a) 4 5# We can append a value at the end 6a = [1, ’hi’, 3] 7a.append(’bye’) 8print(a) 9 10# Remove an element 11a.remove(’hi’) 12print(a) 13 14# Access an element with index 15print(a[2]) 16 17# Find the number of elements 18print(len(a))
[] [1, ’hi’, 3, ’bye’] [1, 3, ’bye’] bye 3
What do we do if we need to access multiple elements? This is where slicing shines. Consider the following example.
1# a[i:j] to access from index ’i’ to index ’j-1’ 2a = [1, 2, 3, 4, 5, 6, 7, 8, 9] 3print(a[1:5]) 4 5# Omit ’i’ or ’j’ to define only the start or end values 6print(a[:6]) 7print(a[3:])
[2, 3, 4, 5] [1, 2, 3, 4, 5, 6] [4, 5, 6, 7, 8, 9]
Now that we know basic operations, lists, and how we can use functions, let’s discuss about conditions and loops.
Definition 1.1.3
Conditional statements are statements that performs certain tasks under specific conditions. Loops are structure that repeats a block of code until they are explicitly told to end. One more definition that will make our lives easier is increment, which is an assignment operator that augments certain values.
Here is an example of conditional statements.
1# If, Else, Elif (else if) 2banana_num = 10 3if banana_num < 10: 4 print("There are less than ten bananas") 5elif banana_num == 10: 6 print("There are ten bananas") 7else: 8 print("There are more than ten bananas")
There are ten bananas
Here, because “\(=\)” is reserved, we use “\(==\)” for “equal to.” We could also do less than or equal to, greater than or equal to, and not equal to with \(<=\), \(>=\), and \(!=\) respectively.
Regarding loops, “for loop” and “while loop” are two primary methods. Below are examples of how both loops can be used.
1# While loop 2love_banana = True 3banana_num = 0 4 5while love_banana and banana_num < 5: 6 banana_num += 1 # Increment 7 print(banana_num) 8 9print() 10 11# For loop 12my_banana = [’yellow’, ’green’, ’mushy’, ’sweet’] 13 14for i in range(len(my_banana)): 15 print(my_banana[i])
1 2 3 4 5 yellow green mushy sweet
First, love_banana = True is a Boolean value.
In the while loop, what it does is while love_banana is True,
we will increase the value banana_num by 1 and print them. Notice that the
increment with banana_num += 1 is a shorter form of
writing banana_num =
banana_num + 1. Moreover, we use
and to
make the loop continue if love_banana is true and banana_num is less
than five.
In the second loop, we listed all elements in the
list my_banana. The loop for i in range() tells Python to
go through the code
print(my_banana[i]) from \(i = 0\) to \(\text {range(len(my\_banana))} - 1\).
We discussed very basic Python syntax that we must know to understand the following notes. Let’s discuss core libraries NumPy and Matplotlib now, and save PyTorch for later.