## 隐马尔科夫和最大熵马尔科夫 原

Digimon

#统计a,pi
def HMMA():
for i in range(len(label)):
#初始化计数
countp=[]
s=0
for j in range(len(label)):
countp.append(0)
for j in range(trainlen-1):
if(trainos[j+1]==label[i]):
s+=1
countp[label.index(trainos[j])]+=1
for j in range(len(label)):
if(s!=0):a[i].append(countp[j]/s)
else:a[i].append(0)
#初始初始矩阵
pi.append(s/trainlen)
global b
b=proyx

#统计b
def HMMB():
count=[]
for i in range(len(label)):
countall=0
for j in range(len(trainx)):
count.append(0)
for j in range(trainlen):
if(trainos[j]==label[i]):
countall+=1
count[trainxx.index(trainsq[j])]+=1
for j in range(len(trainxx)):
hmmb[i].append(count[j]/countall)
count=[]

#维特比
def viterbi(testsq,testos,b):
delta = [[]for i in range(len(testsq))]
fai = [[]for i in range(len(testsq))]
#初始化
if(len(testsq)==0):return
for i in range(len(label)):
if(testsq[0])in trainxx:
delta[0].append(pi[i]*b[i][trainxx.index(testsq[0])]*bigger)
else:
delta[0].append(0)
fai[0].append(0)
for j in range(1,len(testsq)):
for i in range(len(label)):
if(testsq[j]) in trainxx:
maxtmp=-1
max_i=0
for k in range(len(label)):
if(delta[j-1][k]*a[i][k]>maxtmp):
maxtmp=delta[j-1][k]*a[i][k]
max_i=k
delta[j].append(maxtmp*b[i][trainxx.index(testsq[j])])
fai[j].append(max_i)
else:
delta[j].append(0)
fai[j].append(0)
maxtmp=0
max_i=0
mytestchoic=[]
mytestos=[]
for i in range(len(label)):
if maxtmp<delta[len(testsq)-1][i]:
maxtmp=delta[len(testsq)-1][i]
max_i=i
mytestchoic.append(max_i)
for i in range(1,len(testsq)):
mytestchoic.append(fai[i-1][mytestchoic[i-1]])
for i in range(len(testsq)):
mytestos.append(label[mytestchoic[i]])
print(mytestos)

#各种统计初始
def definef():
for i in range(len(label)):
M.append(trainos.count(label[i]))
for j in range(len(trainx)):
f[i].append(0)
w[i].append(0)
proyx[i].append(0)
provx[i].append(0)
for i in range(trainlen-1):
f[label.index(trainos[i])][trainx.index(str(trainsq[i+1]+trainos[i+1]))]=1

def countpos():
#对于每个特征找各自概率
for i in range(len(label)):
countxy=0
for j in range(trainlen-1):
if(f[i][trainx.index(trainsq[j+1]+trainos[j+1])]==1 and trainos[j]==label[i]):
countxy+=1
provx[i][trainx.index(trainsq[j+1]+trainos[j+1])]+=1
for j in range(len(trainx)):
provx[i][j]/=trainlen
provxy[i]=countxy/trainlen

def countyxpos():
for i in range(len(label)):
z=0
p=[]
for j in range(len(trainx)):
p.append(0)
for j in range(trainlen-1):
if(trainos[j+1]==label[i]):
if(f[i][trainx.index(trainsq[j+1]+trainos[j+1])]==1):
z+=math.e**(w[i][trainx.index(trainsq[j+1]+trainos[j+1])]*trainx.count(trainsq[j+1]+trainos[j+1]))
if (f[i][trainx.index(trainsq[j + 1] + trainos[j + 1])] == 1):
p[trainx.index(trainsq[j+1]+trainos[j+1])]+=w[i][trainx.index(trainsq[j+1]+trainos[j+1])]
for j in range(len(trainx)):
if(z!=0):proyx[i][j]=(math.e**p[j])/z
#开始迭代了
def iterate(times):
for i in range(times):
for l in range(len(label)):
for j in range(len(trainx)):
tmp=0
if(M[l]!=0 and provx[l][j]*proyx[l][j]*f[l][j]!=0):
tmp+=math.log(provxy[l]/provx[l][j]*proyx[l][j]*f[l][j])/M[l]
global w
w[l][j]+=tmp
countyxpos()
print(i)

### Digimon

lirainbow0
2017/09/10
0
0

08/31
0
0

11/09
0
0
NLP学习:隐马尔科夫模型(一)

2017/12/29
0
0
【机器学习 基本概念】从朴素贝叶斯到维特比算法：详解隐马尔科夫模型

gongxifacai_believe
2017/11/28
0
0

Idea

command + E : 打开最近编辑过的文件 command + O : 打开指定文件 option + enter: 补全变量 实现接口 格式化字符串 System.out.printf("realName:" + u.getRealName() + "email: " + u.get...

xpttxsok
20分钟前
1
0
FTP 协议 1.0

Explorer0
31分钟前
2
0
Android 通过DrawableInflater加载自定义Drawable

IamOkay
42分钟前
1
0

yizhichao
48分钟前
1
0