2013-04-05 4 views
11

Ich möchte eine logarithmische Anpassung vornehmen. Aber ich bekomme immer einen Laufzeitfehler:Scipy curpfit Laufzeitfehler: Optimale Parameter nicht gefunden: Anzahl der Aufrufe an Funktion hat maxfev erreicht = 1000

Optimal parameters not found: Number of calls to function has reached maxfev = 1000

Ich benutze das folgende Skript. Kann mir jemand sagen, wo ich falsch liege? Ich benutze Spyder und bin immer noch Anfänger.

import math 
import matplotlib as mpl 
from scipy.optimize import curve_fit 
import numpy as np 

#data 
F1=[735.0,696.0,690.0,683.0,680.0,678.0,679.0,675.0,671.0,669.0,668.0,664.0,664.0] 
t1=[1,90000.0,178200.0,421200.0,505800.0,592200.0,768600.0,1036800.0,1371600.0,1630800.0,1715400.0,2345400.0,2409012.0] 

F1n=np.array(F1) 
t1n=np.array(t1) 

plt.plot(t1,F1,'ro',label="original data") 

# curvefit 
def func(t,a,b): 
    return a+b*np.log(t) 

t=np.linspace(0,3600*24*28,13) 

popt, pcov = curve_fit(func, t, F1n, maxfev=1000)  

plt.plot(t, func(t, *popt), label="Fitted Curve") 

plt.legend(loc='upper left') 
plt.show() 

Antwort

2

Nach dem Import-Anweisungen Festsetzung:

#import matplotlib as mpl 
import matplotlib.pyplot as plt 

Code erzeugt den folgenden Fehler:

RuntimeWarning: divide by zero encountered in log 

Wechsel:

#t=np.linspace(0,3600*24*28,13) 
t=np.linspace(1,3600*24*28,13) 

die folgende Ausgabe erzeugt:

enter image description here

8

Ihre ursprünglichen Daten t1 und F1. Daher sollte curve_fit als zweites Argument t1 angegeben werden, nichtt.

popt, pcov = curve_fit(func, t1, F1, maxfev=1000) 

Nun, wenn Sie angepassten Parameter erhalten, popt Sie func an den Punkten in t auswerten können eine angepasste Kurve zu erhalten:

t = np.linspace(1, 3600 * 24 * 28, 13) 
plt.plot(t, func(t, *popt), label="Fitted Curve") 

(I entfernt Null von t (pro StuGrey Antwort) zu vermeiden die Warning: divide by zero encountered in log.)


import matplotlib.pyplot as plt 
import scipy.optimize as optimize 
import numpy as np 

# data 
F1 = np.array([ 
    735.0, 696.0, 690.0, 683.0, 680.0, 678.0, 679.0, 675.0, 671.0, 669.0, 668.0, 
    664.0, 664.0]) 
t1 = np.array([ 
    1, 90000.0, 178200.0, 421200.0, 505800.0, 592200.0, 768600.0, 1036800.0, 
    1371600.0, 1630800.0, 1715400.0, 2345400.0, 2409012.0]) 

plt.plot(t1, F1, 'ro', label="original data") 

# curvefit 

def func(t, a, b): 
    return a + b * np.log(t) 

popt, pcov = optimize.curve_fit(func, t1, F1, maxfev=1000) 
t = np.linspace(1, 3600 * 24 * 28, 13) 
plt.plot(t, func(t, *popt), label="Fitted Curve") 
plt.legend(loc='upper left') 
plt.show() 

enter image description here

Verwandte Themen