scipy中最小二乘法的雅可比函数的方法签名 [英] Method signature for Jacobian of a least squares function in scipy

查看:107
本文介绍了scipy中最小二乘法的雅可比函数的方法签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提供向

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 it'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)

如果我想使用col_deriv=0,我认为我基本上必须对dfunc返回的内容进行转置.不过您说的很对:关于此的文档不​​是很好.

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.

这篇关于scipy中最小二乘法的雅可比函数的方法签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