Machine Learning Series: Linear Regression
Hello Reader,
This is the second post of the Machine Learning series, in this post, I am going to introduce the first machine learning algorithm linear regression, and the basic concepts and jargon commonly used by the insiders.
To illustrate the concept presented in the post I am going to use a python library called Scikit-learn is one of the most popular ML libraries, it is simple to use and extremely powerful. The environment I am going to use is Google Colab notebooks. You can find the notebook related to this post in my Github repository. So let's get started
Linear regression is a supervised algorithm that helps us to solve regression problems. Let's decompose this sentence:
- supervised means that the model learns during the training phase using the training examples and the expected outcomes associated with each example
- regression means that the outcome we want to predict is a real number which is different from a classification problem where we will try to predict the class of the instance among a pre-defined number of classes
Dataset
As an introduction, I am going to use a simple dataset generated through Scikit-learn. Having a dummy dataset allows us to skip the data preparation phase. I will describe in detail the end to end data science project in another post.
The dataset is generated using a linear function with some added noise, the output of the data generator is the matrix X that has size m x n that contains m rows (instances) and n columns (features). In our case n = 1 we have one feature x1 and the vector y of the labels or expected values.
below a plot that shows the feature x1 and the labels y, there is a linear correlation between the two
Once we have got our data the first thing we need to do is to slices the dataset between training and test datasets. We are going to use the training dataset to train the model and the test to verify its performance, this is a required step to verify that the model generalizes well and produces good prediction also with instances it never sees before. Scikit-learn offers a function to perform the split, in this case, I will dedicate 20% of the instances to the test set.
Prediction Model
The function that we are going to use to predict our targets is called the hypothesis and is normally denoted with an "h subscript theta". In our case as described in the previous section we know that features and outcomes are linearly correlated, so we are going to use a linear hypothesis.
A linear model makes a prediction by simply computing a weighted sum of the input features, plus a constant called the bias term also called the intercept.
The goal of the machine learning algorithm is to find the best set of parameters Theta that minimizes the cost function.
Vectorize form describe the linear algebra notation generalized on n features
- θ is the model’s parameter vector, containing the bias term θ0 and the feature weights θ1 to θn . In the ML notation θ is usually transposed as row vector.
- x is the instance’s feature vector, containing x0 to xn , with x0 always equal to 1.
- θ · x is the dot product of the vectors θ and x, which is of course equal to θ0x0 + θ1x1 + θ2x2 + ... + θnxn .
- hθ is the hypothesis function, using the model parameters θ.
To understand how good our model predictions we need to define a measure that tells us how good or bad our prediction is. The most common cost function for linear regression is the Mean Square Error (MSE) that measures the mean of the square distance between the predicted and the expected outcomes.
Let's break down the equation and explain the terms one by one
- m is the number of instances of you training dataset, typically is the number of row of our dataset
- x(i) is a feature vector with the one row and n columns, in the machine learning jargon a feature is an attribute of our instance typically the features are the columns of our dataset. The vector represent the ith instance with the related features (the ith row our dataset)
- h is the function we use to do our predictions, it is called hypothesis
- y(i) is the the ith label, a label is the correct answer for that particular instance. The supervised learning algorithms use the correct answers (labels) during the training phase to learn and gradually improve the predictions
Learning
The goal of the learning process is to find the vector Theta that minimizes the cost function, for linear regression turns out that
there is a mathematical formula to determine the best parameter in one shot called the Normal Form
The formula uses some linear algebra notation, XT is the transposed of the feature matrix X, the dot represents the matrix multiplication, X-1 represents the matrix inverse. This method is a simple way to find the optimal Theta, the cons of the normal form are that it is computationally expensive to calculate the matrix inverse it is not recommended for large datasets. In the next posts, I will describe an iterative algorithm used to learn any hypothesis called gradient descent.
Below the code used to train a linear model using Scikit-learn
and here is the cost function value calculated on the training set
Predict
Once our model is trained we can use it to perform our predictions, in this case, we used the test set. As you can see when the model predicts a set of instances that it never saw the cost is slightly higher. The code shows also two additional parameters, the intercept that represents the value θ0 or the value where our line cross the y-axis and other linear coefficients in our case one value that represents θ1
Below the prediction represented by a red line on the test dataset











Comments
Post a Comment