在Scipy 1.0.0中使用leastsq和least_square(with lm)的结果不同?

时间:2018-01-08 21:07:44

标签: python-3.x scipy

我想知道是否预期Scipy的1.0.0 leastsq和least_square(with lm)优化器会产生不同的结果。以下代码为我演示了这个问题。 (它符合Steinhart-Hart参数。)

#!/usr/bin/env python3
import numpy as np
from scipy import optimize

#r = np.array([10030.217595+100, 3065.807003+30, 207.740077+2, 115.510462+1])
r = np.array([10030.217595, 3065.807003, 207.740077, 115.510462])
t = np.array([273.15, 298.17, 373.17, 394.29])

def steinhart(r, a, b, c):
    "NTC : steinhart(x, a, b, c)"
    return 1.0/( a + b*np.log(r) + c*(np.log(r))**3 )

def residual(p, r, t):
    return t - steinhart(r, *p)

p0 = [1e-1, 1e-2, 1e-4]

# This works (uses lm)
popt, xx = optimize.leastsq(residual, p0, args=(r, t))
print("leastsq:        "+str(popt))

# Doesn't work, but should be the same as above
# Now it works!!! ???
popt2 = optimize.least_squares(residual, p0, args=(r, t), method='lm')
print("least_squares:  "+str(popt2))

# Original parameter to generate the data, for comparison
p_org = [1.4e-3, 2.37e-4, 9.9e-8]
print("Correct answer: "+str(p_org))

运行此时,leastsq使其正确,而least_squares不会收敛。我得到message: 'The maximum number of function evaluations is exceeded.'

我做错了吗?该测试适用于较旧的Scipy 0.17.0或0.19.1。

P.S。:我使用p_org参数和拟合函数生成的拟合值可以得到完美的拟合。如果我通过稍微改变一些值来添加一些噪声(只需使用注释行5来定义r),则least_squares会收敛。不过,这应该不会发生,还是应该发生?

0 个答案:

没有答案