Code Monkey home page Code Monkey logo

univariate-linear-regression's Introduction

Implementation of Univariate Linear Regression

Aim:

To implement univariate Linear Regression to fit a straight line using least squares.

Equipment’s required:

  1. Hardware – PCs
  2. Anaconda – Python 3.7 Installation / Moodle-Code Runner

Algorithm:

  1. Get the independent variable X and dependent variable Y.
  2. Calculate the mean of the X -values and the mean of the Y -values.
  3. Find the slope m of the line of best fit using the formula. eqn1
  4. Compute the y -intercept of the line by using the formula: eqn2
  5. Use the slope m and the y -intercept to form the equation of the line.
  6. Obtain the straight line equation Y=mX+b and plot the scatterplot.

Program:

#Program for Univariate linear regression using the least squares method.
#Developed by: B.Pavizhi
#RegisterNumber: 21500608
import numpy as np
import matplotlib.pyplot as plt
X= np.array ([0,1,2,3,4,5,6,7,8,9])
Y= np.array([1,3,2,5,7,8,8,9,10,12])
plt.scatter(X, Y)
plt.show()
num=0
den=0
X_mean = np.mean(X)
Y_mean = np.mean(Y)
for i in range(len(X)):
    num += (X[i]-X_mean)*(Y[i]-Y_mean)
    den += (X[i]-X_mean)**2
m = num/den
c = Y_mean - m*X_mean
print (m, c)
Y_pred = m*X + c
print(Y_pred)
plt.scatter(X,Y, color = "Red")
plt.plot(X,Y_pred, color = "Green")
plt.show()

Output:

Input:

inp

Output 1:

output

output 2:

output1

Result:

Thus the univariate Linear Regression was implemented to fit a straight line using least squares.

univariate-linear-regression's People

Contributors

etjabajasphin avatar pavizhi 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.