scipy优化fmin语法

2024-06-16 14:00:33 发布

您现在位置:Python中文网/ 问答频道 /正文

numseq = ['0012000', '0112000', '0212000', '0312000', '1012000', '1112000',                                                                                   '1212000', '1312000', '2012000', '2112000', '2212000', '2312000', '3012000', '3112000',          '3212000', '3312000', '0002000', '0022000', '0032000', '1002000', '1022000', '1032000',     '2002000', '2022000', '2032000', '3002000', '3022000', '3032000', '0010000', '0011000', '0013000', '1010000', '1011000', '1013000', '2010000', '2011000', '2013000', '3010000', '3011000', '3013000', '0012100', '0012200', '0012300', '1012100', '1012200', '1012300', '2012100', '2012200', '2012300', '3012100']
prob = [-0.66474525640568083, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.78361598908750163, -0.66474525640568083, -0.66474525640568083, -0.66474525640568083, -0.66474525640568083, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.66474525640568083, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.66474525640568083, -0.66474525640568083, -0.66474525640568083, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.66474525640568083, -0.66474525640568083, -0.66474525640568083, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.66474525640568083, -0.66474525640568083, -0.66474525640568083, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212, -0.49518440694747212]

numseqprob是每个长度为50的列表。它们是收集到的实验数据。numseq对应于X轴值,prob对应于Y轴值。在

我想最小化的功能是:

^{pr2}$

所以:

  • allparams是一个4×7矩阵,它包含了所有要优化的参数。在
  • xdata是X轴的值,即numseq
  • ydata只是一个数字列表,即prob

chi2是实验值和模型值之间的平方差。这是必须最小化的。在

参数的初始猜测如下:

x0 = [[-0.6, -0.6, -0.6, -0.6, -0.6, -0.6, -0.6], [-0.6, -0.6, -0.6, -0.6, -0.6, -0.6, -0.6], [-0.6, -0.6, -0.6, -0.6, -0.6, -0.6, -0.6], [-0.6, -0.6, -0.6, -0.6, -0.6, -0.6, -0.6]]

现在我如何调用这个函数的fmin?我试过了

fmin(residue, x0, args=(numseq, prob))

但我一直收到一个错误:

Traceback (most recent call last):
  File "<pyshell#362>", line 1, in <module>
    fmin(residue, x0, args=(numseq, prob))
  File "C:\Python31\lib\site-packages\scipy\optimize\optimize.py", line 258, in fmin
    fsim[0] = func(x0)
  File "C:\Python31\lib\site-packages\scipy\optimize\optimize.py", line 177, in function_wrapper
    return function(x, *args)
  File "<pyshell#361>", line 7, in residue
    y = y-allparams[int(x[j])][j]
IndexError: invalid index to scalar variable.

为什么会这样?是因为fmin不能接受2D数组作为初始猜测?那么我是否必须更改整个代码来处理一维参数数组?在

即使您不能解释这个问题,您能至少告诉我fmin模块是如何工作的吗?i、 如何实现优化N维数组的fmin的语法?你能解释一下args()是什么吗?我是一个新的优化,我不知道如何实现它:(


Tags: in列表参数lineargs数组fileoptimize
1条回答
网友
1楼 · 发布于 2024-06-16 14:00:33

“fmin”例程可以接受2d数组作为初始猜测。但是它做的第一件事是将这个数组展平[(4,7)>;(28)]。所以,你的余数函数以一个(4,7)数组作为输入,“fmin”例程给它一个长度为28的平坦“x0”。这就是您看到错误的原因:
y = y-allparams[int(x[j])][j]
IndexError: invalid index to scalar variable.

See the source code here.

所以看来你必须改变你的留数函数来接受向量而不是数组。不过,这似乎不算太糟。我尝试了以下似乎有效的方法(注:请仔细检查!)

def residue_alternative(allparams, inshape, xdata, ydata):
    m, n = inshape
    chi2 = 0.0
    for i in range(0,len(xdata)):
        x = xdata[i]
        y = 0
        for j in range(len(x)):
            idx = int(x[j]) * n +  j #Double check this to 
            y = y-allparams[idx]     #make sure it does what you want
            chi2 = chi2 + (ydata[i]-y)**2
    return chi2

我用:

^{pr2}$

并得到以下结果:

Optimization terminated successfully.
         Current function value: 7.750523
         Iterations: 21570
         Function evaluations: 26076

>>>xopt
array([ 0.57669042, -0.21965861,  0.2635061 , -0.08284016, -0.0779489 ,
   -0.10358114,  0.14041582,  0.72469391, -0.43190214,  0.31269757,
   -0.0338726 , -0.14919739, -2.58314651,  2.74251214,  0.57695759,
   -0.49574628,  0.1490926 ,  0.04912353,  0.02420988,  1.17924051,
   -7.2147027 ,  0.57860843, -0.28386938,  0.2431877 , -0.22674694,
   -0.58308225, -6.05706775, -2.06350063])    

您可以将其重塑为4x7阵列。试试这个,告诉我它是否有用。在

相关问题 更多 >