-
Notifications
You must be signed in to change notification settings - Fork 14
Simple Linear Regression Tutorial
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.
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).
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.
The cost function/RSS(Residual Sum of Squares) can be obtained by calling the RSS method like so:
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
Want to make a prediction with one data point? Simply call the predict method like so:
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.