# CRF model model_crf = sklearn_crfsuite.CRF(algorithm='lbfgs', c1=0.25, c2=0.01, max_iterations=100, all_possible_transitions=True) # train the model model_crf.fit(X_train, y_train) labels = list(model_crf.classes_) # preditcion of the labels of the test sequence y_pred = model_crf.predict(X_test) # print output time remarks outputSteps(y_pred[0]) # calculate probability def get_prob(y_prob): prob_actions = [] for i in y_prob: for x in i: prob_actions.append(x[max(x, key=x.get)]) prob_seq = prod(prob_actions) return prob_seq # Probability to model sequence y_prob = model_crf.predict_marginals(X_test) prob_results.append(get_prob(y_prob)) # Probability to model random sequence
# flatten ground_truth of trainingsdata y_train_ground_truth_flatten = [ item for sublist in y_train_ground_truth for item in sublist ] # Gaussian Naive Bayes (GaussianNB) model_bayes = GaussianNB() # training model_bayes.fit(X_train_flatten, y_train_ground_truth_flatten) # state prediction y_pred_bayes = model_bayes.predict(X_test_flatten) # print output time remarks outputSteps(y_pred_bayes) # probability calculation def get_prob(y_prob): prob_actions = [] for i in y_prob: prob_actions.append(max(i)) prob_seq = sum(prob_actions) return prob_seq # Probability to model sequence y_prob = model_bayes.predict_log_proba(X_test_flatten) prob_results.append(math.exp(get_prob(y_prob))) # Probability to model random sequence y_prob_random_seq = model_bayes.predict_log_proba(X_random)
# change type to array seqlearn_X_train = np.array(X_train_flatten) seqlearn_y_ground_truth = np.array(y_ground_truth_flatten) # HMM seqlearn MultimodalHMM model_seqlearn = MultinomialHMM() # training model_seqlearn.fit(seqlearn_X_train, seqlearn_y_ground_truth, len_train) # state prediction y_pred_seqlearn = model_seqlearn.predict(X_test_flatten) # print output time remarks outputSteps(y_pred_seqlearn) #state prediction for random sequence y_pred_seqlearn_random = model_seqlearn.predict(X_random) # state prediction for heuristic sequence y_pred_seqlearn_random = model_seqlearn.predict(X_heuristic) # target names for evaluation target_names = list(step_set) target_names = sorted(target_names) # confusion matrix cnf_matrix = confusion_matrix(y_ground_truth, y_pred_seqlearn) normalize = cnf_matrix.astype('float') / cnf_matrix.sum(axis=1)[:, np.newaxis]