本文整理汇总了Python中table.Table.normalize方法的典型用法代码示例。如果您正苦于以下问题:Python Table.normalize方法的具体用法?Python Table.normalize怎么用?Python Table.normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类table.Table
的用法示例。
在下文中一共展示了Table.normalize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sampleGivenMB
# 需要导入模块: from table import Table [as 别名]
# 或者: from table.Table import normalize [as 别名]
def sampleGivenMB(self, v, state):
MBval = Table(v.name, shape=v.nvalues)
children = v.out_v
index = {}
for vert in v.family: # replaced [v]+list(v.in_v)
index[vert.name] = state[vert.name]
# index = {var.name:state} for var in v.family
childrenAndIndex = []
for child in children:
cindex = {}
# family = a node and all its parents
for cvert in child.family: # replaced [child]+list(child.in_v)
# cvert is either a child or an uncle(parent of child) of v
# cindex contains the state of all variables in the family
# of a child of v
cindex[cvert.name] = state[cvert.name]
childrenAndIndex.append((child,cindex))
#OPTIMIZE: could vectorize this code
for value in range(v.nvalues):
index[v.name] = value
# initialise each element of the distribution with the
# conditional probability table values of the variable
# Pr(v=i)=Pr(v=i|Pa(v)=index)
# index is randomly selected at each iteration
MBval[value] = v.distribution[index]
##################################################
# this could be replaced by Table multiplication instead
# of an element-wise multiplication
# in that case we don't need all those index dictionnaries
##################################################
for child,cindex in childrenAndIndex:
cindex[v.name] = value
MBval[value] *= child.distribution[cindex]
MBval.normalize()
#######################################
# added a sample() function in Distribution
#######################################
return MBval.sample()
示例2: Marginalise
# 需要导入模块: from table import Table [as 别名]
# 或者: from table.Table import normalize [as 别名]
def Marginalise(self, v, N):
""" Compute the Pr(v) where v is a variable name,
N is the number of iterations of MCMC to perform.
"""
# the return distribution
###########################################################
#vDist = RawCPT(v, (self.BNet.v[v].nvalues,1))
# vDist should be a potential instance. What do you think?
###########################################################
vDist = Table(v, shape=self.BNet.v[v].nvalues)
nonEvidence = []
# find out which values are not specified in evidence
for vv in self.BNet.v.values():
if not self.evidence.has_key(vv.name): nonEvidence.append(vv.name)
# state is first selected at random
state = copy.copy(self.evidence)
for vname in nonEvidence:
# CHECK: legal values are 0 - nvalues-1: checked OK
state[vname] = random.randint(0, self.BNet.v[vname].nvalues-1)
for i in range(N):
if i > self.cut:
#########################################
# What is this line for ????
# shouldn't we stop iteration here?
# what is cut for?
##################################
vDist[state[v]] += 1
for vname in nonEvidence:
state[vname] = self.sampleGivenMB(self.BNet.v[vname], state)
################################################
# added this line: did you forget it, or i didn't understand
# anything in your code
#################################################
vDist[state[v]] += 1
# added a normalize() function in Table
vDist.normalize()
return vDist