def main(): #-------------------------------------------------------------------------- # 2-state Markov chain #-------------------------------------------------------------------------- P = np.array([[0.8, 0.2], [0.1, 0.9]]) # Transition matrix mc = MarkovChain(P, ['1', '2']) mc.draw("../img/markov-chain-two-states.png") #-------------------------------------------------------------------------- # 3-state Markov chain #-------------------------------------------------------------------------- P = np.array([ [0.8, 0.1, 0.1], [0.1, 0.7, 0.2], [0.1, 0.7, 0.2], ]) mc = MarkovChain(P, ['A', 'B', 'C']) mc.draw("../img/markov-chain-three-states.png") #-------------------------------------------------------------------------- # 4-state Markov chain #-------------------------------------------------------------------------- P = np.array([[0.8, 0.1, 0.1, 0.0], [0.1, 0.7, 0.0, 0.2], [0.1, 0.0, 0.7, 0.2], [0.1, 0.0, 0.7, 0.2]]) mc = MarkovChain(P, ['1', '2', '3', '4']) mc.draw("../img/markov-chain-four-states.png")
def __init__(self): path_ini = os.getcwd()+'/resources/models/mc_model/mc_initial.npy' path_trans = os.getcwd()+'/resources/models/mc_model/mc_transition.npy' trans = np.load(path_trans) pi = np.load(path_ini) self.model = MarkovChain(transition=trans, initial=pi, states=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18])
def plot_graph(self, mk_prop, Labels): P = np.array([mk_prop]) # Transition matrix mc = MarkovChain(P, Labels) mc.draw()
("sjor", "data/usjavarornefni.txt", 3), ("sveit", "data/usveit.txt", 3), ("borg", "data/uthettbyli.txt", 3), ("vatn", "data/uvatnaornefni.txt", 4), ("land", "data/ulandornefni.txt", 4), ("jokull", "data/ujoklaornefni.txt", 3), ("kvk", "data/ukvknofn.txt", 3), ("kk", "data/ukknofn.txt", 3), ("milli", "data/umillinofn.txt", 3), ("gata", "data/ugotunofn.txt", 4), ("hus", "data/uhusanofn.txt", 3), ] CHAINS = {} for name, fname, order in name_files: mc = MarkovChain(order=order, analyzer="char") mc.fit(fname) CHAINS[name] = mc mc = MarkovChain(order=order, analyzer="char") mc.fit(fname, reversed=True) CHAINS[name + "_r"] = mc NAMES = {} for chain, fname, _ in name_files: NAMES[chain] = set() with open(fname) as f: for name in f.readlines(): NAMES[chain].add(name.strip()) def is_bool(value):
if _ == (0, 0): pass else: start += step_size stop += step_size # Slice sessions @sessions(None, None) next_step = input.sessions(start, stop) if _ >= (1, 1): past_start = (start-step_size) past_stop = (stop-step_size) past_step = input.sessions(past_start, past_stop) # Compute transition matrix next mc_next = MarkovChain(next_step, states) mc_next = mc_next.csr_sparse_matrix() # Compute transition matrix past mc_past = MarkovChain(past_step, states) mc_past = mc_past.csr_sparse_matrix() print('matrix done', datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')) print('start clustering') # DBSCAN dbscan_next = Clustering(mc_next) dbscan_past = Clustering(mc_past) print("End clustering", datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'), '\n') """