Code Monkey home page Code Monkey logo

find-the-best-fit-line-using-least-squares-method's Introduction

Implementation of Univariate Linear Regression

AIM:

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

Equipments Required:

  1. Hardware โ€“ PCs
  2. Anaconda โ€“ Python 3.7 Installation / Jupyter notebook

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.

image

4. Compute the y -intercept of the line by using the formula:

image

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 to implement univariate Linear Regression to fit a straight line using least squares.
Developed by:LOGESHWARI.P
RegisterNumber:212221230055
import matplotlib.pyplot as plt
x=[5,6,3,2,6,7,1,2]
y=[2,3,6,5,8,3,5,8]
plt.scatter(x,y)
plt.show()

import numpy as np
import matplotlib.pyplot as plt

#assign input
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])

#mean values of input
x_mean=np.mean(x)
print(x_mean)
y_mean=np.mean(y)
print(y_mean)

num=0
denum=0

for i in range(len(x)):
  num+=(x[i]-x_mean)*(y[i]-y_mean)
  denum+=(x[i]-x_mean)**2

#find m
m=num/denum

#find b
b=y_mean-m*x_mean
print("m",m)
print("b",b)

#find y_pred
y_pred=m*x+b
print(y_pred)

#plot graph
plt.scatter(x,y)
plt.plot(x,y_pred,color='green')
plt.show() 

Output:

198871218-f48e2dc5-1598-40c6-8e4a-1fdbd63d7817

Result:

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

find-the-best-fit-line-using-least-squares-method's People

Contributors

akilamohan avatar logeshwari2004 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.