所以我试图使用scipy.optimize中的leastsq函数来解决最小化问题。 我一直收到以下错误,无法理解原因:
ValueError: object too deep for desired array
Traceback (most recent call last):
File "lesatsq_viscoelast.py", line 161, in <module>
viscoelas_parameters()
File "devoir04.py", line 117, in exercice02
plsq = leastsq( residuals, betas, args = ( deformation_dagger, time ) )
File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 283, in leastsq
gtol, maxfev, epsfcn, factor, diag)
minpack.error: Result from function call is not a proper array of floats.
我认为,在网上研究之后,问题是我没有将数组作为初始值给予最小值但是它没有解决我的问题......
这是我的简化代码:
from numpy import *
from scipy.optimize import leastsq
time = []
deformation = []
time, deformation = import_data_04_02( "data.csv" )
time = asarray( time )
deformation = asarray( deformation )
#initial vector
betas = []
for i in range(0, 12):
betas.append( 0. )
betas = asarray(betas)
plsq = leastsq( residuals, betas, args = ( deformation, time ) )
def residuals( x, deformation, time ):
err = []
for i in range(0, len(time)):
err.append( deformation - deformation_theory( time[i], x ) )
return asarray( err )
#The function that should fit the set of data I have, with x a vector I am trying to determine
def deformation_theory( time, x ):
lambdas = []
for i in range(0, 50, 5):
lambdas.append( 1./pow(10, float(i)/10.) )
stress = 20.
for i in range(0, len(lambdas)):
if i == 0:
f = x[0]
else:
f = f + x[i] * (1. - exp( -lambdas[i-1]* time ) )
f = f * stress
return f
我的目标是找到betas[i]
(在理论函数中称为x
)值的最佳集合,使用我从实验中测量的一组数据(变形到时间),以便残差为最小,这意味着我拥有最佳的beta值。