隐马尔可夫模型之python实战

隐马尔可夫模型之HMM基础
隐马尔可夫模型之评估观察序列概率
隐马尔可夫模型之学习算法
隐马尔可夫模型之预测算法
隐马尔可夫模型之python实战

手写

定义数据

Hidden_states = ("box 1", "box 2", "box 3") # 隐状态集合
Observations_states = ('red', 'white', 'red') # 观测状态集合

Start_probability = {'box 1': 0.2, 'box 2': 0.4, 'box 3': 0.4} # 初始状态

Hidden_transition_probability = { # 隐马尔可夫链中从当前盒子转移到其他盒子的概率
    'box 1' : {'box 1': 0.5, 'box 2': 0.2, 'box 3': 0.3},
    'box 2' : {'box 1': 0.3, 'box 2': 0.5, 'box 3': 0.2},
    'box 3' : {'box 1': 0.2, 'box 2': 0.3, 'box 3': 0.5},
   }

Hidden_observations_probability = {    # 原来叫emission_probability。这里表示每个盒子里面红白球的数量
    'box 1' : {'red': 0.5, 'white': 0.5},
    'box 2' : {'red': 0.4, 'white': 0.6},
    'box 3' : {'red': 0.7, 'white': 0.3},
   }

定义维特比算法

def viterbi(obs, states, start_p, trans_p, h2o_p): # Viterbi算法
    '''
    input:
        obs:观测状态集合O={红,白,红}
        states:隐状态集合Q={盒子1,盒子2,盒子3}
        start_p:初始状态Π=(0.2,0.4,0.4)T
        trans_p:转移状态矩阵A=【0.5 0.3 0.2
                              0.2 0.5 0.3
                              0.3 0.2 0.5】
        h2o_p:观测状态概率矩阵为:B=【0.5 0.4 
                                   0.7 0.5 
                                   0.6 0.3】
    output:
        prob:最优概率
        path[state]:最优路径
    '''
    V = [{}]
    path = {}

    # Initialize base cases (t == 0)
    for y in states:
        #t=1时刻,状态为i观测o1为红的概率,即δ1(i)
        V[0][y] = start_p[y] * h2o_p[y][obs[0]]
        # 初始状态,由start的概率,对应乘上发射概率,即由隐状态到观测状态的可能性
        path[y] = [y]

    # 开始遍历,t=2和t=3时刻
    for t in range(1,len(obs)):
        V.append({})
        newpath = {}

        for y in states:
            # 对于每个箱子,计算其由前一个的各个状态,到现在箱子的概率大小,取最大值。即求出最有可能到达现在箱子的路径
            # 前一个箱子转移到现在箱子的每个状态对应的路径大小都计算了,取最大值。作为V[t][y]的值,并更新路径
            (prob, state) = max([(V[t-1][y0] * trans_p[y0][y] * h2o_p[y][obs[t]], y0) for y0 in states])
            V[t][y] = prob
            newpath[y] = path[state] + [y]

        # Don't need to remember the old paths
        path = newpath

    print_dptable(V)
    (prob, state) = max([(V[len(obs) - 1][y], y) for y in states])
    return (prob, path[state])

预测

viterbi(Observations_states,
                   Hidden_states,
                   Start_probability,
                   Hidden_transition_probability,
                   Hidden_observations_probability)

(0.014699999999999998, [‘box 3’, ‘box 3’, ‘box 3’])

调包

hmmlearn安装很简单,"pip install hmmlearn"即可完成。
hmmlearn文档

定义数据

states = ["box 1", "box 2", "box3"]
n_states = len(states)

observations = ["red", "white"]
n_observations = len(observations)

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

transition_probability = np.array([
  [0.5, 0.2, 0.3],
  [0.3, 0.5, 0.2],
  [0.2, 0.3, 0.5]
])

emission_probability = np.array([
  [0.5, 0.5],
  [0.4, 0.6],
  [0.7, 0.3]
])

建模

model = hmm.MultinomialHMM(n_components=n_states)
model.startprob_=start_probability
model.transmat_=transition_probability
model.emissionprob_=emission_probability

seen = np.array([[0,1,0]]).T
logprob, box = model.decode(seen, algorithm="viterbi")

结果

print("The ball picked:【", ", ".join(map(lambda x: observations[x[0]], seen)),"】")
print("The hidden box【", ", ".join(map(lambda x: states[x], box)),"】")

The ball picked:【 red, white, red 】
The hidden box【 box3, box3, box3 】

github

根据引用和引用中提供的信息,Python中可以使用隐马尔可夫模型进行股市预测和分析。隐马尔可夫模型是一种有趣的随机过程,特别适用于分析时间序列数据。你可以使用hmmlearn库来扩展Python中的隐马尔可夫模型功能。在Anaconda环境中,你可以使用"conda install hmmlearn"命令直接安装该库。 下面是一段示例代码,用于训练和分析隐马尔可夫模型: ```python import numpy as np # 读取训练数据 file = open("traindata.txt", encoding='utf-8') test_str = "中国首次火星探测任务天问一号探测器实施近火捕获制动" new_sents = [] sents_labels = [] # 处理训练数据 for line in file.readlines(): line = line.split() new_sent = '' sent_labels = '' for word in line: if len(word) == 1: new_sent = word sent_labels = 'S' elif len(word) >= 2: new_sent = word sent_labels = 'B' + 'M' * (len(word) - 2) + 'E' if new_sent != '': new_sents.append([new_sent]) sents_labels.append([sent_labels]) print("训练样本准备完毕!") print('共有数据 %d 条' % len(new_sents)) print('平均长度:', np.mean([len(d<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [HMM隐马尔可夫模型股票价格预测(Python完整源码和数据)](https://download.csdn.net/download/m0_57362105/87337214)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【Python机器学习】隐马尔可夫模型讲解及在中文分词中的实战(附源码和数据集)](https://blog.csdn.net/jiebaoshayebuhui/article/details/128397554)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Andy_shenzl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值