<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://wiki.swarma.org/index.php?action=history&amp;feed=atom&amp;title=HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B</id>
	<title>HMM隐性马可夫模型 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.swarma.org/index.php?action=history&amp;feed=atom&amp;title=HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B"/>
	<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B&amp;action=history"/>
	<updated>2026-11-03T05:44:26Z</updated>
	<subtitle>本wiki的该页面的版本历史</subtitle>
	<generator>MediaWiki 1.35.0</generator>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B&amp;diff=15279&amp;oldid=prev</id>
		<title>2020年10月15日 (四) 07:30 思无涯咿呀咿呀</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B&amp;diff=15279&amp;oldid=prev"/>
		<updated>2020-10-15T07:30:35Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2020年10月15日 (四) 07:30的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l143&quot; &gt;第143行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第143行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:机器学习]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:机器学习]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[category:旧词条迁移]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>思无涯咿呀咿呀</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B&amp;diff=5515&amp;oldid=prev</id>
		<title>18621066378：导入1个版本</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B&amp;diff=5515&amp;oldid=prev"/>
		<updated>2020-04-28T01:46:50Z</updated>

		<summary type="html">&lt;p&gt;导入1个版本&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2020年4月28日 (二) 01:46的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;zh-Hans-CN&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;（没有差异）&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>18621066378</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B&amp;diff=5514&amp;oldid=prev</id>
		<title>http://c2.com/cgi/wiki?$1&gt;Wangting：/* 相关wiki */</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B&amp;diff=5514&amp;oldid=prev"/>
		<updated>2016-12-05T06:04:48Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;相关wiki&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==原理==&lt;br /&gt;
&lt;br /&gt;
Hidden Markov model，隐性马可夫模型，是语音处理和图像识别等领域常常会用到的模型。它的核心原理是假设观测变量是由一些隐变量引起的，通过观测变量的数据去推断隐变量的发生概率。隐性马可夫和传统马可夫链的区别就是，在后者中，系统的状态是可观测的，我们只需要根据数据计算状态之间的转移概率即可。&lt;br /&gt;
&lt;br /&gt;
[[File:HMM1.png|400px]]&lt;br /&gt;
&lt;br /&gt;
在上图中：&lt;br /&gt;
&lt;br /&gt;
x 代表状态 （states）&lt;br /&gt;
&lt;br /&gt;
y 代表观测到的行为 （possible observations）&lt;br /&gt;
&lt;br /&gt;
a 代表状态转移概率 （state transition probabilities）&lt;br /&gt;
&lt;br /&gt;
b 代表状态产生行为的概率 （output probabilities）&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Alice和Bob的例子==&lt;br /&gt;
&lt;br /&gt;
[[File:HMM2.png|400px]]&lt;br /&gt;
&lt;br /&gt;
Alice和Bob是朋友，Bob每天打电话和Alice说今天做了什么事，Alice从中可以猜测Bob所在地方的天气如何（隐变量）。天气有两种状态，rainy和sunny。前者概率是0.6，后者概率是0.4，从一个状态转到另一个状态的概率如上图所示。Bob的行为有三种可能，散步（walking）,购物（shopping）和在家里做家务（cleanning）。显然，下雨的时候比较可能在家做家务，天气晴朗的时候比较可能散步，但也不绝对。天气与行为之间的关系也如上图所示。&lt;br /&gt;
&lt;br /&gt;
这个图里的信息使用python语言可以表达如下：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    from __future__ import division&lt;br /&gt;
    import numpy as np&lt;br /&gt;
    from sklearn import hmm&lt;br /&gt;
    &lt;br /&gt;
    states = [&amp;quot;Rainy&amp;quot;, &amp;quot;Sunny&amp;quot;]&lt;br /&gt;
    n_states = len(states)&lt;br /&gt;
    &lt;br /&gt;
    observations = [&amp;quot;walk&amp;quot;, &amp;quot;shop&amp;quot;, &amp;quot;clean&amp;quot;]&lt;br /&gt;
    n_observations = len(observations)&lt;br /&gt;
    &lt;br /&gt;
    start_probability = np.array([0.6, 0.4])&lt;br /&gt;
    &lt;br /&gt;
    transition_probability = np.array([&lt;br /&gt;
      [0.7, 0.3],&lt;br /&gt;
      [0.4, 0.6]&lt;br /&gt;
    ])&lt;br /&gt;
    &lt;br /&gt;
    emission_probability = np.array([&lt;br /&gt;
      [0.1, 0.4, 0.5],&lt;br /&gt;
      [0.6, 0.3, 0.1]&lt;br /&gt;
    ])&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
