def test_fit(self, params='ste', n_iter=15, verbose=False, **kwargs): np.random.seed(0) h = hmm.MultinomialHMM(self.n_components, startprob=self.startprob, transmat=self.transmat) h.emissionprob = self.emissionprob # Create training data by sampling from the HMM. train_obs = [h.rvs(n=10) for x in xrange(10)] # Mess up the parameters and see if we can re-learn them. h.startprob = hmm.normalize(self.prng.rand(self.n_components)) h.transmat = hmm.normalize(self.prng.rand(self.n_components, self.n_components), axis=1) h.emissionprob = hmm.normalize(self.prng.rand(self.n_components, self.n_symbols), axis=1) trainll = train_hmm_and_keep_track_of_log_likelihood(h, train_obs, n_iter=n_iter, params=params, **kwargs)[1:] if not np.all(np.diff(trainll) > 0) and verbose: print print 'Test train: (%s)\n %s\n %s' % (params, trainll, np.diff(trainll)) self.assertTrue(np.all(np.diff(trainll) > 0))
def __call__(self): hmmdata = dict() for (name, tracks, probs, finfo) in \ self.dtable.iterby(self.ecopts.sortby, True): if tracks is probs is finfo is None: hmmdata[name] = None continue est = self._get_estimator(probs, tracks) est.constrain(self.hmmc(est)) # ugly sklearn hmm_ = hmm.MultinomialHMM(n_components=est.nstates) hmm_._set_startprob(est.startprob) hmm_._set_transmat(est.trans) hmm_._set_emissionprob(est.emis) # trackwise error correction tracks2 = [] for track in self.classdef.label2index(tracks): tracks2.append(hmm_.predict(track)) tracks2 = self.classdef.index2labels(np.array(tracks2, dtype=int)) bucket = HmmBucket(tracks, tracks2, est.startprob, est.emis, est.trans, self.dtable.groups(self.ecopts.sortby, name), tracks.shape[0], self.ecopts.timelapse, finfo) hmmdata[name] = bucket return hmmdata
def test_attributes(self): h = hmm.MultinomialHMM(self.n_components) self.assertEquals(h.n_components, self.n_components) h.startprob = self.startprob assert_array_almost_equal(h.startprob, self.startprob) self.assertRaises(ValueError, h._set_startprob, 2 * self.startprob) self.assertRaises(ValueError, h._set_startprob, []) self.assertRaises(ValueError, h._set_startprob, np.zeros((self.n_components - 2, self.n_symbols))) h._set_transmat(self.transmat) assert_array_almost_equal(h._get_transmat(), self.transmat) self.assertRaises(ValueError, h._set_transmat, 2 * self.transmat) self.assertRaises(ValueError, h._set_transmat, []) self.assertRaises(ValueError, h._set_transmat, np.zeros((self.n_components - 2, self.n_components))) h._set_emissionprob(self.emissionprob) assert_array_almost_equal(h._get_emissionprob(), self.emissionprob) self.assertRaises(ValueError, h._set_emissionprob, []) self.assertRaises(ValueError, h._set_emissionprob, np.zeros((self.n_components - 2, self.n_symbols))) self.assertEquals(h.n_symbols, self.n_symbols)
def test_attributes(self): h = hmm.MultinomialHMM(self.n_components) self.assertEquals(h.n_components, self.n_components) h.startprob_ = self.startprob assert_array_almost_equal(h.startprob_, self.startprob) self.assertRaises(ValueError, h.__setattr__, 'startprob_', 2 * self.startprob) self.assertRaises(ValueError, h.__setattr__, 'startprob_', []) self.assertRaises(ValueError, h.__setattr__, 'startprob_', np.zeros((self.n_components - 2, self.n_symbols))) h.transmat_ = self.transmat assert_array_almost_equal(h.transmat_, self.transmat) self.assertRaises(ValueError, h.__setattr__, 'transmat_', 2 * self.transmat) self.assertRaises(ValueError, h.__setattr__, 'transmat_', []) self.assertRaises(ValueError, h.__setattr__, 'transmat_', np.zeros((self.n_components - 2, self.n_components))) h.emissionprob_ = self.emissionprob assert_array_almost_equal(h.emissionprob_, self.emissionprob) self.assertRaises(ValueError, h.__setattr__, 'emissionprob_', []) self.assertRaises(ValueError, h.__setattr__, 'emissionprob_', np.zeros((self.n_components - 2, self.n_symbols))) self.assertEquals(h.n_symbols, self.n_symbols)
def train(trees): corpus = [list(t.tokens) for t in trees] words = list(sorted(set([t.word for c in corpus for t in c]))) tags = list(sorted(set([t.tag for c in corpus for t in c]))) word_index = _index_positions(words) tag_index = _index_positions(tags) transition = np.zeros((len(tags), len(tags))) start = np.zeros((len(tags),)) emission = np.zeros((len(tags), len(words)+1)) for s in corpus: for i,t in enumerate(s): tag_ix = tag_index[t.tag] word_ix = word_index[t.word] emission[tag_ix, word_ix] += 1 if i == 0: start[tag_ix] += 1 else: transition[tag_index[s[i-1].tag], tag_ix] += 1 start /= start.sum() normalize_rows = lambda x: (x.T / x.sum(axis=1)).T transition = normalize_rows(transition+1) emission = normalize_rows(emission+1) model = hmm.MultinomialHMM(n_components=len(tags), transmat=transition, startprob=start) model.emissionprob_ = emission return HMMTagger(words, tags, model)
def test_fit_with_init(self, params='ste', n_iter=5, verbose=False, **kwargs): h = self.h learner = hmm.MultinomialHMM(self.n_components) # Create training data by sampling from the HMM. train_obs = [h.sample(n=10)[0] for x in xrange(10)] # use init_function to initialize paramerters learner._init(train_obs, params) trainll = train_hmm_and_keep_track_of_log_likelihood(learner, train_obs, n_iter=n_iter, params=params, **kwargs)[1:] # Check that the loglik is always increasing during training if not np.all(np.diff(trainll) > 0) and verbose: print print 'Test train: (%s)\n %s\n %s' % (params, trainll, np.diff(trainll)) self.assertTrue(np.all(np.diff(trainll) > -1.e-3))
def test_decode_map_algorithm(self): observations = [0, 1, 2] h = hmm.MultinomialHMM(self.n_components, startprob=self.startprob, transmat=self.transmat, algorithm="map",) h.emissionprob_ = self.emissionprob logprob, state_sequence = h.decode(observations) assert_array_equal(state_sequence, [1, 0, 0])
def test_rvs(self, n=1000): h = hmm.MultinomialHMM(self.n_components, startprob=self.startprob, transmat=self.transmat) h.emissionprob = self.emissionprob samples = h.rvs(n) self.assertEquals(len(samples), n) self.assertEquals(len(np.unique(samples)), self.n_symbols)
def learn(obs, n_states=10): i2t, t2i = token_features(obs) tokens_i = list(map(t2i, obs)) model = hmm.MultinomialHMM(n_states, n_iter=20) model.fit(tokens_i) return model, i2t, t2i
def setUp(self): self.prng = np.random.RandomState(9) self.n_components = 2 # ('Rainy', 'Sunny') self.n_symbols = 3 # ('walk', 'shop', 'clean') self.emissionprob = [[0.1, 0.4, 0.5], [0.6, 0.3, 0.1]] self.startprob = [0.6, 0.4] self.transmat = [[0.7, 0.3], [0.4, 0.6]] self.h = hmm.MultinomialHMM(self.n_components, startprob=self.startprob, transmat=self.transmat) self.h.emissionprob_ = self.emissionprob
def test_eval(self): h = hmm.MultinomialHMM(self.n_components, startprob=self.startprob, transmat=self.transmat) h.emissionprob = self.emissionprob idx = np.repeat(range(self.n_components), 10) nobs = len(idx) obs = [int(x) for x in np.floor(self.prng.rand(nobs) * self.n_symbols)] ll, posteriors = h.eval(obs) self.assertEqual(posteriors.shape, (nobs, self.n_components)) assert_array_almost_equal(posteriors.sum(axis=1), np.ones(nobs))
def f(marks,prb): if len(marks)!=1: print "Error" else: #states = ["A+","A","A-", "B+","B","B-","C+","C","C-", "F"] states = ["A","B","C", "F"] n_states = len(states) observations = ["Sub1:A+","Sub1:A","Sub1:A-", "Sub1:B+", "Sub1:B", "Sub1:B-", "Sub1:C+", "Sub1:C", "Sub1:C_","Sub1:F"] n_observations = len(observations) #Starting proberbilities start_probability = np.array([0.25, 0.25,0.25, 0.25]) #Transision proberbilities transition_probability = np.array([[0.4, 0.3,0.2, 0.1],[0.3, 0.4,0.2, 0.1],[0.1, 0.3,0.4, 0.2],[0.1, 0.2,0.3, 0.4]]) # Emission proberbilities are read from file emission_probability = np.array(prb) model = hmm.MultinomialHMM(n_components=n_states) model._set_startprob(start_probability) model._set_transmat(transition_probability) model._set_emissionprob(emission_probability) bob_says = marks #bob_says=[0] logprob, pred = model.decode(bob_says, algorithm="viterbi") # Display the result print "Previous Exam Result:", ", ".join(map(lambda x: observations[x], bob_says)) print "Predicted Software Engineering Result:", ", ".join(map(lambda x: states[x], pred)) ''' If C and F values are not get as a predicted then student is displayed as approved ''' if (pred[0]!=3)&(pred[0]!=2): con="Student Approved" else:con="Not Sufficient Requirement" # Create GUI window = Tk() window.configure(background='#8A002E') window.title('Margov Prediction') window.geometry('480x200') # Size 200, 200 frame = Frame(window,background='#8A002E') frame.pack() def back(): window.withdraw() Label(frame, background='#8A002E',font=("Helvetica", 14),foreground="white",text="Student's predicted success:").grid(row=0, column=0) Label(frame, background='#8A002E',font=("Helvetica", 14),foreground="white",text=map(lambda x: states[x], pred)).grid(row=0, column=1) Label(frame,background='#8A002E', text=con,fg="red",font=("Helvetica", 20)).grid(row=1, column=0) Button(frame, text='Next', command=back).grid(row=2, column=2, sticky=W, pady=4) return pred[0]
def test_wikipedia_viterbi_example(self): # From http://en.wikipedia.org/wiki/Viterbi_algorithm: # "This reveals that the observations ['walk', 'shop', 'clean'] # were most likely generated by states ['Sunny', 'Rainy', # 'Rainy'], with probability 0.01344." observations = [0, 1, 2] h = hmm.MultinomialHMM(self.n_components, startprob=self.startprob, transmat=self.transmat) h.emissionprob = self.emissionprob logprob, state_sequence = h.decode(observations) self.assertAlmostEqual(np.exp(logprob), 0.01344) assert_array_equal(state_sequence, [1, 0, 0])
def absolute_train(self, data): #save the data so that we may use it some other time (e.g. visualization) self.data = data #gather the codebook from VQ.py module self.codebook = VQModel.get_codebook(data) #quantize the data according to the codebook quantized_value = quantize(self.codebook, data) #Now train the HMM #WARNING: The number of components have to be researched. self.hmm = hmm.MultinomialHMM(n_components=5) self.hmm.fit([quantized_value]) return
def f(marks): if len(marks) != 4: print "Error" else: states = ["pass", "fail"] n_states = len(states) observations = [ "OOP CA Passed", "OOP CA Failed", "OOP Exam Passed", "OOP Exam Failed", "OOSD CA Passed", "OOSD CA Failed", "OOSD Exam Passed", "OOSD Exam Failed" ] n_observations = len(observations) start_probability = np.array([0.5, 0.5]) transition_probability = np.array([[0.7, 0.3], [0.3, 0.7]]) emission_probability = np.array( [[0.0, 0.0, 0.1, 0.1, 0.1, 0.1, 0.4, 0.2], [0.0, 0.05, 0.1, 0.2, 0.1, 0.1, 0.05, 0.4]]) model = hmm.MultinomialHMM(n_components=n_states) model._set_startprob(start_probability) model._set_transmat(transition_probability) model._set_emissionprob(emission_probability) bob_says = marks logprob, pred = model.decode(bob_says, algorithm="viterbi") prob = 0 if pred[0] == 0: prob = prob + 1 if pred[1] == 0: prob = prob + 1 if pred[2] == 0: prob = prob + 1 if pred[3] == 0: prob = prob + 1 print "Persentage of Passing : ", float( (float(prob) / 4) * float(100)), "%" print "Previous Exam Result:", ", ".join( map(lambda x: observations[x], bob_says)) print "Predicted Software Engineering Result:", ", ".join( map(lambda x: states[x], pred)) return float((float(prob) / 4) * float(100))
import numpy as np from sklearn import hmm prng = np.random.RandomState(9) ############################################################## n_components = 3 # ('Urna A', 'Urna B', 'Urna C') n_symbols = 3 # ('Bola Red', 'Bola Blue', 'Bola Green') emissionprob = [[0.34, 0.33, 0.33], [0.4, 0.55, 0.05], [0.05, 0.55, 0.4]] startprob = [0.34, 0.33, 0.33] transmat = [[0.34, 0.33, 0.33], [0.9, 0.05, 0.05], [0.9, 0.05, 0.05]] h = hmm.MultinomialHMM(n_components, startprob=startprob, transmat=transmat) h.emissionprob = emissionprob observations = [1, 1, 2, 2, 1, 0, 1, 2, 2, 0] h.fit(observations, n_iter=2) print h.startprob
def f(marks, prb): if len(marks) != 3: print "Error" else: states = ["A", "B", "C", "F"] n_states = len(states) observations = [ "sub1-A", "sub1-B", "sub1-C", "sub1-D", "sub2-A", "sub2-B", "sub2-C", "sub2-D", "sub3-A", "sub3-B", "sub3-C", "sub3-D" ] #observations = ["OOP-A", "OOP-B", "OOP-C","OOP-D"] n_observations = len(observations) start_probability = np.array([0.25, 0.25, 0.25, 0.25]) transition_probability = np.array([[0.4, 0.3, 0.2, 0.1], [0.3, 0.4, 0.2, 0.1], [0.1, 0.3, 0.4, 0.2], [0.1, 0.2, 0.3, 0.4]]) #emission_probability = np.array([[0.2, 0.1,0.1, 0.1,0.2, 0.1,0.1, 0.1],[0.2, 0.1,0.1, 0.1,0.2, 0.1,0.1, 0.1],[0.2, 0.1,0.1, 0.1,0.2, 0.1,0.1, 0.1],[0.2, 0.1,0.1, 0.1,0.2, 0.1,0.1, 0.1]]) #emission_probability = np.array([[0.4, 0.2,0.2, 0.2],[0.4, 0.2,0.2, 0.2],[0.4, 0.2,0.2, 0.2],[0.4, 0.2,0.2, 0.2]]) emission_probability = np.array(prb) model = hmm.MultinomialHMM(n_components=n_states) model._set_startprob(start_probability) model._set_transmat(transition_probability) model._set_emissionprob(emission_probability) bob_says = marks logprob, pred = model.decode(bob_says, algorithm="viterbi") print "Previous Exam Result:", ", ".join( map(lambda x: observations[x], bob_says)) print "Predicted Software Engineering Result:", ", ".join( map(lambda x: states[x], pred)) if (pred[0] != 3) & (pred[1] != 3) & (pred[0] != 2) & ( pred[1] != 2) & (pred[2] != 3) & (pred[2] != 2): con = "Student Approved" else: con = "Not Sufficient Requirenment" window = Tk() window.configure(background='#8A002E') window.title('Margov Prediction') window.geometry('380x180') # Size 200, 200 frame = Frame(window, background='#8A002E') frame.pack() Label(frame, background='#8A002E', font=("Helvetica", 14), foreground="white", text="Student's predicted success:").grid(row=0, column=0) Label(frame, background='#8A002E', font=("Helvetica", 14), foreground="white", text=", ".join(map(lambda x: states[x], pred))).grid(row=0, column=1) Label(frame, background='#8A002E', text=con, fg="red", font=("Helvetica", 24)).grid(row=1, column=0) Button(frame, text='Quit', command=window.quit).grid(row=2, column=2, sticky=W, pady=4) return 0
n_states = len(states) observations = list_day n_observations = len(observations) start_probability = np.array(start_prob_member) # has nothing to do with users #print "start=", start_probability transition_probability = np.array(trans_probality) # has nothing to do with users #print "transition", transition_probability emission_probability = np.array(emission_probab) # here there are users involved #print "emmission", emission_probability model = hmm.MultinomialHMM(n_components=n_states) model._set_startprob(start_probability) model._set_transmat(transition_probability) model._set_emissionprob(emission_probability) # here there are users involved # predict a sequence of hidden states based on visible states LIST_OF_DICT_FOR_PANDAS_DATAFRAME = [] for y in range(0,MAX_USER_ID): user_id = y+1 #print "\n\nUser " , user_id ; for x in range (1,7): DICT_FOR_PANDAS_DATAFRAME = {}
# -*- coding: utf-8 -*- """ Created on Fri Apr 15 15:59:17 2016 @author: gong @description:这是HMM模型 """ #from sklearn.hmm import GaussianHMM from sklearn import hmm X = [0, 1, 2, 1, 1, 2, 0, 3, 9] model = hmm.MultinomialHMM(n_components=4) model.n_symbols = 10 model.fit([X], n_iter=100) print model.predict(X) #print model.eval(X)
def f(marks, prb): if len(marks) != 2: print "Error" else: print "hidden" states = ["A", "B", "C", "F"] n_states = len(states) observations = [ "Sub1:A+", "Sub1:A", "Sub1:A-", "Sub1:B+", "Sub1:B", "Sub1:B-", "Sub1:C+", "Sub1:C", "Sub1:C_", "Sub1:F", "Sub2:A+", "Sub2:A", "Sub2:A-", "Sub2:B+", "Sub2:B", "Sub2:B-", "Sub2:C+", "Sub2:C", "Sub2:C_", "Sub2:F" ] n_observations = len(observations) #Starting proberbilities start_probability = np.array([0.25, 0.25, 0.25, 0.25]) #Transision proberbilities transition_probability = np.array([[0.4, 0.3, 0.2, 0.1], [0.3, 0.4, 0.2, 0.1], [0.1, 0.3, 0.4, 0.2], [0.1, 0.2, 0.3, 0.4]]) # Emission proberbilities are read from file emission_probability = np.array(prb) model = hmm.MultinomialHMM(n_components=n_states) model._set_startprob(start_probability) model._set_transmat(transition_probability) model._set_emissionprob(emission_probability) bob_says = marks logprob, pred = model.decode(bob_says, algorithm="viterbi") ''' Create the mean value for general prediction ''' val1 = 0 val2 = 0 totval = 0 if (pred[0] == 0): val1 = 85 if (pred[0] == 1): val1 = 65 if (pred[0] == 2): val1 = 47.5 if (pred[0] == 3): val1 = 20 if (pred[1] == 0): val2 = 85 if (pred[1] == 1): val2 = 65 if (pred[1] == 2): val2 = 47.5 if (pred[1] == 3): val2 = 20 totval = (val1 + val2) / 2 if (totval > 70): gr = "A" elif (totval > 55): gr = "B" elif (totval > 40): gr = "C" else: gr = "F" #If C and F values are not get as a predicted then student is displayed as approved print "Previous Exam Result:", ", ".join( map(lambda x: observations[x], bob_says)) print "Predicted Software Engineering Result:", ", ".join( map(lambda x: states[x], pred)) if (pred[0] != 3) & (pred[1] != 3) & (pred[0] != 2) & (pred[1] != 2): con = "Student Approved" else: con = "Not Sufficient Requirement" #GUI Preparation window = Tk() window.configure(background='#8A002E') window.title('Margov Prediction') window.geometry('480x200') # Size 200, 200 frame = Frame(window, background='#8A002E') frame.pack() #Call Back Function to the main window def back(): window.withdraw() Label(frame, background='#8A002E', font=("Helvetica", 12), foreground="white", text="Student's predicted success:").grid(row=0, column=0) Label(frame, background='#8A002E', font=("Helvetica", 12), foreground="white", text=", ".join(map(lambda x: states[x], pred))).grid(row=0, column=1) Label(frame, background='#8A002E', text="Overall Grade is :", fg="white", font=("Helvetica", 16)).grid(row=1, column=0) Label(frame, background='#8A002E', text=gr, fg="white", font=("Helvetica", 16)).grid(row=1, column=1) Label(frame, background='#8A002E', text=con, fg="red", font=("Helvetica", 20)).grid(row=2, column=0) Button(frame, text='Next', command=back).grid(row=3, column=2, sticky=W, pady=4) #Return Grade return gr
def f(marks,prb): if len(marks)!=2: print "Error" else: print "hidden" states = ["A", "B","C", "F"] n_states = len(states) observations = ["Sub1:A+","Sub1:A","Sub1:A-", "Sub1:B+", "Sub1:B", "Sub1:B-", "Sub1:C+", "Sub1:C", "Sub1:C_","Sub1:F","Sub2:A+","Sub2:A","Sub2:A-", "Sub2:B+", "Sub2:B", "Sub2:B-", "Sub2:C+", "Sub2:C", "Sub2:C_","Sub2:F"] #observations = ["OOP-A", "OOP-B", "OOP-C","OOP-D"] n_observations = len(observations) start_probability = np.array([0.25, 0.25,0.25, 0.25]) transition_probability = np.array([[0.4, 0.3,0.2, 0.1],[0.3, 0.4,0.2, 0.1],[0.1, 0.3,0.4, 0.2],[0.1, 0.2,0.3, 0.4]]) #emission_probability = np.array([[0.2, 0.1,0.1, 0.1,0.2, 0.1,0.1, 0.1],[0.2, 0.1,0.1, 0.1,0.2, 0.1,0.1, 0.1],[0.2, 0.1,0.1, 0.1,0.2, 0.1,0.1, 0.1],[0.2, 0.1,0.1, 0.1,0.2, 0.1,0.1, 0.1]]) #emission_probability = np.array([[0.4, 0.2,0.2, 0.2],[0.4, 0.2,0.2, 0.2],[0.4, 0.2,0.2, 0.2],[0.4, 0.2,0.2, 0.2]]) emission_probability = np.array(prb) model = hmm.MultinomialHMM(n_components=n_states) model._set_startprob(start_probability) model._set_transmat(transition_probability) model._set_emissionprob(emission_probability) bob_says = marks logprob, pred = model.decode(bob_says, algorithm="viterbi") val1=0 val2=0 totval=0 if(pred[0]==0): val1=85 if(pred[0]==1): val1=65 if(pred[0]==2): val1=47.5 if(pred[0]==3): val1=20 if(pred[1]==0): val2=85 if(pred[1]==1): val2=65 if(pred[1]==2): val2=47.5 if(pred[1]==3): val2=20 totval=(val1+val2)/2 if (totval>70): gr=0 elif (totval>55): gr=1 elif (totval>40): gr=2 else: gr=3 print "Previous Exam Result:", ", ".join(map(lambda x: observations[x], bob_says)) print "Predicted Software Engineering Result:", ", ".join(map(lambda x: states[x], pred)) if (pred[0]!=3)&(pred[1]!=3)&(pred[0]!=2)&(pred[1]!=2): con="Student Approved" else:con="Not Sufficient Requirenment" window = Tk() window.configure(background='#8A002E') window.title('Margov Prediction') window.geometry('450x200') # Size 200, 200 frame = Frame(window,background='#8A002E') frame.pack() Label(frame, background='#8A002E',font=("Helvetica", 14),foreground="white",text="Student's predicted success:").grid(row=0, column=0) Label(frame, background='#8A002E',font=("Helvetica", 14),foreground="white",text=", ".join(map(lambda x: states[x], pred))).grid(row=0, column=1) Label(frame,background='#8A002E', text=con,fg="red",font=("Helvetica", 24)).grid(row=1, column=0) Button(frame, text='Quit', command=window.quit).grid(row=2, column=2, sticky=W, pady=4) return gr
def hmm_model_fit(obs): model = hmm.MultinomialHMM(n_components=2) model.fit([obs]) return model
def test_set_emissionprob(self): h = hmm.MultinomialHMM(self.n_components) emissionprob = np.array([[0.8, 0.2, 0.0], [0.7, 0.2, 1.0]]) h.emissionprob = emissionprob assert np.allclose(emissionprob, h.emissionprob)