🎓 A1Learner
🔍
0 of 47 lessons completed

Julia ML Toolkit

The ML toolkit fits a straight line to fixed data points. The fitted line then predicts a value for a new point.

A perfect line gives a slope of 2 and an intercept of 0

The fitter takes fixed x values and fixed y values. The points lie exactly on a line.

function fit_linear(x, y)
n = length(x)
mx = sum(x) / n
my = sum(y) / n
slope = sum((x .- mx) .* (y .- my)) / sum((x .- mx) .^ 2)
intercept = my - slope * mx
return slope, intercept
end
slope, intercept = fit_linear([1.0, 2.0, 3.0], [2.0, 4.0, 6.0])
println(round(slope, digits=6))
println(round(intercept, digits=6))

A fitted line predicts a value for a new point

Prediction multiplies the slope by the new x value and adds the intercept.

function predict(slope, intercept, x)
return slope * x + intercept
end
println(round(predict(2.0, 0.0, 10.0), digits=6))

Noisy points still give one slope and one intercept

Real points rarely lie exactly on a line. The fitter still returns a single slope and a single intercept.

function fit_linear(x, y)
n = length(x)
mx = sum(x) / n
my = sum(y) / n
slope = sum((x .- mx) .* (y .- my)) / sum((x .- mx) .^ 2)
intercept = my - slope * mx
return slope, intercept
end
slope, intercept = fit_linear([1.0, 2.0, 3.0, 4.0], [2.0, 3.0, 5.0, 4.0])
println(round(slope, digits=6))
println(round(intercept, digits=6))

Example

function fit_linear(x, y)
n = length(x)
mx = sum(x) / n
my = sum(y) / n
slope = sum((x .- mx) .* (y .- my)) / sum((x .- mx) .^ 2)
intercept = my - slope * mx
return slope, intercept
end
function predict(slope, intercept, x)
return slope * x + intercept
end
slope, intercept = fit_linear([1.0, 2.0, 3.0, 4.0], [2.0, 3.0, 5.0, 4.0])
println(round(slope, digits=6))
println(round(intercept, digits=6))
println(round(predict(slope, intercept, 5.0), digits=6))

Key points

  • Line fit: a line fit finds the slope and intercept that best match the points.
  • Slope: the slope is the rise in y for each step in x.
  • Intercept: the intercept is the value of the line when x is zero.
  • Least squares: this method picks the line with the smallest total squared error.
  • Prediction: a prediction applies the slope and intercept to a new x value.

In this example, fit_linear finds the slope and intercept for four noisy points. Predict then applies that line to a new x of 5. The three printed numbers are the slope, the intercept, and the prediction in order.

Note

solver outputs are rounded to six digits so the printed values stay short and exact.

Try it Yourself

Edit the code, then press Run.

Running is simulated · JuliaHub

Exercise

The noisy data give a slope of 0.8 and an intercept of 1.5. What is the prediction for a new x of 5?