Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

python scipy.odrpack.odr example (with sample input / output)?

I am a satisfied user of scipy.optimize.leastsq.

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

Unfortunately, I cannot find an online tutorial which includes sample code with sample input and output. (I am trying to make this as easy as possible.)

I would appreciate it if somebody could post sample code with sample I/O. This would be easy for somebody who uses the routine a lot.

Thanks! Bill

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a fleshed-out version of the example 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...