Python:我的循环第二次运行时发生错误

时间:2014-12-06 14:47:09

标签: python loops time

我对这段代码有些麻烦。我正在为一个常量b的值和循环中的常量a的两个值运行这段代码。当我在没有最后一行t_c.append([au.link_q(f) for f in F2])的情况下运行代码时,它可以完美运行。当我引入最后一行时,代码正在运行,因为它应该是a的第一个值,但是当代码第二次运行a时出现问题}。有些东西告诉我,错误然后放在最后一行。

这是程序在循环中第二次尝试运行时给出的内容。

  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 个答案:

答案 0 :(得分:1)

好像你已经覆盖了t_c.append。在您的代码中的某个时刻,您可能已经编写了

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

然后它尝试在np.ndarray()调用不可调用的t_c.append([...])。要解决此问题,请改用t_c.append(np.ndarray([...]), ...)