2014-12-06 111 views
0

我对这段代码有一些麻烦。我正在运行这段代码的一个常量为b的值,以及循环中常量为a的两个值。当我运行没有最后一行t_c.append([au.link_q(f) for f in F2])的代码时,它可以很好地工作。当我介绍最后一行时,代码运行时应该为a的第一个值,但代码第二次运行第二个值a时出现错误。有些东西告诉我,错误在最后一行。Python:当我的循环第二次运行时发生错误

这是当程序尝试在循环中第二次运行时,它给了我什么。

File "C:/Users/Samir Kozarcanin/Documents/Min kode python/EuropeanGridR/prove_ind_til_videre.py", line 87, in <module> 
    foobar = np.array(ntimesn(xgauss)) 

    File "C:/Users/Samir Kozarcanin/Documents/Min kode python/EuropeanGridR/prove_ind_til_videre.py", line 57, in ntimesn 
    res[u][v] = f(u, v, m)          # 

TypeError: 'numpy.ndarray' object is not callable 

但它奇怪的是,它运行罚款的a的第一个值而不是下一个。 `

def get_q(s,q=0.99): 
""" Looks at a cumulative histogram of a time series.It 
    returns the magnitude of s that happens a fraction 'quant' of the time. 
""" 
return mquant(s,q)[0] 

def link_q(f,q=0.99): 
    a = (1-q)/2.0 
    b = q+a 
    return max(-mquant(f,a)[0],mquant(f,b)[0]) 

def nodes_B(N = None,B=1,alpha=0.8): 
    if N == None: 
     N = EU_Nodes() 
    LEU = np.sum(n.mean for n in N) 
    CFW = np.sum(n.mean*n.cf_w**B for n in N) 
    CFS = np.sum(n.mean*n.cf_s**B for n in N) 
    for n in N: 
     n.gw = n.cf_w**B *LEU/CFW 
     n.gs = n.cf_s**B *LEU/CFS 
     n.gamma = (alpha*n.gw+(1-alpha)*n.gs)*1.0 
     n.alpha = alpha 
     n._update_() 
    return N 

def f(s,p, matrix): 
    res = 0 
    for x in xrange(len(matrix[0])):          
     res += matrix[s][x] * matrix[p][x]        
    res = res/len(matrix[0]) 
    return res 

def ntimesn(m):   
    res = [[0 for x in range(len(m))] for x in range(len(m))] 
    for u in xrange(len(m)): 
     for v in xrange(u, len(m)): 
      res[u][v] = f(u, v, m) 
      res[v][u] = res[u][v] 
    return res 

alphas=[0, 0.05] 
betas = [0] 
d=1 
ii=range(30) 
jj=range(30) 
kk=range(30) 
nul = [0] * 30 

for b in betas: 

    for a in alphas: 
     xgauss=[] 
     N=nodes_B(B=b, alpha=a) 

     for index in ii: 
      hist, x = np.histogram(N[index].mismatch[:10], 70128, normed=1) 
      hist2 = hist*np.diff(x) 
      cumulative=np.cumsum(hist2) # laver cumulative værdier 
      f1 = InterpolatedUnivariateSpline(x[:-1], cumulative, k=3) 
      Fdelta=f1(N[index].mismatch) 
      xax=np.arange(-4,4,0.1) 
      Fgauss=(1+erf(xax/(math.sqrt(2))))/2 
      g1 = InterpolatedUnivariateSpline(Fgauss, xax, k=3) 
      xgauss.append(g1(Fdelta)) 
     foobar = np.array(ntimesn(xgauss)) 

     for i in ii: 
      for j in jj: 
       if i!=j: 
        foobar[i,j] = foobar[i,j]*d  

     xgx = InterpolatedUnivariateSpline(xax,Fgauss, k=3) 
     nul = [0] * 30 
     randxgauss=[] 
     for i in xrange(70128): 
      newvalues=(numpy.random.multivariate_normal(nul, foobar)) 
      randxgauss.append(xgx(newvalues)) 

     randxgauss = np.array(randxgauss) 


     delta=[] 
     kk=range(30) 
     for k in kk: 
      hist, x = np.histogram(N[k].mismatch, 70128, normed=1) 
      hist2 = hist*np.diff(x) 
      cumulative=np.cumsum(hist2) 
      delta.append(interp(randxgauss[:,k], cumulative, x[:-1])) 

     t_c=[] 
     for n in N: 
      n.mismatch = delta[n.id][:] 
     N2,F2 = au.solve(N,mode="copper linear verbose",lapse=100) 

     t_c.append([au.link_q(f) for f in F2])` 

回答

1

看来你已经覆盖了t_c.append。在代码中的某些时候,你可能已经写

t_c.append = np.ndarray(["example ", "array"]) 

,然后尝试拨打uncallable np.ndarray()t_c.append([...])。要解决此问题,请改用t_c.append(np.ndarray([...]), ...)

相关问题