本文整理汇总了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)
示例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])
示例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
示例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)