￼￼Introduction to Linear Regression:
Linear Regression means predicting scores of one variable from the scores of the second variable. The variable we are predicting is called the criterion variable and is referred to as Y. The variable we are basing our predictions is called the predictor variable and is referred to as X. When there is only one predictor variable, the prediction method is called simple regression.The aim of linear regression is to find the best-fitting straight line through the points. The best-fitting line is called a regression line.
The above equation is hypothesis equation
hθ(x) is nothing but the value Y(which we are going to predicate ) for particular x ( means Y is a linear function of x)
θ0 is a constant
θ1 is the regression coefficient
X is value of the independent variable
Properties of the Linear Regression Line
Linear Regression line has the following properties:
- The line minimizes the sum of squared differences between observed values (the y values) and predicted values (the hθ(x) values computed from the regression equation).
- The regression line passes through the mean of the X values (x) and through the mean of the Y values ( hθ(x) ).
- The regression constant (θ0) is equal to the y-intercept of the regression line.
- The regression coefficient (θ1) is the average change in the dependent variable (Y) for a 1-unit change in the independent variable (X). It is the slope of the regression line.
The least squares regression line is the only straight line that has all of these properties.
Goal of Hypothesis Function
The goal of Hypothesis is to choose θ0 and θ1 so that hθ(x) is close to Y for our training data while choosing θ0 and θ1 we have to consider the cost function( J(θ) ) where we are getting low value for cost function( J(θ) ).
The below function is called as a cost function, the cost function ( J(θ) ) is nothing but just a Squared error function.
Let’s Understand Linear Regression with Example
Before going to explain linear Regression let me summarize the things we learn
Suppose we have data some thing look’s like this
Now our task is to answer the below questions
Let me draw a graph for our data
Python Code for graph
# Required Packages
import plotly.plotly as pyfrom plotly.graph_objs import *
from datetime import datetime
x = [
data = Data([
x = x,
y = [1014004000, 1029991000, 1045845000, 1049700000, 1065071000,
1080264000, 1095352000, 1129866000, 1147996000, 1166079000,
plot_url = py.plot(data, filename='DataAspirant')
- Now what we will do is we will find the most suitable value for our θ0 and θ1 using hypotheses equation.
- Where x is nothing but the years , and the hθ(X) is the prediction value for our hypotheses .
- Once we were done finding θ0 and θ1 we can find any value.
- Keep in mind we first find the θ0 and θ1 for our training data.
- Later we will use these θ0 and θ1 values to do the prediction for test data.
Don’t think too much about how to find θ0 and θ1 values, in linear regression implementation in python , I have explained how we can find θ0 and θ1 values with nice example and the coding part too.
I hope you liked today’s post. If you have any questions then feel free to comment below. If you want me to write on one specific topic then do tell it to me in the comments below.