当前位置: 首页>>代码示例>>Python>>正文


Python Table.zeros方法代码示例

本文整理汇总了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 
开发者ID:BackupTheBerlios,项目名称:pybayes-svn,代码行数:70,代码来源:distributions.py

示例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
开发者ID:BackupTheBerlios,项目名称:pybayes-svn,代码行数:104,代码来源:distributions.py


注:本文中的table.Table.zeros方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。