开发者

Method signature for Jacobian of a least squares function in scipy

开发者 https://www.devze.com 2023-01-21 11:10 出处:网络
Can anyone provide an example of providing a Jacobian to a least squares function in scipy? I can\'t figure out the method signature they want - they say it should be a function, yet 开发者_如何学JAV

Can anyone provide an example of providing a Jacobian to a least squares function in scipy?

I can't figure out the method signature they want - they say it should be a function, yet 开发者_如何学JAVAit's very hard to figure out what input parameters in what order this function should accept.


Here's the exponential decay fitting that I got to work with this:

import numpy as np
from scipy.optimize import leastsq

def f(var,xs):
    return var[0]*np.exp(-var[1]*xs)+var[2]

def func(var, xs, ys):
    return f(var,xs) - ys

def dfunc(var,xs,ys):
    v = np.exp(-var[1]*xs)
    return [v,-var[0]*xs*v,np.ones(len(xs))]

xs = np.linspace(0,4,50)
ys = f([2.5,1.3,0.5],xs)
yn = ys + 0.2*np.random.normal(size=len(xs))
fit = leastsq(func,[10,10,10],args=(xs,yn),Dfun=dfunc,col_deriv=1)

If I wanted to use col_deriv=0, I think that I would have to basically take the transpose of what I return with dfunc. You're quite right though: the documentation on this isn't so great.

0

精彩评论

暂无评论...
验证码 换一张
取 消