bigram = (tag1,tag2) transition_probability = tag_bigrams[bigram] / tag_unigrams[tag1] sum_of_probabilities += transition_probability basic_model.add_transition(state1, state2, transition_probability) #============================================================== # finalize the model #============================================================== # NOTE: YOU SHOULD NOT NEED TO MODIFY ANYTHING BELOW THIS LINE basic_model.bake() print("Number of nodes or states: ", basic_model.node_count()) print("Number of edges: ", basic_model.edge_count()) assert all(tag in set(s.name for s in basic_model.states) for tag in data.training_set.tagset), \ "Every state in your network should use the name of the associated tag, which must be one of the training set tags." assert basic_model.edge_count() == 168, \ ("Your network should have an edge from the start node to each state, one edge between every " + "pair of tags (states), and an edge from each state to the end node.") HTML('<div class="alert alert-block alert-success">Your HMM network topology looks good!</div>') #============================================================== # evaluate train / test data set metrics #==============================================================
model.add_transition(model.start, rainy_state, 0.5) # add sunny day transitions (we already know estimates of these probabilities # from the problem statement) model.add_transition(sunny_state, sunny_state, 0.8) # 80% sunny->sunny model.add_transition(sunny_state, rainy_state, 0.2) # 20% sunny->rainy # TODO: add rainy day transitions using the probabilities specified in the transition table model.add_transition(rainy_state, sunny_state, 0.4) # 40% rainy->sunny model.add_transition(rainy_state, rainy_state, 0.6) # 60% rainy->rainy # finally, call the .bake() method to finalize the model model.bake() assert model.edge_count() == 6, "There should be two edges from model.start, two from Rainy, and two from Sunny" assert model.node_count() == 4, "The states should include model.start, model.end, Rainy, and Sunny" print("Great! You've finished the model.") show_model(model, figsize=(5, 5), filename="example.png", overwrite=True, show_ends=False) column_order = ["Example Model-start", "Sunny", "Rainy", "Example Model-end"] # Override the Pomegranate default order column_names = [s.name for s in model.states] order_index = [column_names.index(c) for c in column_order] # re-order the rows/columns to match the specified column order transitions = model.dense_transition_matrix()[:, order_index][order_index, :]
from pomegranate import HiddenMarkovModel, DiscreteDistribution, State import numpy as np model = HiddenMarkovModel(name='weather') sunny_emissions = DiscreteDistribution({'yes': 0.1, 'no': 0.9}) sunny_state = State(sunny_emissions, name='sunny') rainy_emissions = DiscreteDistribution({'yes': 0.8, 'no': 0.2}) rainy_state = State(rainy_emissions, name='rainy') model.add_states(sunny_state, rainy_state) model.add_transition(model.start, sunny_state, 0.5) model.add_transition(model.start, rainy_state, 0.5) model.add_transition(sunny_state, rainy_state, 0.2) model.add_transition(sunny_state, sunny_state, 0.8) model.add_transition(rainy_state, rainy_state, 0.6) model.add_transition(rainy_state, sunny_state, 0.4) model.bake() states = np.array([s.name for s in model.states]) print('{} states: {}'.format(model.node_count(), states)) transitions = model.dense_transition_matrix() print('{} transitions probabilities between states \n{}'.format( model.edge_count(), transitions))