我写了一个脚本,其目的是通过反复试验找到最适合函数的参数(马尔可夫链蒙特卡罗)。参数的可行性由chi值判断 - chi越低越好。
以下是我目前编写的代码部分:
chi1 = fun_chi(B1,G1,C11,C21,C31,C41,C01)
BGCchilist = []
count = -1
for i in range(iteration_MCMC):
count = count + 1
print(count)
B2,G2,C12,C22,C32,C42,C02 = gen_param(B1,G1,C11,C21,C31,C41,C01)
chi2 = fun_chi(B2,G2,C12,C22,C32,C42,C02)
ratio = np.exp((-chi2 + chi1) / 2)
rand = np.random.uniform(0,1)
if rand < ratio:
B1 = B2
G1 = G2
C11 = C12
C21 = C22
C31 = C32
C41 = C42
C01 = C02
chi1 = chi2
##save the data
Bsave = B1
Gsave = G1
C1save = C11
C2save = C21
C3save = C31
C4save = C41
C0save = C01
chisave = chi1
BGCchilist.append((Bsave,Gsave,C1save,C2save,C3save,C4save,C0save,chisave))
Blist = [x[0] for x in BGCchilist]
Glist = [x[1] for x in BGCchilist]
C1list = [x[2] for x in BGCchilist]
C2list = [x[3] for x in BGCchilist]
C3list = [x[4] for x in BGCchilist]
C4list = [x[5] for x in BGCchilist]
C0list = [x[6] for x in BGCchilist]
chilist = [x[7] for x in BGCchilist]
minchi = min(x[7] for x in BGCchilist)
mintuple = [x for x in BGCchilist if x[7] == minchi]
我最终通过Matplotlib单独绘制所有这些列表与迭代的关系,然后将mintuple
保存为文本文件。
一位同事看了一眼并告诉我,也许我会更好地使用字典,每次迭代我做a_dictionary[chisave] = (B,G,...C0)
之类的事情,然后通过查找mintuple
找到{{1}}字典中的最小值。但是,似乎从字典中的值元组中提取列表以进行绘图会更加笨拙并采取更多步骤。
字典方法是否允许更少的步骤,更高的效率,或者所有列表的方法是否正常?
感谢您的帮助。
答案 0 :(得分:2)
这是在评论中作出回答。我认为关于效率的问题,但经过仔细研究后,它看起来就像是如何清理代码。如果这是授予,那么以下可能是有用的:
对于第一部分,我们只是将所有内容都保留为元组,并像这样使用inlace扩展......
par1 = B1,G1,C11,C21,C31,C41,C01
chi1 = fun_chi(*par1)
BGCchilist = []
for i in range(iteration_MCMC):
print (i-1)
par2 = gen_param(*par1)
chi2 = fun_chi(*par2)
ratio = np.exp((-chi2 + chi1) / 2)
rand = np.random.uniform(0,1)
if rand < ratio: par1 = par2
BGCchilist.append(list(par1) + [chi1]) ##save the data
这是一个逐字逻辑的副本。但是,我没有看到chi1在for
循环中更新。我猜你也希望这种情况发生?像这样:
par1 = B1,G1,C11,C21,C31,C41,C01
BGCchilist = []
for i in range(iteration_MCMC):
print (i-1)
chi1 = fun_chi(*par1) # This is the change
par2 = gen_param(*par1)
chi2 = fun_chi(*par2)
if np.random.uniform(0,1) < np.exp((-chi2 + chi1) / 2): par1 = par2
BGCchilist.append(list(par1) + [chi1]) ##save the data
我不确定算法,因此无法对该部分进行明确评论。
下一部分更有趣。
Blist = [x[0] for x in BGCchilist]
Glist = [x[1] for x in BGCchilist]
C1list = [x[2] for x in BGCchilist]
C2list = [x[3] for x in BGCchilist]
C3list = [x[4] for x in BGCchilist]
C0list = [x[6] for x in BGCchilist]
C4list = [x[5] for x in BGCchilist]
chilist = [x[7] for x in BGCchilist]
可以简单地替换为:
BList, GList, \
C1List, C2List, C3List \
C0List, C4List, chilist = zip(*BGCchilist)
我认为这就是它的作用。请单独检查此部分。
最后,你真的需要所有的名单吗?或者你只需要chiList
?
我想其余的应该很容易吗?你拥有的是非常好的。检查key
功能中的min
参数。因此,如果您出于某种原因不想要所有列表,您可以简单地跳过该部分并在for循环中找到最小值,或者您可以这样做:
minChi = min( zip(*BGCchilist)[-1] )
allMinChis = filter( lambda m: m[7] == minChi, BGCchilist)
Cheeers!
N.B。我没有测试代码,所以可能会有错误。请仔细检查代码并确保这是您想要的。
答案 1 :(得分:1)
dict
步数更少,也更容易阅读。使用minchi
作为密钥:
BGCchilist = {}
# in the loop
BGCchilist[chisave] = [Bsave, Gsave, C1save,C2save,C3save,C4save,C0save,chisave]
# out of the loop
minchi = min(BGCchilist.keys())
mintuple = BGCchilist[minchi]
如果您需要保存导致相同chi值的每个参数组合,请更改dict以保存每个键的元组列表,而不仅仅是列表:
from collections import defaultdict
BGCchilist = defaultdict(list)
...
BGCchilist[chisave].append([...all the saves...])
...
minchi = min(BGCchilist.keys())
mintuples = BGCchilist[minchi]
# at this point mintuples has at least one tuple of params that got
# this chi score, maybe more
更新
好的,所以这是我对你的要求的理解:
我会为此使用一个简单的类。它可能没有更少的行,但它更具可读性:
# lightly tested
class BGCChiData(object): # (object) not needed in Python3
def __init__(self):
self.data = defaultdict(list)
def __getitem__(self, chi):
return self.data[chi]
def __setitem__(self, chi, params):
self.data[chi].append(params)
@property
def min_chi(self):
return min(self.data.keys())
@property
def B(self):
return [tup[0] for val in self.data.values() for tup in val]
@property
def G(self):
return [tup[1] for val in self.data.values() for tup in val]
@property
def C1(self):
return [tup[2] for val in self.data.values() for tup in val]
@property
def C2(self):
return [tup[3] for val in self.data.values() for tup in val]
@property
def C3(self):
return [tup[4] for val in self.data.values() for tup in val]
@property
def C4(self):
return [tup[5] for val in self.data.values() for tup in val]
@property
def C0(self):
return [tup[6] for val in self.data.values() for tup in val]
@property
def chi(self):
return [tup[7] for val in self.data.values() for tup in val]
chi1 = fun_chi(B1,G1,C11,C21,C31,C41,C01)
BGCchi = BGCChiData()
for count in range(iteration_MCMC):
print(count)
B2,G2,C12,C22,C32,C42,C02 = gen_param(B1,G1,C11,C21,C31,C41,C01)
chi2 = fun_chi(B2,G2,C12,C22,C32,C42,C02)
ratio = np.exp((-chi2 + chi1) / 2)
rand = np.random.uniform(0,1)
if rand < ratio:
B1 = B2
G1 = G2
C11 = C12
C21 = C22
C31 = C32
C41 = C42
C01 = C02
chi1 = chi2
##save the data
BGCchi[chi1].append(B1, G1, C11, C21, C31, C41, C01)
minchi = BGCchi.min_chi
mintuple = BGCchi[minchi]
B1list = BGCchi.B
# etc. etc.