使用sklearn自带的包来训练这个HMM模型是非常容易的：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    model = hmm.MultinomialHMM(n_components=n_states)&lt;br /&gt;
    model._set_startprob(start_probability)&lt;br /&gt;
    model._set_transmat(transition_probability)&lt;br /&gt;
    model._set_emissionprob(emission_probability)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
训练模型完毕之后，我们就可以根据训练得到的模型，来从Bob对他自己行为的描述中推断天气了：&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    bob_says = [0, 2, 1, 1, 2, 0]&lt;br /&gt;
    logprob, alice_hears = model.decode(bob_says, algorithm=&amp;quot;viterbi&amp;quot;)&lt;br /&gt;
    print &amp;quot;Bob says:&amp;quot;, &amp;quot;, &amp;quot;.join(map(lambda x: observations[x], bob_says))&lt;br /&gt;
    print &amp;quot;Alice hears:&amp;quot;, &amp;quot;, &amp;quot;.join(map(lambda x: states[x], alice_hears))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
打印出来的结果是：&lt;br /&gt;
&lt;br /&gt;
“Bob says: walk, clean, shop, shop, clean, walk”&lt;br /&gt;
“Alice hears: Sunny, Rainy, Rainy, Rainy, Rainy, Sunny”&lt;br /&gt;
&lt;br /&gt;
其实我们这里并不是使用实际数据来训练，而是直接指定了模型的参数。在真实的案例中，需要准备一个训练集，既有Bob行为的数据，也有当地天气的数据，从这里面，使用HMM，估计出各种参数。然后在测试样本中，再根据估计得到的参数和新的Bob行为数据来猜测真实的天气情况。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==使用sklearn来制造一个toy model==&lt;br /&gt;
&lt;br /&gt;
先import一下包&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    import numpy as np&lt;br /&gt;
    import matplotlib.pyplot as plt&lt;br /&gt;
    from sklearn import hmm&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
然后预设一些参数&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    # Initial population probability&lt;br /&gt;
    start_prob = np.array([0.6, 0.3, 0.1, 0.0])&lt;br /&gt;
    # The transition matrix, note that there are no transitions possible&lt;br /&gt;
    # between component 1 and 4&lt;br /&gt;
    trans_mat = np.array([[0.7, 0.2, 0.0, 0.1],&lt;br /&gt;
                          [0.3, 0.5, 0.2, 0.0],&lt;br /&gt;
                          [0.0, 0.3, 0.5, 0.2],&lt;br /&gt;
                          [0.2, 0.0, 0.2, 0.6]])&lt;br /&gt;
    # The means of each component&lt;br /&gt;
    means = np.array([[0.0,  0.0],&lt;br /&gt;
                      [0.0, 11.0],&lt;br /&gt;
                      [9.0, 10.0],&lt;br /&gt;
                      [11.0, -1.0],&lt;br /&gt;
                      ])&lt;br /&gt;
    # The covariance of each component&lt;br /&gt;
    covars = .5 * np.tile(np.identity(2), (4, 1, 1))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
使用预定的参数来指定模型&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    # Build an HMM instance and set parameters&lt;br /&gt;
    model = hmm.GaussianHMM(4, &amp;quot;full&amp;quot;, start_prob, trans_mat,&lt;br /&gt;
                            random_state=42)&lt;br /&gt;
    # Instead of fitting it from the data, we directly set the estimated&lt;br /&gt;
    # parameters, the means and covariance of the components&lt;br /&gt;
    model.means_ = means&lt;br /&gt;
    model.covars_ = covars&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
最后，把四个component画出来&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    # Generate samples&lt;br /&gt;
    X, Z = model.sample(500)&lt;br /&gt;
    &lt;br /&gt;
    # Plot the sampled data&lt;br /&gt;
    plt.plot(X[:, 0], X[:, 1], &amp;quot;-o&amp;quot;, label=&amp;quot;observations&amp;quot;, ms=6,&lt;br /&gt;
             mfc=&amp;quot;orange&amp;quot;, alpha=0.7)&lt;br /&gt;
    &lt;br /&gt;
    # Indicate the component numbers&lt;br /&gt;
    for i, m in enumerate(means):&lt;br /&gt;
        plt.text(m[0], m[1], 'Component %i' % (i + 1),&lt;br /&gt;
                 size=17, horizontalalignment='center',&lt;br /&gt;
                 bbox=dict(alpha=.7, facecolor='w'))&lt;br /&gt;
    plt.legend(loc='best')&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
如下图所示：&lt;br /&gt;
&lt;br /&gt;
[[File:HMM_figure_1.png|500px]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==相关wiki==&lt;br /&gt;
*[[From dynamics to equilibrium]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[category:机器学习]]&lt;/div&gt;</summary>
		<author><name>http://c2.com/cgi/wiki?$1&gt;Wangting</name></author>
	</entry>
</feed>