Linear regression is a linear model, e.g. a model that assumes a linear relationship between the input variables (x) and the single output variable (y). More specifically, that output variable (y) can be calculated from a linear combination of the input variables (x).

Univariate Linear Regression is a linear regression that has only one input parameter and one output label.

Demo Project: In this demo we will build a model that will predict Happiness.Score for the countries based on Economy.GDP.per.Capita parameter.

In [1]:

# To make debugging of linear_regression module easier we enable imported modules autoreloading feature.# By doing this you may change the code of linear_regression library and all these changes will be available here.%load_ext autoreload
%autoreload 2
# Add project root folder to module loading paths.importsyssys.path.append('../..')

In this step we will split our dataset into training and testing subsets (in proportion 80/20%).

Training data set will be used for training of our linear model. Testing dataset will be used for validating of the model. All data from testing dataset will be new to model and we may check how accurate are model predictions.

☝🏻This is the place where you might want to play with model configuration.

polynomial_degree - this parameter will allow you to add additional polynomial features of certain degree. More features - more curved the line will be.

num_iterations - this is the number of iterations that gradient descent algorithm will use to find the minimum of a cost function. Low numbers may prevent gradient descent from reaching the minimum. High numbers will make the algorithm work longer without improving its accuracy.

learning_rate - this is the size of the gradient descent step. Small learning step will make algorithm work longer and will probably require more iterations to reach the minimum of the cost function. Big learning steps may couse missing the minimum and growth of the cost function value with new iterations.

regularization_param - parameter that will fight overfitting. The higher the parameter, the simplier is the model will be.

polynomial_degree - the degree of additional polynomial features (x1^2 * x2, x1^2 * x2^2, ...). This will allow you to curve the predictions.

sinusoid_degree - the degree of sinusoid parameter multipliers of additional features (sin(x), sin(2*x), ...). This will allow you to curve the predictions by adding sinusoidal component to the prediction curve.

The plot below illustrates how the cost function value changes over each iteration. You should see it decreasing.

In case if cost function value increases it may mean that gradient descent missed the cost function minimum and with each step it goes further away from it. In this case you might want to reduce the learning rate parameter (the size of the gradient step).

From this plot you may also get an understanding of how many iterations you need to get an optimal value of the cost function. In current example you may see that there is no much sense to increase the number of gradient descent iterations over 500 since it will not reduce cost function significantly.

Let's now render the table of prediction values that our trained model does for unknown data (for test dataset). You should see that predicted happiness score should be quite similar to the known happiness score fron the test dataset.