Python numpy.random.dirichlet() Examples
The following are 4
code examples of numpy.random.dirichlet().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy.random
, or try the search function
.
Example #1
Source File: test_metrics.py From python-dlpy with Apache License 2.0 | 6 votes |
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)
Example #2
Source File: discrete.py From bhmm with GNU Lesser General Public License v3.0 | 5 votes |
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])
Example #3
Source File: drawbkps.py From ruptures with BSD 2-Clause "Simplified" License | 5 votes |
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
Example #4
Source File: mfm.py From yass with Apache License 2.0 | 5 votes |
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)