Digimon 发表于6个月前

• 发表于 6个月前
• 阅读 31
• 收藏 0
• 评论 0

``````这一步直接就是对训练数据进行统计就完了
``````

``````算法：
``````

``````#统计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)
``````

×