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])`