Search This Blog

8 Linear Regression

1) Linear Regression with Gradient Descent with Random data

# -*- coding: utf-8 -*-

# Making the imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#plt.rcParams['figure.figsize'] = (6, 4)
pd.options.display.max_rows=10

X=np.arange(1,9,2)
Y=np.arange(1,5)

# Building the model
m = 0
c = 0
L = 0.01  # The learning Rate
epochs = 1000  # The number of iterations to perform gradient descent

n = float(len(X)) # Number of elements in X

# Performing Gradient Descent
for i in range(epochs):
    Y_pred = m*X + c  # The current predicted value of Y
    D_m = (-2/n) * sum(X * (Y - Y_pred))  # Derivative wrt m
    D_c = (-2/n) * sum(Y - Y_pred)  # Derivative wrt c
    m = m - L * D_m  # Update m
    c = c - L * D_c  # Update c
print (m, c)

X_new=float(input('enter value of  X_new :'))
Y_pred_new =(m*X_new + c)
print( 'Y_pred_new :', Y_pred_new)

#Making predictions
Y_pred = m*X + c
plt.scatter(X, Y)
plt.plot([min(X), max(X)], [min(Y_pred), max(Y_pred)], color='red')  # regression line
plt.show()


2) Linear Regression with Gradient Descent 


# Linear Regression with Gradient Descent
# -*- coding: utf-8 -*-

# Making the imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (6, 4)

# Preprocessing Input data
data = pd.read_csv("https://raw.githubusercontent.com/hknarra/datasets/master/Salary_Data.csv", sep=',')
X = data.iloc[:, 0]
Y = data.iloc[:, 1]
plt.scatter(X, Y)
plt.show()

data.head()

# Building the model
m = 0
c = 0
L = 0.01  # The learning Rate
epochs = 1000  # The number of iterations to perform gradient descent

n = float(len(X)) # Number of elements in X

# Performing Gradient Descent
for i in range(epochs):
    Y_pred = m*X + c  # The current predicted value of Y
    D_m = (-2/n) * sum(X * (Y - Y_pred))  # Derivative wrt m
    D_c = (-2/n) * sum(Y - Y_pred)  # Derivative wrt c
    m = m - L * D_m  # Update m
    c = c - L * D_c  # Update c
print (m, c)


X_new=float(input('enter value of  X_new :'))
Y_pred_new =(m*X_new + c)
Y_pred_new

#Making predictions
Y_pred = m*X + c
plt.scatter(X, Y)
plt.plot([min(X), max(X)], [min(Y_pred), max(Y_pred)], color='red')  # regression line
plt.show()



No comments:

Post a Comment