Scipy.Odr多变量回归 [英] Scipy.Odr multiple variable regression

查看:179
本文介绍了Scipy.Odr多变量回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用scipy.odr执行多维ODR.我阅读了API文档,它说多维是可能的,但我无法使其起作用.我在互联网上找不到有效的示例,API真的很粗糙,也没有给出如何进行操作的提示.

I would like to perform a multidimensional ODR with scipy.odr. I read the API documentation, it says that multi-dimensionality is possible, but I cannot make it work. I cannot find working example on the internet and API is really crude and give no hints how to proceed.

这是我的MWE:

import numpy as np
import scipy.odr

def linfit(beta, x):
    return beta[0]*x[:,0] + beta[1]*x[:,1] + beta[2]

n = 1000
t = np.linspace(0, 1, n)
x = np.full((n, 2), float('nan'))
x[:,0] = 2.5*np.sin(2*np.pi*6*t)+4
x[:,1] = 0.5*np.sin(2*np.pi*7*t + np.pi/3)+2
e = 0.25*np.random.randn(n)
y = 3*x[:,0] + 4*x[:,1] + 5 + e

print(x.shape)
print(y.shape)

linmod = scipy.odr.Model(linfit)
data = scipy.odr.Data(x, y)
odrfit = scipy.odr.ODR(data, linmod, beta0=[1., 1., 1.])
odrres = odrfit.run()
odrres.pprint()

它引发以下异常:

scipy.odr.odrpack.odr_error: number of observations do not match

这似乎与我的矩阵形状有关,但是我不知道如何正确地对其进行成形.有人知道吗?

Which seems to be related to my matrix shapes, but I do not know how must I shape it properly. Does anyone know?

推荐答案

首先,以我的经验,scipy.odr主要使用数组,而不是矩阵.该库似乎在此过程中进行了大量的大小检查,并且使其与多个变量一起使用似乎很麻烦.

Firstly, in my experience scipy.odr uses mostly arrays, not matrices. The library seems to make a large amount of size checks along the way and getting it to work with multiple variables seems to be quite troublesome.

这是我通常如何使其工作(至少在python 2.7上工作)的工作流程:

This is the workflow how I usually get it to work (and worked at least on python 2.7):

import numpy as np
import scipy.odr

n = 1000
t = np.linspace(0, 1, n)

def linfit(beta, x):
    return beta[0]*x[0] + beta[1]*x[1] + beta[2] #notice changed indices for x

x1 = 2.5*np.sin(2*np.pi*6*t)+4
x2 = 0.5*np.sin(2*np.pi*7*t + np.pi/3)+2

x = np.row_stack( (x1, x2) ) #odr doesn't seem to work with column_stack

e = 0.25*np.random.randn(n)
y = 3*x[0] + 4*x[1] + 5 + e #indices changed

linmod = scipy.odr.Model(linfit)
data = scipy.odr.Data(x, y)
odrfit = scipy.odr.ODR(data, linmod, beta0=[1., 1., 1.])
odrres = odrfit.run()
odrres.pprint()

因此,使用相同的(1D?)数组,使用row_stack和按单个索引号进行地址处理似乎是可行的.

So using identical (1D?) arrays, using row_stack and adressing by single index number seems to work.

这篇关于Scipy.Odr多变量回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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