简体   繁体   English

函数scipy.optimize.fmin for python

[英]Function scipy.optimize.fmin for python

I'm trying to minimize a khi-square using scipy.optimize.fmin. 我正在尝试使用scipy.optimize.fmin最小化khi-square。 Here is my function, (which calls an other simulation function spotdiffusion). 这是我的函数,(调用其他模拟函数spotdiffusion)。 The returned value (chi) is an array of two khi values (one for congruent condition, the other for incongruent condition) which I try to minimize: 返回值(chi)是两个khi值的数组(一个用于全等条件,另一个用于不一致条件)我试图最小化:

def chis (a, ter , v , sda , rd):

    ncond=1 
    ntrials = 1000 
    observed_data = np.array ([ [0.9995835, 24.0, 329.5, 357.9, 370.5, 391.5, 457.6, 0.0004164931, 0, 0],#congruent cond
                                [0.6953498, 16,   409.5, 450.5, 481,   529,   546 ,  0.3046502 ,  7 ,350]])#incongruent cond

    q_probs=np.array ([.1,.2,.2,.2,.2,.1])   
    b_probs=np.array([0.501,0.499])   

    cond = np.arange (0, ncond)
    chi = []
    for g in cond:
        if(g==0):
            fl= 1.0   #flankers congruent with target    
        if(g==1):
            fl= -1.0   # incongruent

        #########
        simTRcorrect, simTRerror, simprobc, simprobe = spotdiffusion (a ,ter ,v, sda,rd ,fl, ntrials = 1000)
        #########        
        top_data = observed_data[g,0]*q_probs  
        bot_data=observed_data[g,7]*b_probs    

        pt1 = (len (simTRcorrect [simTRcorrect < observed_data[g, 2]])) /ntrials 
        pt2 = (len (simTRcorrect [(simTRcorrect < observed_data[g, 3]) & (simTRcorrect >= observed_data[g, 2])])) /ntrials
        pt3 = (len (simTRcorrect [(simTRcorrect < observed_data[g, 4]) & (simTRcorrect >= observed_data[g, 3])])) /ntrials 
        pt4 = (len (simTRcorrect [(simTRcorrect < observed_data[g, 5]) & (simTRcorrect >= observed_data[g, 4])])) /ntrials
        pt5 = (len (simTRcorrect [(simTRcorrect < observed_data[g, 6]) & (simTRcorrect >= observed_data[g, 5])])) /ntrials
        pt6=(len (simTRcorrect [simTRcorrect > observed_data[g, 6]])) /ntrials

        pred_p= np.array ([pt1,pt2,pt3,pt4,pt5,pt6])
        top_chi_array = (np.square (top_data-pred_p))/ (pred_p+ 0.001)
        top_chi = np.sum (top_chi_array) 


        pt1 = (len (simTRerror[simTRerror < observed_data[g, 9]]))  /ntrials
        pt2 = (len (simTRerror[simTRerror >= observed_data[g, 9]])) /ntrials

        pred_p=np.array ([pt1,pt2])
        bot_chi_array = (np.square (bot_data-pred_p)) / (pred_p+ 0.001)
        bot_chi= np.sum (bot_chi_array)

        totchi=(bot_chi+top_chi)*(observed_data[g,1]+ observed_data[g,8])


        chi.append (totchi)


    chi = np.array (chi)       
    return chi

Here is the fitting procedure: 这是拟合程序:

x0 = np.array ([0.11, 0.25,0.35,1.7,0.017]) ####for initial guess 
xopt = fmin (chis(a, ter , v , sda , rd), x0, maxiter=300)

I've got an error that I don't understand: 我有一个我不明白的错误:

Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\startup.py", line 128, in runfile
    execfile(filename, glbs)
  File "C:\Users\mathieu\Desktop\modeling\spotlight diffusion model\fitting_spotlight.py", line 245, in <module>
    xopt = fmin (chis(a, ter , v , sda , rd), x0, maxiter=300)
  File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 257, in fmin
    fsim[0] = func(x0)
  File "C:\Python27\lib\site-packages\scipy\optimize\optimize.py", line 176, in function_wrapper
    return function(x, *args)
TypeError: 'numpy.float64' object is not callable

Does anyone have an idea of what's going wrong? 有没有人知道出了什么问题?

Cheers, Mat 干杯,垫子

The problem is in this line: 问题出在这一行:

xopt = fmin (chis(a, ter , v , sda , rd), x0, maxiter=300)

The expression 表达方式

chis(a, ter , v , sda , rd)

is most likely number. 很可能是数字。 It is the result of calling the function chis . 这是调用函数chis的结果。

Instead, we want to pass the function object chis to the fmin function, without having called chis first. 相反,我们想要将函数对象 chis传递给fmin函数,而不是先调用chis (If we pass chis(a, ter, v, sda, rd) then fmin just gets a number as its first argument. If we pass the function object chis itself, then fmin can call chis how ever it needs to from within the body of fmin . In Python, functions are first-class objects. (如果我们通过chis(a, ter, v, sda, rd)fmin只是得到一个数字作为第一个参数。如果我们通过函数对象chis本身,然后fmin可以调用chis它是如何以往任何时候都需要从身体内fmin 。在Python中,函数是第一类对象。

So try instead 所以试试吧

xopt = fmin (chis, x0, maxiter=300)

the problem seems to be both - in line: 问题似乎是两个 - 在线:

xopt=fmin(chis(a,ter,v,sda,rd),x0,maxiter=300)

which should be as previous user mentioned 应该像之前的用户提到的那样

xopt=fmin(chis,x0,maxiter=300)

but also in the beginning, where function has been defined, parameters should be given as array 而且在开始时,定义了函数,参数应该作为数组给出

instead of 代替

def chis (a, ter , v , sda , rd):

try this: 尝试这个:

def chis (arrays):

    a=arrays[0]
    ter=arrays[1]
    v=arrays[2]
    sda=arrays[3]
    rd=arrays[4]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM