3
5

More than 3 years have passed since last update.

scipy の curve_fit の使い方

Last updated at Posted at 2020-04-16

exp 関数のカーブフィットの使い方です。
次のような結果を得ます。
curve_fit_apr16.png

curve_fit03.py
#! /usr/bin/python
#
#   curve_fit03.py
#
#                   Apr/16/2020             
# ------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# ------------------------------------------------------------------
def exp(xx, aa, bb, dd, pp):
    rvalue = 0.0
    try:
        rvalue = dd * np.exp(aa * xx - bb) + pp
    except Exception as ee:
        sys.stderr.write("*** error *** in exp ***\n")
        sys.stderr.write(str(ee) + "\n")
    return rvalue
# ------------------------------------------------------------------

xx = np.array([0,1,2,3,4,5,6,7,8])
yy = np.array([95.0, 100.0, 114.0,134.0,178.0,245.0,312.0,412.0,603.0])

popt, pcov = curve_fit(exp, xx, yy)

plt.figure()
plt.plot(xx, yy, 'ko', label="Original Data")
plt.plot(xx, exp(xx, *popt), 'r-', label="Fitted Curve")
plt.legend()
plt.show()

# ------------------------------------------------------------------
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5