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


Python hmm.HMM类代码示例

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


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

示例1: main

def main():
    hmm = HMM(*train(sys.argv[1]))
    
    with open(sys.argv[2]) as f:
        correct = 0
        wrong = 0
        
        correct_sents = 0
        wrong_sents = 0
        
        correct_known = 0
        wrong_known = 0
        
        for i, sent in enumerate(Reader(f)):
            prob, path = hmm.decode([word for (word, pos) in sent])
            correct1 = 0
            wrong1 = 0
            for (gold, predicted) in zip(sent, path):
                if gold == predicted:
                    correct1 += 1
                else:
                    wrong1 += 1
            print('%e\t%.3f\t%s' % (prob, correct1 / (correct1 + wrong1), ' '.join('%s/%s' % pair for pair in path)))
            if prob > 0:
                correct_sents += 1
                correct_known += correct1
                wrong_known += wrong1
            else:
                wrong_sents += 1
            correct += correct1
            wrong += wrong1
    
    print("Correctly tagged words: %s" % (correct / (correct + wrong)))
    print("Sentences with non-zero probability: %s" % (correct_sents / (correct_sents + wrong_sents)))
    print("Correctly tagged words when only considering sentences with non-zero probability: %s" % (correct_known / (correct_known + wrong_known)))
开发者ID:steffervescency,项目名称:compling,代码行数:35,代码来源:tagger.py

示例2: train

 def train(self):
     mmModel = np.zeros((noteRange+1, noteRange+1))
     mm3Model = np.zeros((noteRange+1, noteRange+1, noteRange+1))
     hmmModel = HMM(12, noteRange+1)
     obs = []
     ground = []
     actions = []
     for i in range(minNote, maxNote):
         actions.append(i)
     qModel = QLearner(actions, epsilon=0.1, alpha=0.2, gamma=0.9)
     for ls in self.clusterData:
         for quadidx, quad in enumerate(ls):
             tempquad = map(lambda x: x - minNote, quad) #take this out for prevnote stuff
             obs.append(tempquad[1:]) #this is for hmm: you can also do same thing for qlearning to change state that way
             tempquad = map(lambda x: (x - minNote) % 12, quad)
             ground.append(tempquad[:3])
             if (quad):
                 for idx, note in enumerate(quad):
                     if idx > 0:
                         currNote = note
                         prevNote = quad[idx - 1]
                         #Q learning
                         #q.learn(state1, action1, reward, state2)
                         qModel.learn(prevNote, note, 1, note)
                         #Markov model
                         mmModel[currNote - minNote, prevNote - minNote] += 1
                     if idx > 2:
                         #Markov model, more order
                         currNote = note - minNote
                         prevNote = quad[idx - 1] - minNote
                         prevNote2 = quad[idx - 2] - minNote
                         mm3Model[currNote, prevNote, prevNote2] += 1
     hmmModel.learn(obs, ground)
     return (mmModel, mm3Model, hmmModel, qModel)
开发者ID:howonlee,项目名称:221proj,代码行数:34,代码来源:model.py

示例3: HMMPOSTagger

class HMMPOSTagger(object):
    """
    在中文分词结果基础上, 采用 HMM 模型实现词性标注 (Part-of-speech tagging).
    """

    def __init__(self):
        self.hmm = HMM()

        self.re_chinese = re.compile(ur"([\u4E00-\u9FA5]+)")  # 正则匹配汉字串
        self.re_skip = re.compile(ur"([\.0-9]+|[a-zA-Z0-9]+)")  # 正则匹配英文串和数字串

    def load(self, model_dir):
        """
        加载模型文件.
        """
        self.hmm.load(model_dir)

    def pos_tag(self, words):
        """
        基于 HMM 模型的词性标注.
        """
        log_prob, pos_list = self._viterbi(words)

        for i, w in enumerate(words):
            yield (w, pos_list[i])
开发者ID:fandywang,项目名称:python-wordsegmenter,代码行数:25,代码来源:hmm_pos_tagger.py

示例4: prepare_seqs_nl_dbg

    def prepare_seqs_nl_dbg(self, decoding="viterbi"):
        params_fixed = (np.load("{}ip.npy".format(self.path)),
                        np.load("{}tp.npy".format(self.path)),
                        np.load("{}fp.npy".format(self.path)),
                        np.load("{}ep.npy".format(self.path)))

        h = HMM(self.n_states, self.n_obs, params=params_fixed, writeout=False)
        h.dirname = self.path
        self.ner_corpus = Conll2002NerCorpus(self.dataset.x_dict)

        # train_seq = self.ner_corpus.read_sequence_list_conll(ned_train)
        dev_seq = self.ner_corpus.read_sequence_list_conll(ned_dev)
        test_seq = self.ner_corpus.read_sequence_list_conll(ned_test)

        if decoding == "viterbi":
            decoder = h.viterbi_decode_corpus
        elif decoding == "max_emission":
            decoder = h.max_emission_decode_corpus
        elif decoding == "posterior":
            decoder = h.posterior_decode_corpus
        elif decoding == "posterior_cont":
            decoder = h.posterior_cont_decode_corpus
        elif decoding == "posterior_cont_type":
            decoder = h.posterior_cont_type_decode_corpus
        else:
            print("Decoder not defined, using Viterbi.")
            decoder = h.viterbi_decode_corpus

        # print("Decoding word representations on train.")
        # decoder(train_seq)
        print("Decoding word representations on dev.")
        decoder(dev_seq)
开发者ID:leondz,项目名称:hmm-reps,代码行数:32,代码来源:PrepareHmmRep.py

示例5: toy_model

 def toy_model(self):
     hmm = HMM(["s1", "s2"], ["R", "NR"])
     init = [0.5, 0.5]
     trans = [[0.2, 0.8], [0.8, 0.2]]
     observ = [[0.8, 0.2], [0.2, 0.8]]
     hmm.set_hidden_model(init, trans, observ)
     return hmm
开发者ID:jmdupont,项目名称:Stanford-Machine-Learning-Course,代码行数:7,代码来源:test_hmm.py

示例6: __init__

    def __init__(self, limb, name='Demo_26_Final'):
        rospy.init_node(name, anonymous=True)
	self._startGesture = '0'
	self._limb = limb
        self._knhObj = KinectNiteHelp()
	self._baeObj = BaxterArmEndpoint(self._limb)
	
	self._dmObj = Demo26Help()
	
	self._lhObj = LeapHelp()
	
	self._handCoordinates = []
	self._baxterCoordinates = []
	self._posCount = 0
	rtMatFile = open("RTMatFile.dat", "r")
	self._rotMat = cPickle.load(rtMatFile)
	self._transMat = cPickle.load(rtMatFile)
	self._gCount = 0
	self._gOn = 0
	self._gPointCount = 0
	self._gPoints = []
	self._hmmObjG1 = HMM('G1.hmm')
	self._hmmObjG2 = HMM('G2.hmm')
	self._hmmObjG3 = HMM('G3.hmm')
	self._hmmObjG4 = HMM('G4.hmm')
	self._flag = '0'
	self._pub = rospy.Publisher('/robot/xdisplay', Image, latch=True)
	#self._flagPub = rospy.Publisher('flag_topic', String)
	self._sub = rospy.Subscriber('/key_tap_topic', String, self._callback) 
	rtMatFile.close()
	img = cv.LoadImage('Welcome.png')
    	msg = cv_bridge.CvBridge().cv_to_imgmsg(img, encoding="bgr8")
    	self._pub.publish(msg)
    	# Sleep to allow for image to be published.
    	rospy.sleep(3)
开发者ID:sparida,项目名称:PurdueSURFBaxterCode,代码行数:35,代码来源:demo26_final.py

示例7: main

def main():
    parser = OptionParser()
    parser.add_option("-d", dest="training", help="training data directory")
    parser.add_option("-k", dest="K", type="int", help="number of latent states", default=6)
    parser.add_option("-a", dest="a", type="float", help="Dirichlet parameter", default=1.0)
    parser.add_option("-i", dest="I", type="int", help="iteration count", default=10)
    parser.add_option("-m", dest="model", help="model data filename to save")
    (options, args) = parser.parse_args()
    if not options.training: parser.error("need training data directory(-d)")

    features = load_data(options.training)

    hmm = HMM()
    hmm.set_corpus(features)
    hmm.init_inference(options.K, options.a)
    pre_L = -1e10
    for i in range(options.I):
        log_likelihood = hmm.inference()
        print i, ":", log_likelihood
        if pre_L > log_likelihood: break
        pre_L = log_likelihood
    if options.model:
        hmm.save(options.model)
    else:
        hmm.dump()
开发者ID:52nlp,项目名称:iir,代码行数:25,代码来源:train.py

示例8: simple_weather_model

def simple_weather_model():
    hmm = HMM(["s1", "s2"], ["R", "NR"])
    init = [0.7, 0.3]
    trans = [[0.8, 0.2], [0.1, 0.9]]
    observ = [[0.75, 0.25], [0.4, 0.6]]
    hmm.set_hidden_model(init, trans, observ)
    return hmm
开发者ID:jmdupont,项目名称:Stanford-Machine-Learning-Course,代码行数:7,代码来源:test_hmm.py

示例9: main

def main():
    tagged_words = brown.tagged_words()
    words_corpus = brown.words()

    word2vec = Word2Vec()
    word2vec.train(words_corpus)

    word_vecs = [word2vec.word2vec(word) for word in words_corpus]

    n_clusters = 10 # random number for now
    kmeans = KMeans(n_clusters)
    kmeans.compute(word_vecs)

    # word-cluster HMM
    p_word = {}
    p_cluster = {}

    p_cluster_given_word = None # softmax
    p_word_given_cluster = None # joint probability formula

    p_transition_cluster = None # count
    p_initial_cluster = None # count

    # cluster-tag HMM
    p_cluster_given_tag = None # softmax
    p_transition_tag = None # count from tagged data
    p_initial_tag = None # count from tagged data

    hmm_word_cluster = HMM(p_initial_cluster, p_transition_cluster, p_word_given_cluster)
    hmm_cluster_tag = HMM(p_initial_tag, p_transition_tag, p_cluster_given_tag)

    words = []
    clusters = hmm_word_cluster.viterbi(words)
    tags = hmm_cluster_tag.viterbi(clusters)
开发者ID:Sowmith-iiit,项目名称:nlp-ssp,代码行数:34,代码来源:main.py

示例10: main

def main(job_id, params):
    num_runs = 20
    obs_length = 100
    num_states = 2
    num_obs = 2

    # readin hmm indx
    t = 0
    try:
        with open(os.path.join('.', 'hmm_index.txt')) as hmm_index_file:
            t = int(hmm_index_file.read())
        sys.stderr.write("!!!!!!!!!!!!!!!!!!HMM INDEX:  " + str(t) + "   !!!!!!!!!!!!!!!\n")
    except IOError:
        t = 0


    # generate HMM observations
    np.random.seed(0x6b6c26b2)
    seeds = np.random.randint(0x0fffffff, size=num_runs)
    np.random.seed(seeds[t])
    # random hmm
    z_mat, t_mat = random_hmm(num_states, num_obs)
    pi_vec = np.array([1.0 / num_states] * num_states)
    hmm_test = HMM(z_mat, t_mat, pi_vec)
    # random obs trajectory
    obs = hmm_test.generate(obs_length)[np.newaxis,:]

    # calculate log likelihood for input HMM parameters
    z_mat_p_input = np.array([[params['z_mat_p_0'][0], params['z_mat_p_1'][0]]])
    t_mat_p_input = np.array([[params['t_mat_p_0'][0], params['t_mat_p_1'][0]]])
    # pi_vec_input = np.array([params['pi_0'], 1 - params['pi_0']])
    hmm_estimate = make_parameterized_HMM(z_mat_p_input, t_mat_p_input, pi_vec)
    hmm_loglikelihood = hmm_estimate.loglikelihood(obs[0])

    return -hmm_loglikelihood
开发者ID:imoonkey,项目名称:embo,代码行数:35,代码来源:bo.py

示例11: __init__

 def __init__(self, **kwarg):    # lname, url, other prior knowledge
     super(HMMClassifier, self).__init__()
     self.HMMauthor = HMM('author', 2)
     self.HMMvenue = HMM('venue', 2)     # Not important
     self.HMMentire = HMM('entire', 6)   # Set empirically
     self.observations_raw = []
     self.observation_sequences = []
     self.labels = []
开发者ID:xiaoyao1991,项目名称:hmmpy,代码行数:8,代码来源:classifier.py

示例12: TestHMM

class TestHMM():
	
	def __init__(self):
		self.Z = numpy.array([
			[0.8,  0.09, 0.01],
			[0.09, 0.8,  0.01],
			[0.1,  0,    0.8]
		])
		self.b = numpy.array([
			[0.1, 0.1, 0.8],
			[0.05, 0.9, 0.05],
			[0.8, 0.1, 0.1]
		])
		self.pi = numpy.array([0.9,0.05,0.05])
		self.T = 2000
		# we want the errors to be less than 20%
		self.error_threshold = 0.2
	
	def setup(self):	
		self.model = HMM(self.Z,self.b,self.pi)
	
	def gen_states_obs(self):
		states = []
		obsvns = []
		for (s,o) in self.model.gen(self.T):
			states.append(s)
			obsvns.append(o)
		return states, obsvns
	
	def test_init(self):	
		self.model = HMM(self.Z,self.b,self.pi)
		
	def test_gen(self):
		self.setup()
		states = []
		obsvns = []
		for (s,o) in self.model.gen(10):
			states.append(s)
			obsvns.append(o)
		assert len(states) == 10
		assert len(obsvns) == 10
	
	def test_forward_backward(self):
		self.setup()
		states, obsvns = self.gen_states_obs()
		alpha,beta = self.model.forward_backward(obsvns)	
		
		gamma = [a*b/sum(a*b) for a,b in zip(alpha,beta)]
		state_est = numpy.array([numpy.where(g==max(g))[0][0] for g in gamma])
		err = sum(state_est != numpy.array(states))/float(len(states))
		assert err < self.error_threshold
	
	def test_viterbi(self):
		self.setup()
		states, obsvns = self.gen_states_obs()
		state_est = self.model.viterbi(obsvns)		
		err = sum(state_est != numpy.array(states))/float(len(states))
		assert err < self.error_threshold
开发者ID:mikedewar,项目名称:pyLDS,代码行数:58,代码来源:test_HMM.py

示例13: test_simple_hmm_learning

 def test_simple_hmm_learning(self):
     state_seq = [[0, 1, 1, 0, 1, 0, 1, 1], [0, 0, 1, 0]]
     obs_seq = [[0, 0, 1, 1, 0, 0, 0, 1], [0, 1, 0, 0]]
     hmm = HMM(range(2), range(2))
     hmm.learn_from_labeled_data(state_seq, obs_seq)
     print hmm
     eps = 0.00001
     self.assertTrue(max_delta(hmm.initial, [0.750000, 0.250000]) < eps)
     self.assertTrue(max_delta(hmm.transition, [[0.285714, 0.714286], [0.571429, 0.428571]]) < eps)
     self.assertTrue(max_delta(hmm.observation, [[0.625000, 0.375000], [0.625000, 0.375000]]) < eps)
开发者ID:jmdupont,项目名称:Stanford-Machine-Learning-Course,代码行数:10,代码来源:test_hmm.py

示例14: test_hmm

def test_hmm():
    m = HMM(2, 2)
    observations = [[0,0,0,0,0,1,1,1,1,1,0,1,0,0,0,1,0,1,1,1,1],[0,0,0,0,1,0,1,1,0,1,1,0,0,1,0,0,1,1,1,1,0,0,1,0,0]]
    ground = [[0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1],[0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,0,0,0]]
    m.learn(observations, ground, smooth=None)
    trueres = ([0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0], -21.944)
    res = m.viterbi(observations[1])
    assert trueres[0] == res[0]
    print trueres[1]
    print res[1]
    assert abs(trueres[1] - res[1]) < 0.1
开发者ID:howonlee,项目名称:221proj,代码行数:11,代码来源:tests.py

示例15: setUp

 def setUp(self):
     self._model_filename = "hmm_m4n4.pkl"
     self._train_filename = "m4n4.train.data"
     self._num_hidden = 4
     self._num_observ = 4
     transition_matrix = np.random.rand(4, 4)
     observation_matrix = np.random.rand(4, 4)
     hmm = HMM(self._num_hidden, self._num_observ, transition_matrix=transition_matrix,
               observation_matrix=observation_matrix)
     sequences = hmm.generate_data(10000, 4, 51)
     io.save_sequences(self._train_filename, sequences)
     HMM.to_file(self._model_filename, hmm)
开发者ID:DucQuang1,项目名称:Spectral-learning,代码行数:12,代码来源:testHMM.py


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