Coder Social home page Coder Social logo

yogaraj2 / find-the-best-fit-line-using-least-squares-method Goto Github PK

View Code? Open in Web Editor NEW

This project forked from akilamohan/find-the-best-fit-line-using-least-squares-method

0.0 0.0 0.0 18 KB

License: BSD 3-Clause "New" or "Revised" License

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.
  4. Compute the y -intercept of the line by using the formula:
  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: YOGARAJ S
RegisterNumber:  212223040248
*/

import numpy as np

import matplotlib.pyplot as plt

X = np.array(eval(input()))

Y = np.array(eval(input()))

X_mean =np.mean(X)

Y_mean=np.mean(Y)

num=0

denom=0

for i in range(len(X)):

num+=(X[i] -X_mean)*(Y[i]-Y_mean)

denom+= (X[i]-X_mean)**2

m=num/denom

b=Y_mean-m*X_mean

print(m,b)

y_predicted=m*X+b print(y_predicted)

plt.scatter(X,Y)

plt.plot(X,y_predicted,color='green')

plt.show()

Output:

Screenshot 2024-02-23 110635

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

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