<jupyter_start><jupyter_text>Discrete HMM<jupyter_code>import numpy as np
from hmmlearn import hmm

states = ["Rainy", "Sunny"]
n_states = len(states)

vocabulary = ["walk", "shop", "clean"]
n_vocabulary = len(vocabulary)

start_probability = np.array([0.6, 0.4])

transition_probability = np.array([
  [0.7, 0.3],
  [0.10, 0.90]
])

emission_probability = np.array([
  [0.1, 0.4, 0.5],
  [0.6, 0.3, 0.1]
])
model = hmm.MultinomialHMM(n_components=n_states)
model.startprob_ = start_probability
model.transmat_ = transition_probability
model.emissionprob_ = emission_probability
model
observed_sequence = ["walk", "clean", "shop", "clean", "clean", "walk", "shop", "clean", "walk", "walk", "shop", "walk", "walk", "clean","walk","shop"]
X = np.array([[vocabulary.index(o)] for o in observed_sequence])
X
# predict a sequence of hidden states based on visible states

logprob, path = model.decode(X, algorithm="viterbi")
print("Observed sequence:", ", ".join(observed_sequence))
print("Most probable path:", ", ".join(map(lambda x: states[x], path)))
logprob, posteriors = model.score_samples(X)
posteriors = pd.DataFrame(posteriors)
posteriors.columns = states
posteriors.index = observed_sequence
posteriors.head()
ax = posteriors['Rainy'].plot(figsize=(10,3), drawstyle='steps-post', xticks=range(0,len(posteriors)), ylim=(0,1))<jupyter_output><empty_output><jupyter_text>Gaussian mixture HMM<jupyter_code>emission_probability = np.array([
  [0.1, 0.4, 0.5],
  [0.6, 0.3, 0.1]
])
model = hmm.MultinomialHMM(n_components=n_states)
model.startprob_ = start_probability
model.transmat_ = transition_probability
model.emissionprob_ = emission_probability
model<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16528436018957346, 'fraction_numerical': 0.026066350710900472, 'mean_word_length': 6.855813953488372, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '351639', 'license': 'MIT', 'n_tokens_mistral': 649, 'n_tokens_neox': 619, 'n_words': 125, 'path': 'notebooks/ExampleHMM.ipynb', 'repo_name': 'prihoda/bgc-pipeline-1', 'size': '1688'}