def score(self, X, lengths=None): """Compute the log probability under the model. Parameters ---------- X : array-like, shape (n_samples, n_features) Feature matrix of individual samples. lengths : array-like of integers, shape (n_sequences, ), optional Lengths of the individual sequences in ``X``. The sum of these should be ``n_samples``. Returns ------- logprob : float Log likelihood of ``X``. See Also -------- score_samples : Compute the log probability under the model and posteriors. decode : Find most likely state sequence corresponding to ``X``. """ _utils.check_is_fitted(self, "startprob_") self._check() X = check_array(X) # XXX we can unroll forward pass for speed and memory efficiency. logprob = 0 for i, j in iter_from_X_lengths(X, lengths): framelogprob = self._compute_log_likelihood(X[i:j]) logprobij, _fwdlattice = self._do_forward_pass(framelogprob) logprob += logprobij return logprob
def get_stationary_distribution(self): """Compute the stationary distribution of states. """ # The stationary distribution is proportional to the left-eigenvector # associated with the largest eigenvalue (i.e., 1) of the transition # matrix. _utils.check_is_fitted(self, "transmat_") eigvals, eigvecs = np.linalg.eig(self.transmat_.T) eigvec = np.real_if_close(eigvecs[:, np.argmax(eigvals)]) return eigvec / eigvec.sum()
def decode(self, X, lengths=None, algorithm=None): """Find most likely state sequence corresponding to ``X``. Parameters ---------- X : array-like, shape (n_samples, n_features) Feature matrix of individual samples. lengths : array-like of integers, shape (n_sequences, ), optional Lengths of the individual sequences in ``X``. The sum of these should be ``n_samples``. algorithm : string Decoder algorithm. Must be one of "viterbi" or "map". If not given, :attr:`decoder` is used. Returns ------- logprob : float Log probability of the produced state sequence. state_sequence : array, shape (n_samples, ) Labels for each sample from ``X`` obtained via a given decoder ``algorithm``. See Also -------- score_samples : Compute the log probability under the model and posteriors. score : Compute the log probability under the model. """ _utils.check_is_fitted(self, "startprob_") self._check() algorithm = algorithm or self.algorithm if algorithm not in DECODER_ALGORITHMS: raise ValueError("Unknown decoder {!r}".format(algorithm)) decoder = { "viterbi": self._decode_viterbi, "map": self._decode_map }[algorithm] X = check_array(X) n_samples = X.shape[0] logprob = 0 state_sequence = np.empty(n_samples, dtype=int) for i, j in iter_from_X_lengths(X, lengths): # XXX decoder works on a single sample at a time! logprobij, state_sequenceij = decoder(X[i:j]) logprob += logprobij state_sequence[i:j] = state_sequenceij return logprob, state_sequence
def score(self, X, run_lengths, lengths=None): check_is_fitted(self, "startprob_") self._check() X = check_array(X) # XXX we can unroll forward pass for speed and memory efficiency. logprob = 0 for i, j in iter_from_X_lengths(X, lengths): framelogprob = self._compute_log_likelihood(X[i:j]) logprobij, _fwdlattice = self._do_forward_pass( framelogprob, run_lengths[i:j]) logprob += logprobij return logprob
def sample(self, n_samples=1, random_state=None): """Generate random samples from the model. Parameters ---------- n_samples : int Number of samples to generate. random_state : RandomState or an int seed A random number generator instance. If ``None``, the object's ``random_state`` is used. Returns ------- X : array, shape (n_samples, n_features) Feature matrix. state_sequence : array, shape (n_samples, ) State sequence produced by the model. """ _utils.check_is_fitted(self, "startprob_") self._check() if random_state is None: random_state = self.random_state random_state = check_random_state(random_state) startprob_cdf = np.cumsum(self.startprob_) transmat_cdf = np.cumsum(self.transmat_, axis=1) currstate = (startprob_cdf > random_state.rand()).argmax() state_sequence = [currstate] X = [ self._generate_sample_from_state(currstate, random_state=random_state) ] for t in range(n_samples - 1): currstate = (transmat_cdf[currstate] > random_state.rand()) \ .argmax() state_sequence.append(currstate) X.append( self._generate_sample_from_state(currstate, random_state=random_state)) return np.atleast_2d(X), np.array(state_sequence, dtype=int)
def score_samples(self, X, lengths=None): """Compute the log probability under the model and compute posteriors. Parameters ---------- X : array-like, shape (n_samples, n_features) Feature matrix of individual samples. lengths : array-like of integers, shape (n_sequences, ), optional Lengths of the individual sequences in ``X``. The sum of these should be ``n_samples``. Returns ------- logprob : float Log likelihood of ``X``. posteriors : array, shape (n_samples, n_components) State-membership probabilities for each sample in ``X``. See Also -------- score : Compute the log probability under the model. decode : Find most likely state sequence corresponding to ``X``. """ _utils.check_is_fitted(self, "startprob_") self._check() X = check_array(X) n_samples = X.shape[0] logprob = 0 posteriors = np.zeros((n_samples, self.n_components)) for i, j in iter_from_X_lengths(X, lengths): framelogprob = self._compute_log_likelihood(X[i:j]) logprobij, fwdlattice = self._do_forward_pass(framelogprob) logprob += logprobij bwdlattice = self._do_backward_pass(framelogprob) posteriors[i:j] = self._compute_posteriors(fwdlattice, bwdlattice) return logprob, posteriors