资源简介
用python实现的Hmm,有详细的代码注解,对Hmm感兴趣的同学值得下载学习
代码片段和文件信息
# !/usr/bin/python
# -*- coding:utf-8 -*-
import math
import matplotlib.pyplot as plt
import numpy as np
import codecs
import random
infinite = float(-2**31)
def log_normalize(a):
s = 0
for x in a:
s += x
if s == 0:
print “Error..from log_normalize.“
return
s = math.log(s)
for i in range(len(a)):
if a[i] == 0:
a[i] = infinite
else:
a[i] = math.log(a[i]) - s
def log_sum(a):
if not a: # a为空
return infinite
m = max(a)
s = 0
for t in a:
s += math.exp(t-m)
return m + math.log(s)
def calc_alpha(pi A B o alpha):
for i in range(4):
alpha[0][i] = pi[i] + B[i][ord(o[0])]
T = len(o)
temp = [0 for i in range(4)]
del i
for t in range(1 T):
for i in range(4):
for j in range(4):
temp[j] = (alpha[t-1][j] + A[j][i])
alpha[t][i] = log_sum(temp)
alpha[t][i] += B[i][ord(o[t])]
def calc_beta(pi A B o beta):
T = len(o)
for i in range(4):
beta[T-1][i] = 1
temp = [0 for i in range(4)]
del i
for t in range(T-2 -1 -1):
for i in range(4):
beta[t][i] = 0
for j in range(4):
temp[j] = A[i][j] + B[j][ord(o[t+1])] + beta[t+1][j]
beta[t][i] += log_sum(temp)
def calc_gamma(alpha beta gamma):
for t in range(len(alpha)):
for i in range(4):
gamma[t][i] = alpha[t][i] + beta[t][i]
s = log_sum(gamma[t])
for i in range(4):
gamma[t][i] -= s
def calc_ksi(alpha beta A B o ksi):
T = len(alpha)
temp = [0 for x in range(16)]
for t in range(T-1):
k = 0
for i in range(4):
for j in range(4):
ksi[t][i][j] = alpha[t][i] + A[i][j] + B[j][ord(o[t+1])] + beta[t+1][j]
temp[k] =ksi[t][i][j]
k += 1
s = log_sum(temp)
for i in range(4):
for j in range(4):
ksi[t][i][j] -= s
def bw(pi A B alpha beta gamma ksi o):
T = len(alpha)
for i in range(4):
pi[i] = gamma[0][i]
s1 = [0 for x in range(T-1)]
s2 = [0 for x in range(T-1)]
for i in range(4):
for j in range(4):
for t in range(T-1):
s1[t] = ksi[t][i][j]
s2[t] = gamma[t][i]
A[i][j] = log_sum(s1) - log_sum(s2)
s1 = [0 for x in range(T)]
s2 = [0 for x in range(T)]
for i in range(4):
print “bw“ i
for k in range(65536):
valid = 0
if k % 10000 == 0:
print “bw - k“ k
for t in range(T):
if ord(o[t]) == k:
s1[valid] = gamma[t][i]
valid += 1
s2[t] = gamma[t][i]
if valid == 0:
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-05-08 15:01 24.HMM\
目录 0 2017-05-08 15:01 24.HMM\.idea\
文件 459 2017-05-08 13:07 24.HMM\.idea\24.HMM.iml
文件 687 2017-05-08 13:07 24.HMM\.idea\misc.xm
文件 264 2017-05-08 13:01 24.HMM\.idea\modules.xm
文件 25488 2017-05-08 13:15 24.HMM\.idea\workspace.xm
文件 6612 2017-05-08 13:08 24.HMM\24.1.TrainHMM.py
文件 7390 2017-05-08 13:08 24.HMM\24.2.Segmentation.py
文件 355 2017-05-08 13:09 24.HMM\24.3.jieba_intro.py
文件 4036 2017-05-08 13:12 24.HMM\24.4.GMHMM.py
文件 2178 2017-05-08 13:12 24.HMM\24.5.Stock.py
文件 4646 2017-05-08 13:14 24.HMM\24.6.Current.py
文件 245 2017-05-08 13:08 24.HMM\A.txt
文件 3682048 2017-05-08 13:08 24.HMM\B.txt
文件 135168 2017-03-08 13:46 24.HMM\Current.xls
文件 100000000 2006-06-09 20:40 24.HMM\Englishword.train
文件 11515 2016-11-25 13:06 24.HMM\MyBook.txt
文件 84462 2016-11-25 13:06 24.HMM\SH600000.txt
文件 261120 2017-05-08 13:14 24.HMM\new_current.xls
文件 38981 2016-11-25 13:06 24.HMM\novel.txt
文件 62 2017-05-08 13:08 24.HMM\pi.txt
文件 7728239 2016-11-25 13:06 24.HMM\pku_training.utf8
相关资源
- matlab代码转换为python代码
- Python-GUI-PyQ5总概述.xmind
- 基于python实现的BS架构FTP服务器程序
- Networkx安装步骤及资源
- python深度学习-高清pdf-源码
- 嵩天 礼欣 黄天羽 著 Python语言程序
- Python3.8.0 官方中文帮助文档 API参考手
- Introduction to Computation and Programming Us
- 亚马逊电子书-Keras快速上手:基于P
- ROS系统上的python应用开发实践讲解与
- python 数据挖掘入门与实战 pdf+代码
- Python3.4 PyQt5 32位安装版PyQt5-5.5.1-gpl-
- 基于python3 tensorflow DBN_and_RNN的实现
- Fluent Python高清中文版
- python计算机视觉编程programming compute
- python调用dlib库实现简单的人脸识别
- Python-全唐诗分析程序
- Python-我是小诗姬全唐诗作为训练数据
- 基于python和pencv的车牌号码识别
- Python实现的GMM用于语音识别
- python+selenium+unittest自动化测试demo
- Django可用的安装包
- Python for Kids - A Playful Introduction to Pr
- scipy-1.4.1-cp38-cp38-win_amd64.whl
- Python+selenium+HTMLTestRunner+unittest 测试框
- Python做一个推箱子小游戏
- python学习手册中文版.epub
- python预测分析核心算法含大量代码
- python智能垃圾分类串口控制arduino.zi
- python实现kaggle中的数字识别
评论
共有 条评论