.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_hmm_sampling_and_decoding.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_hmm_sampling_and_decoding.py: Sampling from and decoding an HMM --------------------------------- This script shows how to sample points from a Hidden Markov Model (HMM): we use a 4-state model with specified mean and covariance. The plot shows the sequence of observations generated with the transitions between them. We can see that, as specified by our transition matrix, there are no transition between component 1 and 3. Then, we decode our model to recover the input parameters. .. GENERATED FROM PYTHON SOURCE LINES 14-63 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from hmmlearn import hmm # Prepare parameters for a 4-components HMM # Initial population probability startprob = np.array([0.6, 0.3, 0.1, 0.0]) # The transition matrix, note that there are no transitions possible # between component 1 and 3 transmat = np.array([[0.7, 0.2, 0.0, 0.1], [0.3, 0.5, 0.2, 0.0], [0.0, 0.3, 0.5, 0.2], [0.2, 0.0, 0.2, 0.6]]) # The means of each component means = np.array([[0.0, 0.0], [0.0, 11.0], [9.0, 10.0], [11.0, -1.0]]) # The covariance of each component covars = .5 * np.tile(np.identity(2), (4, 1, 1)) # Build an HMM instance and set parameters gen_model = hmm.GaussianHMM(n_components=4, covariance_type="full") # Instead of fitting it from the data, we directly set the estimated # parameters, the means and covariance of the components gen_model.startprob_ = startprob gen_model.transmat_ = transmat gen_model.means_ = means gen_model.covars_ = covars # Generate samples X, Z = gen_model.sample(500) # Plot the sampled data fig, ax = plt.subplots() ax.plot(X[:, 0], X[:, 1], ".-", label="observations", ms=6, mfc="orange", alpha=0.7) # Indicate the component numbers for i, m in enumerate(means): ax.text(m[0], m[1], 'Component %i' % (i + 1), size=17, horizontalalignment='center', bbox=dict(alpha=.7, facecolor='w')) ax.legend(loc='best') fig.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_hmm_sampling_and_decoding_001.png :alt: plot hmm sampling and decoding :srcset: /auto_examples/images/sphx_glr_plot_hmm_sampling_and_decoding_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 64-65 Now, let's ensure we can recover our parameters. .. GENERATED FROM PYTHON SOURCE LINES 65-90 .. code-block:: Python scores = list() models = list() for n_components in (3, 4, 5): for idx in range(10): # define our hidden Markov model model = hmm.GaussianHMM(n_components=n_components, covariance_type='full', random_state=idx) model.fit(X[:X.shape[0] // 2]) # 50/50 train/validate models.append(model) scores.append(model.score(X[X.shape[0] // 2:])) print(f'Converged: {model.monitor_.converged}' f'\tScore: {scores[-1]}') # get the best model model = models[np.argmax(scores)] n_states = model.n_components print(f'The best model had a score of {max(scores)} and {n_states} ' 'states') # use the Viterbi algorithm to predict the most likely sequence of states # given the model states = model.predict(X) .. rst-class:: sphx-glr-script-out .. code-block:: none Converged: True Score: -1527.6103147297638 Converged: True Score: -1148.4356646331694 Converged: True Score: -1191.1601912405647 Converged: True Score: -1098.0750315923015 Converged: True Score: -1119.7452647055522 Converged: True Score: -1098.075031592303 Converged: True Score: -1140.874796473395 Converged: True Score: -1098.0750315923015 Converged: True Score: -1238.5263259024998 Converged: True Score: -1098.075031592301 Converged: True Score: -906.5106498784926 Converged: True Score: -1148.7445110797862 Converged: True Score: -906.5106499565098 Converged: True Score: -1091.4885480848852 Converged: True Score: -906.5106499565106 Converged: True Score: -1100.6839294105614 Converged: True Score: -1013.3406060462202 Converged: True Score: -906.5106499565131 Converged: True Score: -906.5106499565128 Converged: True Score: -1008.0354860189984 Converged: True Score: -1231.847848276572 Converged: True Score: -938.6242405526924 Converged: True Score: -970.1245215103969 Converged: True Score: -909.2005389933829 Converged: True Score: -854.1734350122523 Converged: True Score: -1204.8064665019842 Converged: True Score: -927.3437688713946 Converged: True Score: -908.3461737855713 Converged: True Score: -912.5172728780512 Converged: True Score: -823.1604241221277 The best model had a score of -823.1604241221277 and 5 states .. GENERATED FROM PYTHON SOURCE LINES 91-97 Let's plot our states compared to those generated and our transition matrix to get a sense of our model. We can see that the recovered states follow the same path as the generated states, just with the identities of the states transposed (i.e. instead of following a square as in the first figure, the nodes are switch around but this does not change the basic pattern). The same is true for the transition matrix. .. GENERATED FROM PYTHON SOURCE LINES 97-118 .. code-block:: Python # plot model states over time fig, ax = plt.subplots() ax.plot(Z, states) ax.set_title('States compared to generated') ax.set_xlabel('Generated State') ax.set_ylabel('Recovered State') fig.show() # plot the transition matrix fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 5)) ax1.imshow(gen_model.transmat_, aspect='auto', cmap='spring') ax1.set_title('Generated Transition Matrix') ax2.imshow(model.transmat_, aspect='auto', cmap='spring') ax2.set_title('Recovered Transition Matrix') for ax in (ax1, ax2): ax.set_xlabel('State To') ax.set_ylabel('State From') fig.tight_layout() fig.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_hmm_sampling_and_decoding_002.png :alt: States compared to generated :srcset: /auto_examples/images/sphx_glr_plot_hmm_sampling_and_decoding_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_hmm_sampling_and_decoding_003.png :alt: Generated Transition Matrix, Recovered Transition Matrix :srcset: /auto_examples/images/sphx_glr_plot_hmm_sampling_and_decoding_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.445 seconds) .. _sphx_glr_download_auto_examples_plot_hmm_sampling_and_decoding.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_hmm_sampling_and_decoding.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_hmm_sampling_and_decoding.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_hmm_sampling_and_decoding.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_