Day 2 of 100DaysofML

Charan Soneji
100DaysofMLcode
Published in
4 min readJun 17, 2020

Let’s start simple and dive into Linear Regression. There's a lot to ML and getting to all the ‘cool’ stuff like object detection can be overwhelming if you do not get your fundamentals clear. So, I’m going to start with the explanation of a very basic algorithm called “Linear Regression”. It’s one of those algorithms that you would find any tutorial to start with but the route of this blog is going to be slightly different, so just make sure to stay tuned.

So, what exactly is linear regression?
I don’t think that’s how we should be starting. Let’s split the word up into “Linear” and “Regression”. Okay, so now what exactly does Regression stand for.
Regression basically is a type of model that helps us predicting a certain value based on previous values that have been provided to us. This also means that Regression is a type of Supervised Learning where we are given a set of input variables which are mapped to their respective output values and based on these values, we try to predict the output of a random input variable.
Every input variable can be mapped to an output variable using a function.
Here, is where I can explain the meaning of the word Linear. Based on the type of function, that we are using, we may classify the regression as Linear, Polynomial and so on. This solely depends on the function and its degree.

But, which function do we use? We can simplify this with an example:
We all know that the expression of a simple line is: y=mx+c
For the values:
x=[1,2,3,4,5]
y=[1,2,3,4,5]

Mapping of x and y for x=y

In the above plot, a simple representation of y=x is shown, now if the same line is extended, it may be possible to predict the output of y, if the input variable is given to be 6. Obviously, it will be 6 (because y=x!).
In a similar manner, based on the function that is being used, different types of graphs may be plotted having different curves (based on the type of function used). This is exactly what regression is. Based on the different types of functions used, we have a number of regression types, the simplest one being linear regression which we have seen above. Since the function mapping the input and output variable shows a degree of 1, it is considered to be linear and by substituting the value of our input x in the expression, we can predict the output y. Let's get to the code for Linear Regression:

#Importing libraries
import numpy as np
from sklearn.linear_model import LinearRegression
#Declaring your arrays and variables
x = np.array([9, 13, 25, 33, 41, 50]).reshape((-1, 1)) #We are reshaping to get a 2 dimensional array which is needed by the linear_model
y = np.array([9, 23, 14, 32, 29, 40])
#Intitializing your model using sklearn and fitting your variable
model=LinearRegression()
model.fit(x,y)
#Evalurating your r score
r=model.score(x,y)
print(r)
#Predicting the output y values for the given values x
y_pred=model.predict(x)
print(y_pred)

NOTE: Sklearn or scikit-learn is the ML based library in python and all the basic models are present within it. They just need to be imported and Voila. The same functions can be hard coded as well but that's kind of pointless.

Now, what we saw above represents Linear Regression but only with a single variable. Linear regression may be done with multiple variables as well. This means that our mapping function basically contains more than one “explanatory” variables. Wait what. What does explanatory mean?
This is another important concept which is more mathematical related than ML related, but it is the concept of dependent and independent variables. Dependent variables, as the word suggests is the variable upon which the output function depends unlike independent variables upon which does not depend. I’d suggest reading up a bit about these variables before getting into Linear Regression but if you’re comfortable with just the understanding and their usage, you’re good to go.

The last thing I want to discuss about are the types of regression based on the function that use. Some of the basic ones that we should know about are:
1. Linear Regression: y=mx+c
2. Polynomial Regression: y=B0+B1(x)+B2(x²)+B3(x³)+…+Bn(x^n)
3.Logistic Regression:

Formula for logistic regression

Before it gets overwhelming, I wanna summarize whatever i have covered so far using a simple flow chart:

There are obviously more types of regression but these are the fundamentals ones

We are not even close to how much is available in ML but this is what I have covered so far. But that's it for today, I’m going to start diving deeper right into the coding portions of the fundamentals ML models from tomorrow. Along with some Pytorch implementations of the same along with discussion of weights and biases. Keep Learning.

Cheers.

--

--