Ian Langmore Daniel Krasner
,2
,Contents
I Programming Prerequisites 1
1 Unix 2
1.1 History and Culture . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 The Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.1 Standard streams . . . . . . . . . . . . . . . . . . . . . 6
1.3.2 Pipes . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.5 Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.5.1 In a nutshell . . . . . . . . . . . . . . . . . . . . . . . 10
1.5.2 More nuts and bolts . . . . . . . . . . . . . . . . . . . 10
1.6 End Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2 Version Control with Git 13
2.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 What is Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Setting Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 Online Materials . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.5 Basic Git Concepts . . . . . . . . . . . . . . . . . . . . . . . . 15
2.6 Common Git Workflows . . . . . . . . . . . . . . . . . . . . . 15
2.6.1 Linear Move from Working to Remote . . . . . . . . . 16
2.6.2 Discarding changes in your working copy . . . . . . . 17
2.6.3 Erasing changes . . . . . . . . . . . . . . . . . . . . . 17
2.6.4 Remotes . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.6.5 Merge conflicts . . . . . . . . . . . . . . . . . . . . . . 18
3 Building a Data Cleaning Pipeline with Python 19
3.1 Simple Shell Scripts . . . . . . . . . . . . . . . . . . . . . . . 19
3.2 Template for a Python CLI Utility . . . . . . . . . . . . . . . 21
i
, ii CONTENTS
II The Classic Regression Models 23
4 Notation 24
4.1 Notation for Structured Data . . . . . . . . . . . . . . . . . . 24
5 Linear Regression 26
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
5.2 Coefficient Estimation: Bayesian Formulation . . . . . . . . . 29
5.2.1 Generic setup . . . . . . . . . . . . . . . . . . . . . . . 29
5.2.2 Ideal Gaussian World . . . . . . . . . . . . . . . . . . 30
5.3 Coefficient Estimation: Optimization Formulation . . . . . . 33
5.3.1 The least squares problem and the singular value de-
composition . . . . . . . . . . . . . . . . . . . . . . . . 35
5.3.2 Overfitting examples . . . . . . . . . . . . . . . . . . . 39
5.3.3 L2 regularization . . . . . . . . . . . . . . . . . . . . . 43
5.3.4 Choosing the regularization parameter . . . . . . . . . 44
5.3.5 Numerical techniques . . . . . . . . . . . . . . . . . . 46
5.4 Variable Scaling and Transformations . . . . . . . . . . . . . 47
5.4.1 Simple variable scaling . . . . . . . . . . . . . . . . . . 48
5.4.2 Linear transformations of variables . . . . . . . . . . . 51
5.4.3 Nonlinear transformations and segmentation . . . . . 52
5.5 Error Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.6 End Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
6 Logistic Regression 55
6.1 Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.1.1 Presenter’s viewpoint . . . . . . . . . . . . . . . . . . 55
6.1.2 Classical viewpoint . . . . . . . . . . . . . . . . . . . . 56
6.1.3 Data generating viewpoint . . . . . . . . . . . . . . . . 57
6.2 Determining the regression coefficient w . . . . . . . . . . . . 58
6.3 Multinomial logistic regression . . . . . . . . . . . . . . . . . 61
6.4 Logistic regression for classification . . . . . . . . . . . . . . . 62
6.5 L1 regularization . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.6 Numerical solution . . . . . . . . . . . . . . . . . . . . . . . . 66
6.6.1 Gradient descent . . . . . . . . . . . . . . . . . . . . . 67
6.6.2 Newton’s method . . . . . . . . . . . . . . . . . . . . . 68
6.6.3 Solving the L1 regularized problem . . . . . . . . . . . 70
6.6.4 Common numerical issues . . . . . . . . . . . . . . . . 70
6.7 Model evaluation . . . . . . . . . . . . . . . . . . . . . . . . . 72
6.8 End Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73