DEV Community

Neha Gupta
Neha Gupta

Posted on

Day 7 of Machine Learning|| Linear Regression Part 1

Hey reader👋Hope you are doing well😊
In the last post we have read about Supervised Machine Learning and the types of problems that we can encounter.
In this post we are going to discuss about our very first algorithm that is Linear Regression ,we are going the see the math's behind it and in later part we are going to see its implementation.
So let's get started🔥

Linear Regression

This algorithm is used for dataset having continuous output such as price, age, height, weight etc. To understand linear regression let's consider an example-:
Image description
So here we have a dataset that takes size of apartment and number of bedrooms as independent variables and based on these price of each apartment is decided (price is dependent variable). Now suppose we have an apartment of 1000 sq. feet and 2 bedrooms. Can we predict the price of this apartment based on above data?
Yes we can predict the price. For accurate prediction we need to find the relation between independent variables or hypothesis function. And we need to train our data on this function so that it can predict accurate results.
Image description
The hypothesis function say h(x), where x is input parameter is defined as (for this problem)-:
Image description
x1 = size of apartment
x2 = number of bedrooms
Θ1 and Θ2 = weights
Θ0 = bias, collectively all Θ's are parameter of learning algorithm.
h(x) = predicted price / hypothesis function
The weights are assigned to input features such that the feature having more contribution in predicting output gets larger weight than the feature contributing less.
Now you can see that the above equation is similar to the equation of line i.e. y=mx+c , so we can conclude that the data is fit against a straight line.
By adjusting the values of Θ0, Θ1 and Θ2, the hypothesis function can accurately predict the price of an apartment based on its size and number of bedrooms. Linear regression assumes that the relationship between the independent variables and the dependent variable is linear, which means that the data can be fit against a straight line.
Image description

Linear regression is an algorithm that provides a linear relationship between an independent variable and a dependent variable to predict the outcome of future events.

General Equation for linear regression is -:
Image description
here x0=1 (dummy variable)
n = number of features
hΘ(x) emphasizes that hypothesis depends on both input features and parameters.

Notations -:
m = number of training samples i.e. number of rows in dataset
x = inputs (features)
y = output (target variable)
xi, yi = training example
n = number of features i.e. number of columns in dataset

Choose Θ such that hΘ(x) ~ y for training example.
So our goal is to minimize the squared distance between predicted value and actual output.
Image description
For best fit line -:
Image description
Here d is squared distance between actual and predicted value.
Image description

Cost Function -:
We are using mean squared error (MSE) to determine our cost function. So our cost function is given as -:
Image description
We need to minimize the cost function to get best fit line.
1/2m : This term scales the sum of squared errors to compute the average, where m is the number of training examples. 1/2 s often included to simplify the derivative calculation.

So this was it for this blog. In the next blog we are going to see how we can minimize the cost function. I hope you have understood this blog. If you have any doubts please leave it in comment section I'll definitely try to solve your problem.
Don't forget to like the post and follow me.
Thankyou💙

Top comments (0)