Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
Regression Analysis with SciPy
Next article icon

Regression Analysis with SciPy

Last Updated : 23 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Regression analysis is a statistical technique used to examine the relationship between one or more independent variables and a dependent variable. It helps in modelling and analyzing data where the focus is on the relationship between variables. In Python SciPy is a popular library for statistical analysis which offers simple and efficient tools for performing regression using its stats module.

Regression-Line-copy
Regression Analysis

Regression Analysis

Regression analysis is a statistical technique used to test the relationship between a dependent variable and one or more independent variables. It helps in understanding how the dependent variable changes when any one of the independent variables is varied while the others are held fixed. Regression analysis is commonly used for prediction, forecasting and determining causal relationships between variables.

1. Using SciPy for Linear Regression

Linear Regression models a straight line relationship between one independent and one dependent variable:

y = mx+c

  • This code performs Linear regression using SciPy’s linregress() on sample x and y data.
  • It calculates the slope, intercept and R squared value to describe the line of best fit.
  • Finally, it visualizes the original data points and the fitted regression line using Matplotlib.
Python
import numpy as np from scipy import stats import matplotlib.pyplot as plt  # Sample data x = np.array([1, 2, 3, 4, 5]) y = np.array([2, 4, 5, 4, 5])  # Perform linear regression slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)  print(f"Slope: {slope}") print(f"Intercept: {intercept}") print(f"R-squared: {r_value**2}")  plt.scatter(x, y, color='blue', label='Data') plt.plot(x, slope * x + intercept, color='red', label='Regression Line') plt.legend() plt.xlabel("X") plt.ylabel("Y") plt.title("Linear Regression with SciPy") plt.show() 

Output:

Screenshot-2025-06-23-095651
Output for Linear Regression

2. Using SciPy for Exponential Regression

Exponential Regression is used when data shows exponential growth or decay modelled as:

y = a \cdot e^{bx}

  • This code performs Exponential regression using curve_fit from SciPy.
  • It fits the data to the model and finds the best values for a and b. The result is plotted by showing both the original data points and the fitted exponential curve.
Python
import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt  #Sample data x = np.array([0, 1, 2, 3, 4]) y = np.array([1, 2.7, 7.4, 20.1, 54.6])    # Define the exponential function def exp_func(x, a, b):     return a * np.exp(b * x)  # Fit curve params, _ = curve_fit(exp_func, x, y) a, b = params print(f"a = {a}, b = {b}")  # Plot x_line = np.linspace(min(x), max(x), 100) plt.scatter(x, y) plt.plot(x_line, exp_func(x_line, *params), color='green') plt.show() 

Output:

Screenshot-2025-06-23-095544
Output for Exponential Regression


3. Using SciPy for Logarithmic Regression

Logarithmic Regression models relationships where the dependent variable changes proportionally to the logarithm of the independent variable:

y = a + b \ln(x)

  • This code fits a Logarithmic regression model to the data using curve_fit. It estimates the parameters a and b that best describe the relationship between x and y.
  • The plot visualizes the original data points alongside the fitted logarithmic curve.
Python
import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt  x = np.array([1, 2, 3, 4, 5]) y = np.array([2.3, 3.1, 3.8, 4.4, 5.0])  def log_func(x, a, b):     return a + b * np.log(x)  params, _ = curve_fit(log_func, x, y) print(f"a = {params[0]}, b = {params[1]}")  # Plot x_line = np.linspace(min(x), max(x), 100) plt.scatter(x, y) plt.plot(x_line, log_func(x_line, *params), color='purple') plt.show() 

Output:

Screenshot-2025-06-23-095722
Output for Logarithmic Regression


4. Using SciPy for Logistic Regression

Logistic Regression models S shaped curves often used for classification or growth processes:

y = \frac{L}{1 + e^{-k(x - x_0)}}

  • This code fits a Logistic regression curve to the data, modelling an S shaped growth using the logistic function.
  • It estimates parameters for curve height (L), growth rate (k) and midpoint (x0) using curve_fit. The plot shows the noisy data points and the smooth fitted logistic curve.
Python
import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt  x = np.linspace(0, 10, 100) y = 1 / (1 + np.exp(-1.5*(x - 5))) + 0.05 * np.random.normal(size=len(x))   def logistic(x, L, k, x0):     return L / (1 + np.exp(-k*(x - x0)))  params, _ = curve_fit(logistic, x, y, p0=[1, 1, 1]) print(f"L = {params[0]}, k = {params[1]}, x0 = {params[2]}")  plt.scatter(x, y, s=10) plt.plot(x, logistic(x, *params), color='red') plt.title("Logistic Regression") plt.show() 

Output:

Screenshot-2025-06-23-095850
Output for Logistic Regression



Next Article
Regression Analysis with SciPy

S

shrurfu5
Improve
Article Tags :
  • AI-ML-DS
  • Python-scipy
  • AI-ML-DS With Python

Similar Reads

    What is Regression Analysis?
    In this article, we discuss about regression analysis, types of regression analysis, its applications, advantages, and disadvantages.What is regression?Regression Analysis is a supervised learning analysis where supervised learning is the analyzing or predicting the data based on the previously avai
    15+ min read
    Multiple Linear Regression With scikit-learn
    In this article, let's learn about multiple linear regression using scikit-learn in the Python programming language. Regression is a statistical method for determining the relationship between features and an outcome variable or result. Machine learning, it's utilized as a method for predictive mode
    11 min read
    Orthogonal distance regression using SciPy
    Regression basically involves determining the relationship between a dependent variable and one or more independent variables. It generally involves finding the best fit line that minimizes the sum of squared errors for each point. Based on the implementation procedures, regression algorithms are cl
    4 min read
    K-Nearest Neighbors (KNN) Regression with Scikit-Learn
    K-Nearest Neighbors (KNN) is one of the simplest and most intuitive machine learning algorithms. While it is commonly associated with classification tasks, KNN can also be used for regression. This article will delve into the fundamentals of KNN regression, how it works, and how to implement it usin
    7 min read
    Data Analysis with Python
    Data Analysis is the technique of collecting, transforming and organizing data to make future predictions and informed data-driven decisions. It also helps to find possible solutions for a business problem. In this article, we will discuss how to do data analysis with Python i.e. analyzing numerical
    15+ 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