Skip to content

Simple Linear Regression Tutorial

Guled edited this page Aug 18, 2016 · 8 revisions

The SimpleLinearRegression class allows you to utilize only 1 feature and 1 target/observation. Below you'll see some simple boilerplate code to get started with this class.

⚠️ All of your data (features, observations, etc...) must be floating point values. You can utilize the MLDataManager class's convertMyDataToFloat method.

Our Dataset for this tutorial

X Y
0 1
1 3
2 7
3 13
4 21
  • X is our feature set
  • Y is our target/observation

###Simple Linear Regression Example: Suppose you have the following feature dataset and observation below:

let dataset: Array<Float> = [0, 1, 2, 3, 4]
let target: Array<Float> = [1, 3, 7, 13, 21]

Now that we have this established let us setup our model. To do this you simply instantiate the SimpleLinearRegression class and call the train function (which will return your regression coefficients (weights)).

let model = SimpleLinearRegression()
let weights = model.train(dataset, output: target) // (This is a tuple) You will get a slope of 5.0, and an intercept of -1.0. 

It's as simple as that. Note that the example above is utilizing a closed-form solution and not optimizing the solution via gradient descent. Below is an example using gradient descent (note we will use the same dataset to stay consistent).

Simple Linear Regression using Gradient Descent algorithm

Now instead of passing in two parameters you will now need to specify both a step size and a tolerance value. Tolerance being our convergence criteria and step size being the amount of steps we take on the function of our model until we reach the optimum solution.

First we instantiate the SimpleLinearRegression class and then, like before, we pass in our feature (dataset variable) and our target/observation (target variable) along with our desired step size and tolerance values.

let model = SimpleLinearRegression()
let weights = try! model.train(dataset, output: target, step_size: 0.05, tolerance: 0.01) // (This is a tuple) You will get a slope of 4.99797, and an intercept of -0.994207. 

How to obtain your cost function value (RSS)

The cost function/RSS(Residual Sum of Squares) can be obtained by calling the RSS method like so:

⚠️ Note that before you can obtain your cost function result you must have already fit/trained your model.

let model = SimpleLinearRegression()
let weights = try! model.train(dataset, output: target, step_size: 0.05, tolerance: 0.01) // (This is a tuple)  You will get a slope of 4.99797, and an intercept of -0.994207. 

// Now that we have trained our model we can obtain the cost function result/RSS value
let rss = model.RSS(dataset, output: output, slope: weights.0, intercept: weights.1) // Turns out to be 14.0001

How to make a one-time prediction

Want to make a prediction with one data point? Simply call the predict method like so:

⚠️ Note before you attempt to make a prediction you must have fit/trained your model.

let model = SimpleLinearRegression()
let weights = try! model.train(dataset, output: output, step_size: 0.05, tolerance: 0.01) // You will get a slope of 4.99797, and an intercept of -0.994207. 

let quickPrediction = model.predict(weights.0, intercept: weights.1, inputValue: 2) // Prediction turns out to be 9.00173

Please make a github issue if you are having any problems setting this up, or if you would like to make any recommendations or corrections. Thank you.