使用scipy.ODR的线性回归失败(解决方案不完全排名) [英] Linear Regression using scipy.ODR fails (Not full rank at solution)

查看:146
本文介绍了使用scipy.ODR的线性回归失败(解决方案不完全排名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此正在尝试使用scipy.odr进行线性回归.但是,它失败了. scipy.odr以前为我工作过,并且我的代码中没有看到任何错误.我能想到的唯一原因是坡度可能太小,但我看不出它怎么会打扰人. 谢谢您的帮助.

so was trying a linear regression with scipy.odr. However, it failed miserably. scipy.odr has worked for me before, and I don't see any errors in my code. The only reason I can think of is that the slope may be too small but I don't see how that could bother scipy. Thank you for your help.

代码:

#!/usr/bin/env python3 -i
# -*- coding: iso-8859-1 -*-

import matplotlib.pyplot as plt
import numpy as np
from scipy.odr import *


fig = plt.figure()
ax1 = fig.add_subplot(111)


x   = np.linspace(0,1E15,10)
y   = 1E-15*x-2



ax1.set_xlim(-0.05E15,1.1E15)
ax1.set_ylim(-2.1, -0.7)


ax1.plot(x, y, 'o')

# Fit using odr
def f(B, x):
    return B[0]*x + B[1]    

linear = Model(f)
mydata = RealData(x, y)
myodr = ODR(mydata, linear, beta0=[1., 2.])
myoutput = myodr.run()
myoutput.pprint()

a, b = myoutput.beta
sa, sb = myoutput.sd_beta

xp = np.linspace(ax1.get_xlim()[0], ax1.get_xlim()[1], 1000)
yp = a*xp+b


ax1.plot(xp,yp)

plt.show()

这是最终的终端输出:

Beta: [ -4.84615388e-15   2.00000000e+00]
Beta Std Error: [  8.14077323e-16   0.00000000e+00]
Beta Covariance: [[  1.46153845e-31   0.00000000e+00]
 [  0.00000000e+00   0.00000000e+00]]
Residual Variance: 4.534412955465587
Inverse Condition #: 1.0
Reason(s) for Halting:
  Problem is not full rank at solution
  Parameter convergence

这是结果图形:

edit:我的odr回归代码来自 http://docs.scipy.org/doc/scipy/reference/odr.html

edit: My code for the odr-regression comes from http://docs.scipy.org/doc/scipy/reference/odr.html

推荐答案

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
x   = np.linspace(0,1E15,10)
y   = 1E-15*x-2    
ax1.set_xlim(-0.05E15,1.1E15)
ax1.set_ylim(-2.1, -0.7)    
ax1.plot(x, y, 'o')
# Fit using odr


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

sx = np.std(x)
sy = np.std(y)
linear = Model(f)
mydata = RealData(x=x,y=y, sx=sx, sy=sy)
myodr = ODR(mydata, linear, beta0=[1.00000000e-15, 2.])
myoutput = myodr.run()
myoutput.pprint()

a, b = myoutput.beta
sa, sb = myoutput.sd_beta

xp = np.linspace(min(x), max(x), 1000)
yp = a*xp+b
ax1.plot(xp,yp)
plt.show()

这篇关于使用scipy.ODR的线性回归失败(解决方案不完全排名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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