Breaking News
Home / Machine Learning / Simple Linear Regression

# Simple Linear Regression

## Understanding Linear Regression in Machine Learning using Python Language

Linear Regression is a simple machine learning model for regression problems, i.e., when the target variable is a real value.

Example: Let’s start with an example — suppose we have a dataset with information about the area of a house (in square feet) and its price and our task is to build a machine learning model which can predict the price given the area.

Linear Regression is a linear model, e.g. a model that assumes a linear relationship between the input variables (x) and the single output variable (y). More specifically, that y can be calculated from a linear combination of the input variables (x).

• When there is a single input variable (x), the method is referred to as `simple linear regression.`
• When there are multiple input variables, the method is known as `multiple linear regression.`

X is the Independent Variable

Y is the Dependent Variable

## Simple Linear Regression using Python without Scikit-Learn Library

Import Python Packages

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

Create dummy data and convert it into DataFrame

l=[[1,3],[2,4],[3,2],[4,4],[5,5]]
df=pd.DataFrame(l,columns=[‘x’,’y’])
x=df.x
y=df.y

Calculate the means of x and y

mean_x=np.mean(x)
mean_y=np.mean(y)

print(“Mean of X:”,mean_x)
print(“Mean of Y:”,mean_y)

Find the Slope and Intercept for the best fit line

numer=0
denom=0

for i in range(n):

numer += (x[i] – mean_x) * (y[i] – mean_y)

denom += (x[i] – mean_x) ** 2

print(numer)
print(denom)

W1=numer/denom
print(“Slope M:”,W1)

Wo= mean_y – (W1 * mean_x)
print(“Intercept C:”,Wo)

Predict all the values for x using new Weights W0 and W1

Num=0 Den=0 f=[] for i in range(n):

y_pred= Wo + W1 * x[i]

f.append(y_pred)

Num += (y_pred- mean_y) ** 2

Den += (y[i] – mean_y) ** 2

print(numer)
print(denom)

Plot the Line Plot and Scatter Plot for the Real and Predicted Value

plt.plot(x,f,color=”red”,label=”Regression Line”)
plt.scatter(x,y,label=”Actual Value”)
plt.legend()
plt.show()

Calculate the R2 Error

print(Num,Den)
R=Num/Den
print(“R:”,R)

## Simple Linear Regression using Python with Scikit-Learn Library

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

l=[[1,3],[2,4],[3,2],[4,4],[5,5]]
df=pd.DataFrame(l,columns=[‘x’,’y’])
x=df.x.values.reshape(-1,1)
y=df.y.values

from sklearn.linear_model import LinearRegression
reg=LinearRegression()
reg=reg.fit(x,y)
Y_Pred=reg.predict(x)
print(“Y Predict:”,Y_Pred)

print(‘Coefficients:’, reg.coef_)
print(‘Intercept:’, reg.intercept_)

R2=reg.score(x,y)
print(R2) 