Complete Course Summary
Python Fundamentals, Model Evaluation & Regularization,
Dimensionality Reduction, Classi
cation, Clustering &
Bayesian Updating, and Model Averaging
Lecture Notes, Tutorials, Worked Exam Exercises & Quick-Reference Guide
,Data Science Methods Complete Course Summary 1
Contents
I Lecture Notes & Tutorials 3
1 Introduction to Python 3
1.1 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.6 Input, Functions & Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.7 String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.8 Mathematical Sequences (Fibonacci) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.9 NumPy Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.10 Pandas Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.11 Function Calls & Code Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2 Object-Oriented Programming 5
2.1 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Attributes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.4 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Practical Example: Linear Regression Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 Model Evaluation 6
3.1 Linear Model for Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Determine Model Parameters using Maximum Likelihood . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Types of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4 Bias-Variance Decomposition and Trade-O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.5 Model Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.6 Data-Rich Situations: Many Data Available . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.7 Cross-Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Dimensionality & Nonparametric Methods 9
4.1 Shrinkage Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2 Dimension Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.3 Principal Component Analysis (PCA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.4 Selecting the Optimal L . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.5 Nonparametric Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 LDA and Logistic Regression 12
5.1 Classi
cation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.2 Bayes Classi
er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5.3 Decision Boundary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.4 Linear Classi
ers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.5 Linear Probability Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.6 Linear Discriminant Analysis (LDA) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
5.7 Quadratic Discriminant Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.8 Receiver Operating Characteristic (ROC) Curve . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.9 Logistic Regression Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.10 Stochastic Gradient Descent (SGD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.11 Comparison Logistic Regression and LDA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
6 Clustering and Bayesian Updating 17
6.1 Clustering Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.2 K-means Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.3 Hierarchical Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.4 Bayesian Updating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.5 Bayes Estimator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.6 Priors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.7 Bayesian Linear Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.8 Bayes Estimator (Squared Loss) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
,Data Science Methods Complete Course Summary 2
6.9 Bayes Estimator (Absolute Loss) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7 Model Averaging 21
7.1 Model Averaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.2 The Simplest Way for Model Averaging Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.3 Granger-Ramanathan Averaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.4 Expectation-Maximization (EM) Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
II Additional Worked Exam Exercises 25
8 Generalization Error & Bias-Variance (Exam Exercises) 26
9 PCA & Dimensionality Reduction (Exam Exercises) 26
10 Classi
cation: LDA & k-NN (Exam Exercises) 28
11 Clustering & Bayesian Updating (Exam Exercises) 30
12 Code Questions (Debugging & Reading Python) 31
13 Machine Learning Methods Quick Reference 32
13.1 1. Classi
cation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
13.2 2. Non-Parametric Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
13.3 3. Regression & Regularization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
13.4 4. Optimization Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
13.5 5. Dimensionality Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
13.6 6. Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
13.7 7. Fisher's Discriminant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
13.8 Key Formulas Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
, Data Science Methods Complete Course Summary 3
Part I
Lecture Notes & Tutorials
1 Introduction to Python
1.1 Lists
# Create
a = [1 ,2 ,3]
# Add element
a . append (4)
# Insert element at position
a . insert (2 , " June " )
# Negative indexing
a [ -1] # last element
# Loop through list
for i in a :
print ( i *2)
# Array indexing with indices
indices = [0 ,2 ,1]
result = a [ indices ]
1.2 Tuples
# Create
t = (2 ,2 ,3)
# Immutable ! Cannot be changed
1.3 Dictionaries
# Create
d = { 'a ' :1}
# Add element
d [ 'b '] = 2
# Remove element
d . pop ( 'a ')
1.4 Sets
# Unique values , unordered
s = {1 ,2 ,3}
# Add element
s . add (4)
1.5 Control Flow
# If - else
if x <0:
print ( " negative " )
elif x ==0:
pass
else :
print ( " positive " )
# For - loop
a = [1 ,2 ,3 ,4]
for i in a :
print ( i *2)
# While - loop with stopping criterion
n = 5
while n < 10:
print ( n *( n -1) /2)