0

scipy.optimize.fmin を使用して khi-square を最小化しようとしています。これが私の関数です (他のシミュレーション関数 spotdiffusion を呼び出します)。返される値 (chi) は、最小化しようとする 2 つの khi 値 (1 つは合同条件用、もう 1 つは不一致条件用) の配列です。

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

フィッティング手順は次のとおりです。

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)

理解できないエラーが発生しました:

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

誰が何がうまくいかないのか考えていますか?

乾杯、マット

4

2 に答える 2

2

問題は次の行にあります。

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

表現

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

可能性が高い数字です。関数を呼び出した結果ですchis

代わりに、最初に呼び出さずに、関数オブジェクト chisを関数に渡したいと考えています。(渡すと、最初の引数として数値を取得するだけです。関数オブジェクト自体を渡すと、の本体内から必要なだけ呼び出すことができます。Python では、関数はファーストクラス オブジェクトです。fminchischis(a, ter, v, sda, rd)fminchisfminchisfmin

代わりに試してみてください

xopt = fmin (chis, x0, maxiter=300)
于 2012-04-28T19:32:33.677 に答える
0

問題は両方のようです-インライン:

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

これは、前のユーザーが述べたとおりです

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

ただし、関数が定義されている最初の部分でも、パラメーターは配列として指定する必要があります

それ以外の

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

これを試して:

def chis (arrays):

    a=arrays[0]
    ter=arrays[1]
    v=arrays[2]
    sda=arrays[3]
    rd=arrays[4]
于 2012-05-04T22:50:11.437 に答える