Machine learning introduction with linear regression and Gradient decent PART 1

Somto Achu
4 min readJul 24, 2017

--

Much of the things we do today is fully automated and controlled by computers ,buts thats too general, take example something like facebook, if the underlying code we just had a bunch of if and else statement, well we probably would get unreasonable friend suggestions more often than we do ,or if we were to use just if and else statements for a self driving cars, we probably would get horrible driving ,so basically we use machine learning to train our system to be able to make choices by itself based on a set of inputs from previous data …..simple enough.

When i started learning ,obviously like any wanna be future Tonny stark i went for the hard stuff and i got slammed so had with math and concepts i didn’t even think a normal human should handle so i had to start from the easy stuff (linear regression).

So now linear regression what is it ?

Ever seen this formular y=x?

When i saw this for the first time, i remembered high school math ,its the formula for linearity or lets just say equation of a straight line ,but lets be more exact here and add some more variables lets say

y = mx + c

we now have more variables that our value y depends on which is good because y does not always equal x in all cases of a straight line except if the m= 1 && c = 0 then its the same thing y = x ,looking at the parameters m and c ,m is our gradient and c is the starting point of the line.

Now that we have that down lets look into what we consider our data set .

Its what the system learns with or learns on.

So lets look at this a set of data containing information on price of houses and its size and lets say we put all this data into x and y where x is equal to the price and y is equal to the size as you can see we can plot all this on a graph where our price is on the y axis and size in the x axis .

Now we are meant to build a system that can predict prices of building based on the size of the building simple enough right ,we are gonna use the equation of a straight line as our function remember ,

y= mx + c

with m as gradient and c as the starting point of the graph on the y axis simple enough lets just use the random data i have

x=[1,2,3,4,5,6,7]

y=[1,2,3,4,5,6,7]

simple enough right ?, so we plot this on the graph and you notice we get a straight line formed by dots on the graph like this.

So next step would be to draw the line of best fit which just goes through all d points and if you notice it starts at 0 and the gradient is a perfect 1(by 1 i mean it doesn’t bend and is a perfect 45 degrees)

so we have our parameters as the gradient being 0 and starting point being 1.

So lets try and calculate a point in our data thats known already using our formula above.

Lets assume for our second item among the 7 sets of data in x and y ,we try to find y so x=2 and m = 1 and c = 0

2*1 + 0 = 2

and thats our y for the second set of data so now lets try and calculate a value thats unknown lets assume we want to find the price of a house of size 20 using our formula.

20*1 + 0 = 20

Simple actually and if you trace your line to the point where it is 20 the x axis you will notice that if you trace a line to the y axis you read 20.

Putting this in code is as simple as.

function straightline(m,c,x){

return m*x + c;

}

Wow thats a wrap for now the next part of this would be me explaining how we get the m and c values without having to draw a graph we would be using the gradient decent and cost function to get this, obviously you could use other methods such as neural networks or a genetic algorithm to try and accomplish this 😂😂😂😂😂😂but lets be frank this is a beginner tutorial if i did that you probably wont continue this series so till next time.

--

--

Somto Achu

Developer living in nigeria into robotics and AI trying to make an impact with the little i know