mirror of https://github.com/openXC7/prjxray.git
timfuz leastsq: remove test code
Signed-off-by: John McMaster <johndmcmaster@gmail.com>
This commit is contained in:
parent
f6fb4cb60c
commit
5ecbd135ce
|
|
@ -175,166 +175,5 @@ def main():
|
|||
finally:
|
||||
print('Exiting after %s' % bench)
|
||||
|
||||
# optimize.curve_fit wrapper
|
||||
def test1():
|
||||
# Generate artificial data = straight line with a=0 and b=1
|
||||
# plus some noise.
|
||||
xdata = np.array([0.0,1.0,2.0,3.0,4.0,5.0])
|
||||
ydata = np.array([0.1,0.9,2.2,2.8,3.9,5.1])
|
||||
# Initial guess.
|
||||
x0 = np.array([0.0, 0.0, 0.0])
|
||||
sigma = np.array([1.0,1.0,1.0,1.0,1.0,1.0])
|
||||
|
||||
def func(x, a, b, c):
|
||||
return a + b*x + c*x*x
|
||||
|
||||
print(optimize.curve_fit(func, xdata, ydata, x0, sigma))
|
||||
|
||||
# optimize.leastsq
|
||||
def test2():
|
||||
|
||||
# The function whose square is to be minimised.
|
||||
# params ... list of parameters tuned to minimise function.
|
||||
# Further arguments:
|
||||
# xdata ... design matrix for a linear model.
|
||||
# ydata ... observed data.
|
||||
def func(params, xdata, ydata):
|
||||
return (ydata - np.dot(xdata, params))
|
||||
|
||||
x0 = np.array([0.0, 0.0])
|
||||
|
||||
'''
|
||||
a = 10
|
||||
a + b = 100
|
||||
'''
|
||||
xdata = np.array([[1, 0],
|
||||
[1, 1]])
|
||||
ydata = np.array([10, 100])
|
||||
|
||||
'''
|
||||
x [ 10. 90.]
|
||||
cov_x [[ 1. -1.]
|
||||
[-1. 2.]]
|
||||
infodictx {'ipvt': array([1, 2], dtype=int32), 'qtf': array([ 1.69649118e-10, 1.38802454e-10]), 'nfev': 7, 'fjac': array([[ 1.41421356, 0.70710678],
|
||||
[ 0.70710678, 0.70710678]]), 'fvec': array([ 0., 0.])}
|
||||
mesg The relative error between two consecutive iterates is at most 0.000000
|
||||
ier 2
|
||||
Solution found: True
|
||||
'''
|
||||
x, cov_x, infodict, mesg, ier = optimize.leastsq(func, x0, args=(xdata, ydata), full_output=True)
|
||||
print('x', x)
|
||||
print('cov_x', cov_x)
|
||||
print('infodictx', infodict)
|
||||
print('mesg', mesg)
|
||||
print('ier', ier)
|
||||
print(' Solution found: %s' % (ier in (1, 2, 3, 4)))
|
||||
|
||||
# non-square
|
||||
def test3():
|
||||
def func(params, xdata, ydata):
|
||||
return (ydata - np.dot(xdata, params))
|
||||
|
||||
x0 = np.array([0.0, 0.0, 0.0])
|
||||
|
||||
'''
|
||||
a = 10
|
||||
a + b + c = 100
|
||||
'''
|
||||
xdata = np.array([[1, 0, 0],
|
||||
[1, 1, 1],
|
||||
[0, 0, 0]])
|
||||
ydata = np.array([10, 100, 0])
|
||||
|
||||
x, cov_x, infodict, mesg, ier = optimize.leastsq(func, x0, args=(xdata, ydata), full_output=True)
|
||||
print('x', x)
|
||||
print('cov_x', cov_x)
|
||||
print('infodictx', infodict)
|
||||
print('mesg', mesg)
|
||||
print('ier', ier)
|
||||
print(' Solution found: %s' % (ier in (1, 2, 3, 4)))
|
||||
|
||||
def test4():
|
||||
def func(params):
|
||||
print('')
|
||||
print('iter')
|
||||
print(params)
|
||||
print(xdata)
|
||||
print(ydata)
|
||||
return (ydata - np.dot(xdata, params))
|
||||
|
||||
x0 = np.array([0.0, 0.0, 0.0])
|
||||
|
||||
'''
|
||||
You must have at least as many things to optimize as variables
|
||||
That is, the system must be plausibly constrained for it to attempt a solve
|
||||
If not, you'll get a message like
|
||||
TypeError: Improper input: N=3 must not exceed M=2
|
||||
'''
|
||||
xdata = np.array([[1, 0, 0],
|
||||
[1, 1, 1],
|
||||
[1, 0, 1],
|
||||
[0, 1, 1],
|
||||
])
|
||||
ydata = np.array([10, 100, 120, 140])
|
||||
|
||||
x, cov_x, infodict, mesg, ier = optimize.leastsq(func, x0, full_output=True)
|
||||
print('x', x)
|
||||
print('cov_x', cov_x)
|
||||
print('infodictx', infodict)
|
||||
print('mesg', mesg)
|
||||
print('ier', ier)
|
||||
print(' Solution found: %s' % (ier in (1, 2, 3, 4)))
|
||||
|
||||
def test5():
|
||||
from scipy.optimize import least_squares
|
||||
|
||||
def fun_rosenbrock(x):
|
||||
return np.array([10 * (x[1] - x[0]**2), (1 - x[0])])
|
||||
x0_rosenbrock = np.array([2, 2])
|
||||
res = least_squares(fun_rosenbrock, x0_rosenbrock)
|
||||
'''
|
||||
active_mask: array([ 0., 0.])
|
||||
cost: 9.8669242910846867e-30
|
||||
fun: array([ 4.44089210e-15, 1.11022302e-16])
|
||||
grad: array([ -8.89288649e-14, 4.44089210e-14])
|
||||
jac: array([[-20.00000015, 10. ],
|
||||
[ -1. , 0. ]])
|
||||
message: '`gtol` termination condition is satisfied.'
|
||||
nfev: 3
|
||||
njev: 3
|
||||
optimality: 8.8928864934219529e-14
|
||||
status: 1
|
||||
success: True
|
||||
x: array([ 1., 1.])
|
||||
'''
|
||||
print(res)
|
||||
|
||||
def test6():
|
||||
def func(params):
|
||||
return (ydata - np.dot(xdata, params))
|
||||
|
||||
x0 = np.array([0.0, 0.0, 0.0])
|
||||
|
||||
'''
|
||||
a = 10
|
||||
a + b + c = 100
|
||||
'''
|
||||
xdata = np.array([[1, 0, 0],
|
||||
[1, 1, 1],
|
||||
[0, 0, 0]])
|
||||
ydata = np.array([10, 100, 0])
|
||||
|
||||
res = least_squares(func, x0)
|
||||
'''
|
||||
'''
|
||||
print(res)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
#test1()
|
||||
#test2()
|
||||
#test3()
|
||||
#test4()
|
||||
#test5()
|
||||
#test6()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue