0

尝试使用 scipy.odr 进行线性回归也是如此。然而,它惨遭失败。scipy.odr 以前为我工作过,我没有在我的代码中看到任何错误。我能想到的唯一原因是斜率可能太小,但我不明白这会如何打扰 scipy。谢谢您的帮助。

编码:

#!/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

这是生成的图形:

废话回归

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

4

1 回答 1

1
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()

在此处输入图像描述

于 2014-12-04T16:42:54.710 回答