【发布时间】:2014-05-29 16:15:23
【问题描述】:
我正在尝试使用 scipy.optimize 模块中的 leastsq 来找到一个最佳拟合线,其中有 3 个未知参数。我已经写出了代码,但是程序运行并返回初始猜测作为优化参数(本质上,leastsq 函数在我的程序中什么都不做)。
这是简化的代码,以及我正在使用的数据。
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize #Leastsq Levenberg-Marquadt Algorithm
a = [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 where p is the list of variables that go into the fitting function and x is the 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()
每次我得到 ('估计参数:', array([ 5.01187234e+11, 3.00000000e+02, 5.00000000e+01])) ('观察到的参数:', [501187233627.2715, 300.0, 50.0]) 这是同一件事。
我尝试过使用 lambda 函数,改变数据类型。
是不是我的 errfunc(成本/目标函数)不正确?
提前致谢!
【问题讨论】:
标签: python optimization scipy least-squares