本文整理汇总了Python中table.Table.zeros方法的典型用法代码示例。如果您正苦于以下问题:Python Table.zeros方法的具体用法?Python Table.zeros怎么用?Python Table.zeros使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类table.Table
的用法示例。
在下文中一共展示了Table.zeros方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MultinomialDistribution
# 需要导入模块: from table import Table [as 别名]
# 或者: from table.Table import zeros [as 别名]
#.........这里部分代码省略.........
if index is None:
index = {}
assert(len(self.names) == 1 or \
len(self.names - set(index.keys())) == 1), \
"Sample only works for one variable tables"
if not index == {}:
tcpt = self.__getitem__(index)
else:
tcpt = self.cpt
# csum is the cumulative sum of the distribution
# csum[i] = numpy.sum(self.cpt[0:i])
# csum[-1] = numpy.sum(self.cpt)
csum = [numpy.sum(tcpt.flat[0:end+1]) for end in range(tcpt.shape[0])]
# sample in this distribution
r = random.random()
for i, cs in enumerate(csum):
if r < cs:
return i
return i
def random(self):
""" Returns a random state of this distribution,
chosen completely at random, it does not take account of the
underlying distribution
"""
# CHECK: legal values are 0 - nvalues-1: checked OK
return random.randint(0, self.nvalues-1)
#==================================================
#=== Learning & Sampling Functions
def initialize_counts(self):
''' initialize counts array to zeros '''
self.counts = Table(self.names_list, shape=self.shape)
self.counts.zeros()
def initialize_counts_to_ones(self):
''' initialize counts array to ones '''
self.counts = Table(self.names_list, shape=self.shape)
self.counts.ones()
def incr_counts(self, index):
""" add one to given count """
self.counts[index] += 1
def add_to_counts(self, index, counts):
""" add counts to the given count """
self.counts[index] += counts
def set_counts_to(self, index, counts):
""" set counts to a given value """
self.counts[index] = counts
def set_counts(self):
""" set the distributions underlying cpt equal to the counts """
assert(self.names_list == self.counts.names_list)
#set to copy in case we later destroy the counts or reinitialize them
self.cpt = self.counts.cpt.copy()
#=== Augmented Bayesian Network
# The augmented bayesain parameters are used to enforce the
# direction in which the CPT's evolve when learning parameters
#
# They can also be set to equivalent chances if we do not want to
# enforce a CPT, in this case this is useful because it prohibits
示例2: MultinomialDistribution
# 需要导入模块: from table import Table [as 别名]
# 或者: from table.Table import zeros [as 别名]
#.........这里部分代码省略.........
if dim == -1 or len(self.cpt.shape) == 1:
self.cpt /= self.cpt.sum()
else:
ndim = self.assocdim[dim]
order = range(len(self.names_list))
order[0] = ndim
order[ndim] = 0
tcpt = na.transpose(self.cpt, order)
t1cpt = na.sum(tcpt, axis=0)
t1cpt = na.resize(t1cpt,tcpt.shape)
tcpt = tcpt/t1cpt
self.cpt = na.transpose(tcpt, order)
def uniform(self):
""" All CPT elements have equal probability :
a = Pr(A|B,C,D)
a.uniform()
Pr(A=0)=Pr(A=1)=...=Pr(A=N)
the result is a normalized CPT
calls self.ones() and then self.normalize()
"""
self.ones()
self.normalize()
######################################################
#---TODO: Should add some initialisation functions:
# all ones, uniform, zeros
# gaussian, ...
######################################################
#======================================================
#=== Sampling
def sample(self, index={}):
""" returns the index of the sampled value
eg. a=Pr(A)=[0.5 0.3 0.0 0.2]
a.sample() --> 5/10 times will return 0
3/10 times will return 1
2/10 times will return 3
2 will never be returned
- returns an integer
- only works for one variable tables
eg. a=Pr(A,B); a.sample() --> ERROR
"""
assert(len(self.names) == 1 or len(self.names - set(index.keys())) == 1),\
"Sample only works for one variable tables"
if not index == {}:
tcpt = self.__getitem__(index)
else:
tcpt = self.cpt
# csum is the cumulative sum of the distribution
# csum[i] = na.sum(self.cpt[0:i])
# csum[-1] = na.sum(self.cpt)
csum = [na.sum(tcpt.flat[0:end+1]) for end in range(tcpt.shape[0])]
# sample in this distribution
r = random.random()
for i,cs in enumerate(csum):
if r < cs: return i
return i
def random(self):
""" Returns a random state of this distribution,
chosen completely at random, it does not take account of the
underlying distribution """
# CHECK: legal values are 0 - nvalues-1: checked OK
return random.randint(0, self.nvalues-1)
#==================================================
#=== Learning & Sampling Functions
def initializeCounts(self):
''' initialize counts array to ones '''
self.counts = Table(self.names_list, shape=self.shape)
self.counts.zeros()
def incrCounts(self, index):
""" add one to given count """
self.counts[index] += 1
def addToCounts(self, index, counts):
self.counts[index] += counts
def setCounts(self):
""" set the distributions underlying cpt equal to the counts """
assert(self.names_list == self.counts.names_list)
#set to copy in case we later destroy the counts or reinitialize them
self.cpt = self.counts.cpt.copy()
#===================================================
#=== printing functions
def __str__(self):
string = 'Multinomial ' + Distribution.__str__(self)
string += '\nConditional Probability Table (CPT) :\n'
#---TODO: should find a nice neat way to represent numarrays
# only 3 decimals are sufficient... any ideas?
string += repr(self.cpt)
return string