Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

[python] Fit a curve through a given set of data points

#!/usr/bin/env python

# import the necessary modules
import numpy as np
from scipy.optimize import curve_fit

# Define the fitting function
def func(x, a, b, c):
  return a*np.exp(-b*x) + c

# Generate x and y values which the curve will be fitted to
# (In practical cases, these should be read in)
x = np.linspace(0,4,50)
y = func(x, 2.5, 1.3, 0.5)
yn = y + 0.2*np.random.normal(size=len(x))

# The actual fitting part
# popt = the fitted parameters as a tuple, namely (a,b,c)
# pconv = The estimated covariance of popt.
#        The diagonals provide the variance of the parameter estimate.
popt, pcov = curve_fit(func, x, yn)

Source: Scipy



This post first appeared on A Walk In The Rain | My Life And My Thoughts, please read the originial post: here

Share the post

[python] Fit a curve through a given set of data points

×

Subscribe to A Walk In The Rain | My Life And My Thoughts

Get updates delivered right to your inbox!

Thank you for your subscription

×