简体   繁体   English

对scipy curve_fit进行猜测

[英]Using a guess with scipy curve_fit

I have a function that I want to curve fit with knowing the error of the curve fit. 我有一个要知道曲线拟合误差的曲线拟合函数。 I'm trying to use scipy.optimize.curve_fit to do this but am running into problem. 我正在尝试使用scipy.optimize.curve_fit来执行此操作,但遇到了问题。 Right now my code is: 现在我的代码是:

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

pi = np.pi
sqrt = np.sqrt
mean = np.mean

A = 1
T_2 = 100
nu_0 = 10
phi_0 = 0
n = .001
nu_s = 500
T = 1000

t = np.linspace(0, T, num = (nu_s*T))

def S_n(A,t,T_2,nu_0,phi_0,n,nu_s,T):
    return (A/np.sqrt(2))*np.exp(-t/T_2)*np.cos(2*pi*nu_0*t+phi_0)

S = S_n(A,t,T_2,nu_0,phi_0,n,nu_s,T) + np.random.normal(0, n, nu_s*T)

guess = np.array([A,T_2,nu_0,phi_0,n,nu_s,T])
print guess

popt, pcov = curve_fit(S_n,t,S, guess)
print popt
perr = sqrt(np.diag(pcov))
print perr

Which gives me infinite error. 这给了我无限的错误。 I'm not sure I am doing my guess correctly because in the equation, everything remains constant except t, so do I leave t out of my guess because it is no longer an array because t is a sequence. 我不确定我是否正确地进行了猜测,因为在方程式中,除了t之外,其他所有东西都保持不变,所以我将t排除在猜测之外,因为t不再是数组,因为t是一个序列。 When I leave t out of the guess, which I am doing here I receive values for each variable to be far from the values I give initially with infinite error. 当我不做任何猜测的时候(我在这里这样做),我收到的每个变量的值都与最初给我的无穷大的值相去甚远。 If I include t in the guess, then I get an error. 如果我在猜测中包括t,那么我会得到一个错误。

You didn't take the order of the parameters to curve_fit into account: 您没有考虑到参数curve_fit的顺序:

Definition: curve_fit(f, xdata, ydata, p0=None, sigma=None, **kw) 定义:curve_fit(f,xdata,ydata,p0 = None,sigma = None,** kw)

Docstring: Use non-linear least squares to fit a function, f, to data. Docstring:使用非线性最小二乘法将函数f拟合到数据。

Assumes ydata = f(xdata, *params) + eps 假设ydata = f(xdata, *params) + eps

Parameters 参数

f : callable The model function, f(x, ...). f:可调用的模型函数f(x,...)。 It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments. 它必须将自变量作为第一个参数,并将参数作为单独的剩余参数来容纳。

If you make a function: 如果执行功能:

def Sm(t, A,T_2,nu_0,phi_0,n,nu_s,T):
    return S_n(A, t, T_2,nu_0,phi_0,n,nu_s,T)

(notice the order of the first 2 parameters has changed) and pass that to curve_fit , it will work. (注意前两个参数的顺序已更改),并将其传递给curve_fit ,它将起作用。

It is probably more pythonic to clean your original function though: 不过,清理原始功能可能更像pythonic:

def S_n(t, A, T_2, nu_0, phi_0, n, nu_s, T):
    return (A/np.sqrt(2))*np.exp(-t/T_2)*np.cos(2*pi*nu_0*t+phi_0)

S = S_n(t, A, T_2, nu_0, phi_0, n, nu_s, T) + np.random.normal(0, n, nu_s*T)

Then you can pass S_n to curve_fit unchanged, as well as S and guess . 然后,您可以将S_n不变地传递给curve_fit ,也可以将Sguess传递给。

To be clear, the following code produces the desired fit: 需要明确的是,以下代码可产生所需的拟合度:

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

def S_n(t, amplitude, sigma,freq0,phase):
    return (amplitude/np.sqrt(2))*np.exp(-t/sigma)*np.cos(2*np.pi*freq0*t+ phase)

amplitude = 1
sigma = 100
freq0 = 10
phase = 0
n = .001
sample_freq = 500
period = 1000

t = np.linspace(0, period, num = (sample_freq*period))
S = S_n(t, amplitude, sigma,freq0,phi_0) + np.random.normal(0, n, sample_freq*period)

guess = np.array([amplitude, sigma, freq0, phase ])
print guess
popt, pcov = curve_fit(S_n,t,S, guess)
print popt
print(np.all(np.isfinite(pcov)))
# output
[  1 100  10   0]
[  1.00000532e+00   1.00000409e+02   1.00000000e+01  -1.49076430e-05]
True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM