当前位置: 首页>>代码示例>>Python>>正文


Python HMM.setAij方法代码示例

本文整理汇总了Python中hmm.HMM.setAij方法的典型用法代码示例。如果您正苦于以下问题:Python HMM.setAij方法的具体用法?Python HMM.setAij怎么用?Python HMM.setAij使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在hmm.HMM的用法示例。


在下文中一共展示了HMM.setAij方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: learn

# 需要导入模块: from hmm import HMM [as 别名]
# 或者: from hmm.HMM import setAij [as 别名]
 def learn(self, oseq, nbIter=1):
     '''
     Build the new model that increase the probability of observing the sequence o
     '''
     
     T = len(oseq)
     #self.N = hmm.stateSize
     
     oseqInd = range( T )
     #self.stateInd = range( self.N )
     #interInd = range( nbIter )
     
     xi = np.zeros((T-1, self.N, self.N)) # 3-D arrays : (T-1) * self.N * self.N
     gamma = np.zeros((T, self.N))
     sumXi = np.zeros((self.N, self.N))
     sumGamma = np.zeros(self.N)
     
     fwbw = ForwardBackward(self.hmm, oseq, True, True)
     
     def calculateXi() :
         '''
         Compute the xi arrays
         '''
                                                                            
         po = fwbw.probability
     
         for t in oseqInd[:T-1] : # last transition start at T-1 to T.
                                 # we have T-1 possible transitions 
             for i in self.self.stateInd :
             
                 for j in self.stateInd : # x[t][i][j] probability of being in state i at time t and in state j at time t+1
                     xi[t, i, j] = fwbw.getAlphati(t, i) *  self.hmm.getAij(i, j) * self.hmm.getBik(j, oseq[t]) * fwbw.getBetatj(t+1, j) / po
      
      
     def calculateGamma() :
         '''
         Compute the gamma array
         '''
     
         for t in oseqInd[:T-1] :                
             for i in self.stateInd :
                 for j in self.stateInd :
                     gamma[t, i] += xi[t, i, j] # The nb transition from i at time t
         
         t += 1
         
         for j in self.stateInd :    # t = T, probability that the last observable will be produce by State j
             for i in self.stateInd :
                 gamma[t][j] = xi[t-1][i][j];
       
       
     calculateXi()
     calculateGamma()
     
     for i in self.stateInd : # compute the number of transition from state i to state j        
         for j in self.stateInd :
             for t in oseqInd[:T-1] :
                 sumXi[i, j] += xi[t, i, j];
                 
     for i in self.stateInd : # compute the number of transition from state i               
         for t in oseqInd[:T-1] :
             sumGamma[i] += gamma[t, i];
     
     newHmm = HMM(self.hmm.stateSize, self.hmm.obsSize)
     
     for i in self.stateInd :
         newHmm.setPii( i,  gamma[1, i] )
         
         for j in self.stateInd :
             newHmm.setAij(i, j, sumXi[i, j] / sumGamma[i] );    # aij
         
         for k in range(newHmm.obsSize) : #compute bik
             
             sumGammaOk = 0.0;
             t = 0;
             
             for t in oseqInd : #compute the number of times ot is produced by state i
                 
                 ot = oseq[t]
                 
                 if ot == k :
                     sumGammaOk += gamma[t][i];
                     #System.out.println(" obs " + ot );
                 
             
             newHmm.setBik(i, k, sumGammaOk / ( sumGamma[i] + gamma[T-1][i] )  ) # we add the last
         
     return newHmm    
         
开发者ID:openforai,项目名称:posTagging,代码行数:90,代码来源:baumWelch.py

示例2: HMM

# 需要导入模块: from hmm import HMM [as 别名]
# 或者: from hmm.HMM import setAij [as 别名]
bik = np.genfromtxt(StringIO(opdf), delimiter=' ')
oseq = np.array([0, 0, 0, 0, 1, 0, 1, 1, 1, 1])


hmm = HMM(nbState, nbObs)

for i in range( nbState ):
    
    hmm.setPii(i, pi)
    
    for k in range( nbObs ):
        hmm.setBik( i, k, bik[i,k] )

for i in range( nbState ):
    for j in range( nbState ):
        hmm.setAij(i, j, aij)
     
hmm.display()


bestPath = viterbi.viterbiProcessing(hmm, oseq)
print("Best Path : {0}, p = {1}".format(bestPath[0], bestPath[1]))
  
fwbw = ForwardBackward(hmm, oseq, False, True)
print("Probabilty of {0} is {1}".format(oseq, fwbw.probability) )
  
print("Baum Welch processing ...")
  
oldHmm = hmm
oldP = bestPath[1]
for i in range(1,11):
开发者ID:openforai,项目名称:posTagging,代码行数:33,代码来源:testHmm.py


注:本文中的hmm.HMM.setAij方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。