Day 7 of Machine Learning|| Linear Regression Part 1
Tags: tutorial, datascience, machinelearning, beginners
Published: 2024-06-12 11:05:51

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkcbgwxn7amswhnlf65b.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/msn1uhd1a2e303dqzpql.png)
The hypothesis function say h(x), where x is input parameter is defined as (for this problem)-:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkveuiu1daoxn5wepbf6.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x155cyzyzwjk4fbbfwun.png)

> **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/touho8zdchntk299g1tc.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcgan67d9yisogkp5ouh.png)
For best fit line -:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwfpa2q1z321mybew2on.png)
Here `d` is squared distance between actual and predicted value.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l20lyyciagq4lykipnir.png)

**Cost Function -:**
We are using mean squared error (MSE) to determine our cost function. So our cost function is given as -:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2cq4w6fstsdx1lhjj6b.png)
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💙