scipy 最小二乘法中的正交回归拟合 [英] Orthogonal regression fitting in scipy least squares method

查看:56
本文介绍了scipy 最小二乘法中的正交回归拟合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scipy lib 中的 leastsq 方法将曲线拟合到一些数据.这个方法意味着在这个数据中 Y 值取决于某个 X 参数.并计算曲线与Y轴数据点的最小距离(dy)

The leastsq method in scipy lib fits a curve to some data. And this method implies that in this data Y values depends on some X argument. And calculates the minimal distance between curve and the data point in the Y axis (dy)

但是如果我需要计算两个轴(dy 和 dx)上的最小距离怎么办

But what if I need to calculate minimal distance in both axes (dy and dx)

有什么方法可以实现这个计算吗?

Is there some ways to implement this calculation?

这是使用单轴计算时的代码示例:

Here is a sample of code when using one axis calculation:

import numpy as np
from scipy.optimize import leastsq

xData = [some data...]
yData = [some data...]

def mFunc(p, x, y):
    return y - (p[0]*x**p[1])  # is takes into account only y axis

plsq, pcov = leastsq(mFunc, [1,1], args=(xData,yData))
print plsq

我最近尝试了 scipy.odr 库,它只为线性函数返回正确的结果.对于 y=a*x^b 等其他函数,它返回错误的结果.这就是我使用它的方式:

I recently tryed scipy.odr library and it returns the proper results only for linear function. For other functions like y=a*x^b it returns wrong results. This is how I use it:

def f(p, x):      
    return p[0]*x**p[1]

myModel = Model(f)
myData = Data(xData, yData)
myOdr = ODR(myData, myModel , beta0=[1,1])
myOdr.set_job(fit_type=0) #if set fit_type=2, returns the same as leastsq
out = myOdr.run()
out.pprint()

这会返回错误的结果,这是不希望的,并且在某些输入数据中甚至不接近真实.可能是,有一些特殊的使用方法,我做错了什么?

This returns wrong results, not desired, and in some input data not even close to real. May be, there is some special ways of using it, what do I do wrong?

推荐答案

我找到了解决方案.Scipy Odrpack 正常工作,但它需要一个很好的初始猜测才能获得正确的结果.所以我把这个过程分为两个步骤.

I've found the solution. Scipy Odrpack works noramally but it needs a good initial guess for correct results. So I divided the process into two steps.

第一步:使用序数最小二乘法找到初始猜测.

First step: find the initial guess by using ordinaty least squares method.

第二步:将 ODR 中的这些初始猜测替换为 beta0 参数.

Second step: substitude these initial guess in ODR as beta0 parameter.

它在可接受的速度下运行良好.

And it works very well with an acceptable speed.

谢谢你们,你们的建议让我找到了正确的解决方案

Thank you guys, your advice directed me to the right solution

这篇关于scipy 最小二乘法中的正交回归拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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