python - Scipy.optimize.leastsq returns the initial guess not optimization parameters -


i trying use leastsq scipy.optimize module find best fit line, there 3 unknown parameters. have written out code program runs , returns initial guess optimization parameters (essentially leastsq function nothing in program).

here simplified code, data using.

import numpy np import matplotlib.pyplot plt scipy import optimize #leastsq levenberg-marquadt algorithm  = [6.011737374832778931e+10, 1.253141174941418152e+11, 1.297179270983954620e+11, 1.577611269699349976e+11, 2.238721138568337708e+11, 4.315190768277650146e+11, 5.407543229455815430e+11, 5.382697825162881470e+11, 5.308844442309879150e+11, 4.528975799036213379e+11, 2.890679882365477905e+11, 2.798981319634357300e+11, 2.798981319634357300e+11] b = [1.228900000000000006e+02, 1.465500000000000114e+02, 1.761399999999999864e+02, 2.057199999999999989e+02, 2.353100000000000023e+02,2.648999999999999773e+02, 2.945000000000000000e+02, 3.315000000000000000e+02, 3.758999999999999773e+02, 4.203199999999999932e+02, 4.647400000000000091e+02, 5.091700000000000159e+02, 5.980399999999999636e+02]  a_arr = np.asarray(a) #electron density b_arr = np.asarray(b) #altitude  #function p list of variables go fitting function , x independent variable  def fitfunc (p,x):     return p[0] * np.exp((0.5 * (1- (x - p[1])/p[2])) - np.exp(-(x - p[1])/p[2])) def errfunc (p,x,y):     err =  y - fitfunc(p,x)     return err  #fitfunc = lambda p, x: p[0] * np.exp((0.5 * (1- (x - p[1])/p[2])) - np.exp(-(x - p[1])/p[2])) #errfunc = lambda p, x, y: fitfunc(p,x)-y p0 = [10**11.7,300.,50.] #p = np.asarray(p0)  p1, success = optimize.leastsq(errfunc, p0[:], args=(a_arr, b_arr))     b_arr2 = np.linspace(b_arr.min(), b_arr.max(), 80)  print("estimated parameters: ", p1) print("observed parameters: ", p0)      #with optimization plt.plot(a_arr, b_arr,"o--", fitfunc(p1, b_arr2 ), b_arr2, '-') plt.legend(['data', 'optimization'], loc='best') plt.title('plot') plt.xlabel('x') plt.ylabel('y') plt.show() 

everytime ('estimated parameters: ', array([ 5.01187234e+11, 3.00000000e+02, 5.00000000e+01])) ('observed parameters: ', [501187233627.2715, 300.0, 50.0]) same thing.

i have tried use lambda functions, changing around data types.

is errfunc (cost/objective function) incorrect?

thanks in advance!

if understand code correctly, think you're passing arguments in wrong order errfunc.

instead of:

p1, success = optimize.leastsq(errfunc, p0[:], args=(a_arr, b_arr)) 

it should be

p1, success = optimize.leastsq(errfunc, p0[:], args=(b_arr, a_arr)) 

your errfunc wants b array first , a array second.

when make above change, observed parameters as:

[7.43278755e+11,   2.77592278e+02,   8.88750029e+01] 

and plot looks better well.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -