Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
ML | Stochastic Gradient Descent (SGD)
Next article icon

Stochastic Gradient Descent Regressor using Scikit-learn

Last Updated : 18 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Stochastic Gradient Descent (SGD) is a popular optimization technique in the field of machine learning. It is particularly well-suited for handling large datasets and online learning scenarios where data arrives sequentially. In this article, we will discuss how a stochastic gradient descent regressor is implemented using Scikit-Learn.

What is a stochastic gradient descent regressor?

The Stochastic Gradient Descent Regressor (SGD Regressor) is a linear model used for regression tasks that employ the Stochastic Gradient Descent optimization algorithm. Unlike traditional gradient descent, which computes the gradient of the cost function using the entire dataset, stochastic gradient descent updates the model parameters iteratively using each training example.

Steps to use Stochastic Gradient Descent Regressor using Scikit-learn

  1. Load the Diabetes Dataset
  2. Split the Dataset into Training and Testing Sets
  3. Create a Linear Regression Model
  4. Train the Model
  5. Make Predictions
  6. Evaluate the Model

Implementation of Stochastic Gradient Descent Regressor using Scikit-learn

We will use the diabetes dataset to build and evaluate a linear regression model using SGD.

Step 1 : Importing the required libraries

Python
from sklearn.datasets import load_diabetes import numpy as np from sklearn.linear_model import LinearRegression from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split 

Step 2 :Splitting the dataset

We will now split our dataset in test and training parts, in the ratio 1:4.

Python
X,y = load_diabetes(return_X_y=True) print(X.shape) print(y.shape) X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=2) 

Output :

(442, 10)
(442,)

Step 3 : Fitting the linear Regression model on Training data

After training the linear regression model using the fit method, you can access the coefficients and intercept of the model. The coefficients represent the weight of each feature in the linear equation, and the intercept is the constant term.

Python
reg = LinearRegression() reg.fit(X_train,y_train)  print(reg.coef_) print(reg.intercept_) 

Output :

LinearRegression()
[ -9.16088483 -205.46225988 516.68462383 340.62734108 -895.54360867
561.21453306 153.88478595 126.73431596 861.12139955 52.41982836]
151.88334520854633

Each number in the Coefficients array corresponds to a feature from the diabetes dataset, and the Intercept is the constant term in the linear model.

Step 4 : Evaluating the Model

The code is used to predict values on the test set (X_test) using a trained regression model (reg), and then calculate the R-squared score between the predicted values (y_pred) and the actual values (y_test).


Python
y_pred = reg.predict(X_test) r2_score(y_test,y_pred) 

Output:

0.4399387660024645


Step 5: Implementing SGD

We wil implement the Stochastic Gradient Descent Regressor using Scikit-learn library.

Python
from sklearn.linear_model import SGDRegressor reg = SGDRegressor(max_iter=100,learning_rate='constant',eta0=0.01) reg.fit(X_train,y_train)  SGDRegressor(learning_rate='constant', max_iter=100) y_pred = reg.predict(X_test) r2_score(y_test,y_pred) 

Output :

SGDRegressor(learning_rate='constant', max_iter=100)
0.49059547063734904


The scikit-learn's implementation of SGDRegressor benefits from optimized internal algorithms, better learning rate schedules, and regularization techniques, leading to improved performance over the custom implementation.


Next Article
ML | Stochastic Gradient Descent (SGD)

A

aryanmishra21
Improve
Article Tags :
  • Machine Learning
  • Blogathon
  • AI-ML-DS
  • Data Science Blogathon 2024
  • Sklearn
Practice Tags :
  • Machine Learning

Similar Reads

  • Stochastic Gradient Descent Regressor
    A key method in data science and machine learning is the stochastic gradient descent (SGD) regression. It is essential to many regression activities and aids in the creation of predictive models for a variety of uses. We will study the idea of the SGD Regressor, its operation, and its importance in
    10 min read
  • Stochastic Gradient Descent In R
    Gradient Descent is an iterative optimization process that searches for an objective function’s optimum value (Minimum/Maximum). It is one of the most used methods for changing a model’s parameters to reduce a cost function in machine learning projects. In this article, we will learn the concept of
    10 min read
  • ML | Stochastic Gradient Descent (SGD)
    Stochastic Gradient Descent (SGD) is an optimization algorithm in machine learning, particularly when dealing with large datasets. It is a variant of the traditional gradient descent algorithm but offers several advantages in terms of efficiency and scalability, making it the go-to method for many d
    8 min read
  • Gradient Descent in Linear Regression
    Gradient descent is a optimization algorithm used in linear regression to minimize the error in predictions. This article explores how gradient descent works in linear regression. Why Gradient Descent in Linear Regression?Linear regression involves finding the best-fit line for a dataset by minimizi
    4 min read
  • Compare Stochastic Learning Strategies for MLPClassifier in Scikit Learn
    A stochastic learning strategy is a method for training a Machine Learning model using stochastic optimization algorithms. These algorithms update the model's weights and biases using a randomly selected subset of the training data, rather than using the entire dataset. This can improve convergence
    5 min read
  • Linear Regression Implementation From Scratch using Python
    Linear Regression is a supervised learning algorithm which is both a statistical and a machine learning algorithm. It is used to predict the real-valued output y based on the given input value x. It depicts the relationship between the dependent variable y and the independent variables xi ( or featu
    4 min read
  • Vectorization Of Gradient Descent
    In Machine Learning, Regression problems can be solved in the following ways: 1. Using Optimization Algorithms - Gradient Descent Batch Gradient Descent.Stochastic Gradient Descent.Mini-Batch Gradient DescentOther Advanced Optimization Algorithms like ( Conjugate Descent ... ) 2. Using the Normal Eq
    5 min read
  • How to Implement Adam Gradient Descent from Scratch using Python?
    Grade descent is an extensively used optimization algorithm in machine literacy and deep literacy. It's used to minimize the cost or loss function of a model by iteratively confirming the model's parameters grounded on the slants of the cost function with respect to those parameters. One variant of
    14 min read
  • Support Vector Regression (SVR) using Linear and Non-Linear Kernels in Scikit Learn
    Support vector regression (SVR) is a type of support vector machine (SVM) that is used for regression tasks. It tries to find a function that best predicts the continuous output value for a given input value. SVR can use both linear and non-linear kernels. A linear kernel is a simple dot product bet
    5 min read
  • How to Normalize Data Using scikit-learn in Python
    Data normalization is a crucial preprocessing step in machine learning. It ensures that features contribute equally to the model by scaling them to a common range. This process helps in improving the convergence of gradient-based optimization algorithms and makes the model training process more effi
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences