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:
SciPy | Curve Fitting
Next article icon

SciPy | Curve Fitting

Last Updated : 06 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a Dataset comprising of a group of points, find the best fit representing the Data.
We often have a dataset comprising of data following a general path, but each data has a standard deviation which makes them scattered across the line of best fit. We can get a single line using curve-fit() function. 
Using SciPy : 
Scipy is the scientific computing module of Python providing in-built functions on a lot of well-known Mathematical functions. The scipy.optimize package equips us with multiple optimization procedures. A detailed list of all functionalities of Optimize can be found on typing the following in the iPython console: 
 

help(scipy.optimize)


Among the most used are Least-Square minimization, curve-fitting, minimization of multivariate scalar functions etc.
Curve Fitting Examples - 
Input : 
 


Output : 
 


Input : 
 


Output : 
 


As seen in the input, the Dataset seems to be scattered across a sine function in the first case and an exponential function in the second case, Curve-Fit gives legitimacy to the functions and determines the coefficients to provide the line of best fit.
  
Code showing the generation of the first example - 
 

Python3
import numpy as np  # curve-fit() function imported from scipy from scipy.optimize import curve_fit  from matplotlib import pyplot as plt  # numpy.linspace with the given arguments # produce an array of 40 numbers between 0 # and 10, both inclusive x = np.linspace(0, 10, num = 40)   # y is another array which stores 3.45 times # the sine of (values in x) * 1.334.  # The random.normal() draws random sample  # from normal (Gaussian) distribution to make # them scatter across the base line y = 3.45 * np.sin(1.334 * x) + np.random.normal(size = 40)  # Test function with coefficients as parameters def test(x, a, b):     return a * np.sin(b * x)  # curve_fit() function takes the test-function # x-data and y-data as argument and returns  # the coefficients a and b in param and # the estimated covariance of param in param_cov param, param_cov = curve_fit(test, x, y)   print("Sine function coefficients:") print(param) print("Covariance of coefficients:") print(param_cov)  # ans stores the new y-data according to  # the coefficients given by curve-fit() function ans = (param[0]*(np.sin(param[1]*x)))  '''Below 4 lines can be un-commented for plotting results  using matplotlib as shown in the first example. '''  # plt.plot(x, y, 'o', color ='red', label ="data") # plt.plot(x, ans, '--', color ='blue', label ="optimized data") # plt.legend() # plt.show() 

Output: 
Sine function coefficients: [ 3.66474998  1.32876756] Covariance of coefficients: [[  5.43766857e-02  -3.69114170e-05]  [ -3.69114170e-05   1.02824503e-04]]

 

  
Second example can be achieved by using the numpy exponential function shown as follows: 
 

Python3
x = np.linspace(0, 1, num = 40)  y = 3.45 * np.exp(1.334 * x) + np.random.normal(size = 40)  def test(x, a, b):     return a*np.exp(b*x)  param, param_cov = curve_fit(test, x, y) 

However, if the coefficients are too large, the curve flattens and fails to provide the best fit. The following code explains this fact:
 

Python3
import numpy as np from scipy.optimize import curve_fit  from matplotlib import pyplot as plt  x = np.linspace(0, 10, num = 40)  # The coefficients are much bigger. y = 10.45 * np.sin(5.334 * x) + np.random.normal(size = 40)  def test(x, a, b):     return a * np.sin(b * x)  param, param_cov = curve_fit(test, x, y)  print("Sine function coefficients:") print(param) print("Covariance of coefficients:") print(param_cov)  ans = (param[0]*(np.sin(param[1]*x)))  plt.plot(x, y, 'o', color ='red', label ="data") plt.plot(x, ans, '--', color ='blue', label ="optimized data") plt.legend() plt.show() 

Output: 
Sine function coefficients: [ 0.70867169  0.7346216 ] Covariance of coefficients: [[ 2.87320136 -0.05245869]  [-0.05245869  0.14094361]]
 

 

The blue dotted line is undoubtedly the line with best-optimized distances from all points of the dataset, but it fails to provide a sine function with the best fit.
Curve Fitting should not be confused with Regression. They both involve approximating data with functions. But the goal of Curve-fitting is to get the values for a Dataset through which a given set of explanatory variables can actually depict another variable. Regression is a special case of curve fitting but here you just don't need a curve that fits the training data in the best possible way(which may lead to overfitting) but a model which is able to generalize the learning and thus predict new points efficiently.
 


Next Article
SciPy | Curve Fitting

A

ArijitGayen
Improve
Article Tags :
  • Machine Learning
  • Python-scipy
  • python
Practice Tags :
  • Machine Learning
  • python

Similar Reads

    3D Curve Fitting With Python
    Curve fitting is a widely used technique in the field of data analysis and mathematical modeling. It involves the process of finding a mathematical function that best approximates a set of data points. In 3D curve fitting, the process is extended to three-dimensional space, where the goal is to find
    7 min read
    Validation Curve
    Model validation is an important part of the data science project since want to select a model which not only performs well on our training dataset but also has good accuracy on the testing dataset. Model validation helps us in finding a model which has low variance. What is Validation Curve   A Val
    4 min read
    Using Learning Curves - ML
    A learning model of a Machine Learning model shows how the error in the prediction of a Machine Learning model changes as the size of the training set increases or decreases. Before we continue, we must first understand what variance and bias mean in the Machine Learning model. Bias: It is basically
    4 min read
    SciPy - Integration of a Differential Equation for Curve Fit
    In Machine Learning, often what we do is gather data, visualize it, then fit a curve in the graph and then predict certain parameters based on the curve fit. If we have some theoretical data we can use curve fitting from the verified dataset to extract the equation and verify it. So to find the equa
    2 min read
    Cubic spline Interpolation
    Interpolation: We estimate f(x) for arbitrary x, by drawing a smooth curve through the xi. If the desired x is between the largest and smallest of the xi then it is called interpolation, otherwise, it is called Extrapolation. Random pointsLinear Interpolation: Linear Interpolation is a way of curve
    5 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