Giter VIP home page Giter VIP logo

gradient-descent-python's Introduction

Gradient Descent Implementation in Python

We have seen in the lectures how Gradient Descent helps us minimize the cost function. In this exercise we will stepwise write to functions to implement our own batch gradient descent algorithm for univariate linear regression.

You may want to revise and refer to the DG algorithm from the slides.

You cannot use any sklearn objects for the purpose of this exercise.

Task: Write your own Gradient Descent Algorithm

1. Write error_calculator() funtion:

  • Wrtie a function that given b, theta, x and y, calculates squared error, where

    y = theta * x + b

  • Accepts:

    • b (int/float) intercept
    • theta (int/float) coefficient
    • points (list m x 2 format. First column contains x values and second column contains y values)
  • Returns

    • MSE for given b, theta and points

2. Write gradient() funtion:

  • Wrtie a function that given current b, current theta, x, y and learing rate, calculates the gradient and returns new b and theta

  • Accepts:

    • current_b (int/float) current intercept
    • current_theta (int/float) current coefficient
    • points (list m x 2 format. First column contains x values and second column contains y values)
    • learning_rate (float)
  • Returns

    • new b
    • new theta

3. Write gradient_descent() funtion:

  • Wrtie a function that given current b, current theta, x, y, learing rate, num_iterations performs gradient descent and returns lists of calculated b, theta and errors

  • Accepts:

    • starting_b (int/float) initial intercept
    • starting_theta (int/float) initial coefficient
    • points (list m x 2 format. First column contains x values and second column contains y values)
    • learning_rate (float)
    • num_iterations (int) number of iterations to perform gradient descent
  • Returns

    • list of b
    • list of theta
    • list of erros for each stage

Note: you can use previously defined functions in this function

4. Write plot_theta() funtion:

  • Wrtie a function that given current b, current theta, x, y, learing rate and num_iterations plots values of theta against iteration number

  • Accepts:

    • starting_b (int/float) initial intercept
    • starting_theta (int/float) initial coefficient
    • points (list m x 2 format. First column contains x values and second column contains y values)
    • learning_rate (float)
    • num_iterations (int) number of iterations to perform gradient descent
  • Returns

    • plots a graph with
      • title "Theta"
      • x label "iterations"
      • y label "theta"

Note: you can use previously defined functions in this function

5. Write plot_b() funtion:

  • Wrtie a function that given current b, current theta, x, y, learing rate and num_iterations plots values of b against iteration number

  • Accepts:

    • starting_b (int/float) initial intercept
    • starting_theta (int/float) initial coefficient
    • points (list m x 2 format. First column contains x values and second column contains y values)
    • learning_rate (float)
    • num_iterations (int) number of iterations to perform gradient descent
  • Returns

    • plots a graph with
      • title "b"
      • x label "iterations"
      • y label "constant term"

Note: you can use previously defined functions in this function

6. Write plot_errors() funtion:

  • Wrtie a function that given current b, current theta, x, y, learing rate and num_iterations plots values of errors against iteration number

  • Accepts:

    • starting_b (int/float) initial intercept
    • starting_theta (int/float) initial coefficient
    • points (list m x 2 format. First column contains x values and second column contains y values)
    • learning_rate (float)
    • num_iterations (int) number of iterations to perform gradient descent
  • Returns

    • plots a graph with
      • title "Error"
      • x label "iterations"
      • y label "Error"

Note: you can use previously defined functions in this function

gradient-descent-python's People

Contributors

mudassirkhan19 avatar

Watchers

Soumendra Prasad Dhanee avatar James Cloos avatar Suraj Jadhav avatar PradeepJaiswar avatar Mayuresh Suresh Shilotri avatar  avatar Sangam Angre avatar Shweta Doshi 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.