Code Monkey home page Code Monkey logo

implementation-of-linear-regression-using-gradient-descent's Introduction

Implementation-of-Linear-Regression-Using-Gradient-Descent

AIM:

To write a program to implement the linear regression using gradient descent.

Equipments Required:

  1. Hardware โ€“ PCs
  2. Anaconda โ€“ Python 3.7 Installation / Moodle-Code Runner

Algorithm

  1. Import the standard libraries in python required for finding Gradient Design.
  2. Read the dataset file and check any null value using .isnull() method.
  3. Declare the default variables with respective values for linear regression.
  4. Calculate the loss using Mean Square Error.
  5. Predict the value of y.
  6. Plot the graph respect to hours and scores using .scatterplot() method for Linear Regression.
  7. Plot the graph respect to loss and iterations using .plot() method for Gradient Descent.

Program:

/*
Program to implement the linear regression using gradient descent.
Developed by: AAKASH S
RegisterNumber:  212221240001
*/
import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_csv("/content/student_scores.csv")
data.head()
data.tail()
data.isnull().sum()
#storing data to variable
X=data.Hours
Y=data.Scores
print(X)
print(Y)
#defining variables
n=len(X)
m=0
c=0
L=0.01
loss=[]
for i in range(10000):
    ypred = m*X + c
    MSE = (1/n) * sum((ypred - Y)*2)
    dm = (2/n) * sum(X*(ypred-Y))
    dc = (2/n) * sum(ypred-Y)
    c = c-L*dc
    m = m-L*dm
    loss.append(MSE)
print(m,c)
#Plotting Linear Regression Graph
ypred=m*X+c
plt.scatter(X,Y,color="red")
plt.plot(X,ypred,color="green")
plt.xlabel("Study hours")
plt.ylabel("Scores")
plt.title("Study hours vs Scores")
#Graph between iteration Vs loss
plt.show()plt.title("study iteration vs loss")
plt.plot(loss) 
plt.xlabel("iterations") 
plt.ylabel("loss") 
plt.show()

Output:

Training data

Output 1

Testing data

Output 2

Result:

Thus the program to implement the linear regression using gradient descent is written and verified using python programming.

implementation-of-linear-regression-using-gradient-descent's People

Contributors

akilamohan avatar aakash-suresh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.