Coder Social home page Coder Social logo

mkumar262006 / implementation-of-linear-regression-using-gradient-descent Goto Github PK

View Code? Open in Web Editor NEW

This project forked from akilamohan/implementation-of-linear-regression-using-gradient-descent

0.0 0.0 0.0 9 KB

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

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

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

AIM:

To write a program to predict the profit of a city using the linear regression model with gradient descent.

Equipments Required:

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

Algorithm

  1. Get population and profit data for cities.
  2. Begin with random guesses for how population influences profit.
  3. Gradually adjust guesses to minimize the difference between predicted and actual profits.
  4. Keep adjusting until predictions are close to actual profits.
  5. Once adjusted, predict profit for new city populations.
  6. Evaluate how well predictions match actual profits.
  7. If predictions are off, refine guesses and repeat the process.
  8. Once satisfied, use the model to predict profits based on population for decision-making.

Program:

```py
Program to implement the linear regression using gradient descent.
Developed by: MANOJ KUMAR S
RegisterNumber: 212223240082

import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
def linear_regression(X1,y,learning_rate=0.1,num_iters=1000):
 X = np.c_[np.ones(len(X1)),X1]

 theta = np.zeros(X.shape[1]).reshape(-1,1)
 for _ in range(num_iters):
  #Calculate predictions
  predictions = (X).dot(theta).reshape(-1,1)
  #Calculate errors
  errors=(predictions-y).reshape(-1,1)
  #update theta using gradient descent
  theta-=learning_rate*(1/len(X1))*X.T.dot(errors)
 return theta
data=pd.read_csv("50_Startups.csv")
data.head()

X = (data.iloc[1:,:-2].values)
X1 =X.astype(float)
scaler = StandardScaler()
y = (data.iloc[1:,-1].values).reshape(-1,1)
X1_Scaled = scaler.fit_transform(X1)
Y1_Scaled = scaler.fit_transform(y)
print(X)
print(X1_Scaled)

#learn model Parameters
theta=linear_regression(X1_Scaled,Y1_Scaled)
#predict target calue for a new data point
new_data=np.array([165349.2,136897.8,471784.1]).reshape(-1,1)
new_Scaled=scaler.fit_transform(new_data)
prediction=np.dot(np.append(1,new_Scaled),theta)
prediction= prediction.reshape(-1,1)
pre = scaler.inverse_transform(prediction)
print(prediction)
print(f"Predicted value:{pre}")

Output:

image image image

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 mkumar262006 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.