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


Python random.dirichlet方法代码示例

本文整理汇总了Python中numpy.random.dirichlet方法的典型用法代码示例。如果您正苦于以下问题:Python random.dirichlet方法的具体用法?Python random.dirichlet怎么用?Python random.dirichlet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在numpy.random的用法示例。


在下文中一共展示了random.dirichlet方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _create_classification_table

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import dirichlet [as 别名]
def _create_classification_table(nclass, nrow, id_vars=None, alpha=None, seed=1234,
                                 true_label='target', pred_label='p_target'):

    if alpha is None:
        alpha = [1]*nclass

    nr.seed(seed)
    prob_matrix = nr.dirichlet(alpha, size=nrow)
    target = _random_weighted_select(prob_matrix).reshape(-1, 1)
    p_target = prob_matrix.argmax(axis=1).reshape(-1, 1)
    classification_matrix = np.hstack((prob_matrix, target, p_target))
    colnames = ['p_' + str(i) for i in range(nclass)] + [true_label, pred_label]

    if id_vars is not None:
        if not isinstance(id_vars, list):
            id_vars = [id_vars]
        ncol = len(id_vars)
        id_matrix = _create_id_matrix(nrow, ncol)
        classification_matrix = np.hstack((classification_matrix, id_matrix))
        colnames = colnames + id_vars


    return pd.DataFrame(classification_matrix, columns=colnames) 
开发者ID:sassoftware,项目名称:python-dlpy,代码行数:25,代码来源:test_metrics.py

示例2: sample

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import dirichlet [as 别名]
def sample(self, observations_by_state):
        """
        Sample a new set of distribution parameters given a sample of observations from the given state.

        The internal parameters are updated.

        Parameters
        ----------
        observations :  [ numpy.array with shape (N_k,) ] with nstates elements
            observations[k] are all observations associated with hidden state k

        Examples
        --------

        initialize output model

        >>> B = np.array([[0.5, 0.5], [0.1, 0.9]])
        >>> output_model = DiscreteOutputModel(B)

        sample given observation

        >>> obs = [[0, 0, 0, 1, 1, 1], [1, 1, 1, 1, 1, 1]]
        >>> output_model.sample(obs)

        """
        from numpy.random import dirichlet
        N, M = self._output_probabilities.shape  # nstates, nsymbols
        for i, obs_by_state in enumerate(observations_by_state):
            # count symbols found in data
            count = np.bincount(obs_by_state, minlength=M).astype(float)
            # sample dirichlet distribution
            count += self.prior[i]
            positive = count > 0
            # if counts at all: can't sample, so leave output probabilities as they are.
            self._output_probabilities[i, positive] = dirichlet(count[positive]) 
开发者ID:bhmm,项目名称:bhmm,代码行数:37,代码来源:discrete.py

示例3: draw_bkps

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import dirichlet [as 别名]
def draw_bkps(n_samples=100, n_bkps=3):
    """Draw a random partition with specified number of samples and specified number of changes."""
    alpha = np.ones(n_bkps + 1) / (n_bkps + 1) * 2000
    bkps = np.cumsum(dirichlet(alpha) * n_samples).astype(int).tolist()
    bkps[-1] = n_samples
    return bkps 
开发者ID:deepcharles,项目名称:ruptures,代码行数:8,代码来源:drawbkps.py

示例4: update_local

# 需要导入模块: from numpy import random [as 别名]
# 或者: from numpy.random import dirichlet [as 别名]
def update_local(self, maskedData):
        """
            Updates the local parameter rhat for VB inference

            Parameters:
            -----------

            maskedData: maskData object
        """

        pik = dirichlet(self.ahat.ravel())
        Khat = self.ahat.size
        Ngroup, nfeatures, nchannel = maskedData.meanY.shape



        const1 = -nfeatures / 2 * np.log(2 * np.pi)
        prec = self.Vhat.transpose([2, 3, 0, 1]) * self.nuhat[:, np.newaxis, np.newaxis, np.newaxis]
        xmu = (maskedData.meanY[:, :, np.newaxis] - self.muhat).transpose([0, 2, 3, 1])
        maha = -np.squeeze(np.matmul(xmu[:, :, :, np.newaxis], np.matmul(prec, xmu[..., np.newaxis])), axis=(3, 4))/2.0
        const2 = np.linalg.slogdet(prec)[1] / 2.0
        log_rho = np.sum(maha + const1 + const2, axis=-1)
        log_rho += np.log(pik)
        log_rho = log_rho - np.max(log_rho, axis=1)[:, np.newaxis]
        rho = np.exp(log_rho)
        self.rhat = rho / np.sum(rho, axis=1, keepdims=True) 
开发者ID:paninski-lab,项目名称:yass,代码行数:28,代码来源:mfm.py


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