Understanding Machine Learning - Part 2 - The Code
This understanding machine learning was created so that one need not know coding so well, it’s just to understand machine learning and not the code. So even if this section makes no sense to you, don’t worry. We coded understanding machine learning with Julia. We used to packages named Plots and Distributions.
Plots was used to create the plots you see in the blog series, and Distributions was used to create the blue dots in the charts below.
using Plots
using Distributions
In the code below, we have X
that ranges from 0 to 5 with a step of 0.1, and Y
is the function 5x
with some noise.
X = 0:0.1:5
y(x) = 5x
random_numbers = rand(Normal(-1, 1), length(X))
Y = y.(X) .+ random_numbers;
We pass X
(set of points) to y
, thus it becomes y.(X)
, and we add some random numbers to it, to make the points bit scattered as shown below:
Now let plot a line, for that we have a slope of 2 for the line, denoted by the variable m
as shown below:
m = 2
In the code below we plot the line, and Julia Plots assign this line a red color:
plot_first_attempt = scatter(X, Y)
plot!(plot_first_attempt, X, m .* X, color="red")
We need a value to change the slope, lets call it delta
and assign it a value of 1. In the code below we are decreasing the slope by delta
calling the variable m_down
and increasing the slope by delta
calling the variable m_up
.
delta = 1
m_up = m + delta
m_down = m - delta
In the code below we plot the line with slope of m_up
in purple:
plot!(plot_first_attempt, X, m_up .* X, color="purple")
In the code below we plot the line with slope of m_down
in green:
plot!(plot_first_attempt, X, m_down .* X, color="green")
This is how we created the plots for the blog Understanding Machine Learning - Part 1.
One can download the code for this blog series here Codeberg.