Python scipy.odrpack.odr example (with sample I / O)?

I am a satisfied user scipy.optimize.leastsq.

I now have - in fact always had - x, y data with variable error columns, and it looks like scipy.odrpack.odr is what I need to use to respect the big uncertainty in some data.

Unfortunately, I cannot find an online tutorial that includes sample code with sample input and output. (I try to make it as simple as possible.)

I would appreciate if anyone could post sample code with sample I / O. This would be easy for those who often use routine.

Thank! Bill

+5
source share
1 answer

This is the version of the example below in the docs :

import numpy as np
import scipy.odr.odrpack as odrpack
np.random.seed(1)

N = 100
x = np.linspace(0,10,N)
y = 3*x - 1 + np.random.random(N)
sx = np.random.random(N)
sy = np.random.random(N)

def f(B, x):
    return B[0]*x + B[1]
linear = odrpack.Model(f)
# mydata = odrpack.Data(x, y, wd=1./np.power(sx,2), we=1./np.power(sy,2))
mydata = odrpack.RealData(x, y, sx=sx, sy=sy)

myodr = odrpack.ODR(mydata, linear, beta0=[1., 2.])
myoutput = myodr.run()
myoutput.pprint()
# Beta: [ 3.02012862 -0.63168734]
# Beta Std Error: [ 0.01188347  0.05616458]
# Beta Covariance: [[ 0.00067276 -0.00267082]
#  [-0.00267082  0.01502792]]
# Residual Variance: 0.209906660703
# Inverse Condition #: 0.105981202542
# Reason(s) for Halting:
#   Sum of squares convergence
+11

All Articles