def ml_k_cross_validation(class1_data, class2_data, p1, p2, k, n1, n2): test_results_ml_class1 = [] test_results_ml_class2 = [] accuracies = [] for i in range(0, k, 1): print('Cross:' + str(i + 1)) class1_testing_points_count = int(n1 / k) class1_training_points_count = int(n1 - n1 / k) class1_start = int(n1 * i / k) class1_end = int((i + 1) * n1 / k) class2_testing_points_count = int(n2 / k) class2_training_points_count = int(n2 - n2 / k) class2_start = int(n2 * i / k) class2_end = int((i + 1) * n2 / k) # print("start:", class1_start, "\tend:", class1_end) # print("start:", class2_start, "\tend:", class2_end) class1_test_points = class1_data[:, class1_start: class1_end] class1_train_points = class1_data[:, 0:class1_start] class1_train_points = np.append(class1_train_points, class1_data[:, class1_end:], axis=1) class2_test_points = class2_data[:, class2_start: class2_end] class2_train_points = class2_data[:, 0:class2_start] class2_train_points = np.append(class2_train_points, class2_data[:, class2_end:], axis=1) # estimated mean and cov using ML x1_ml_estimated_mean = ml.estimate_mean_ml(class1_train_points, len(class1_train_points[0])) x1_ml_estimated_cov = ml.estimate_cov_ml(class1_train_points, x1_ml_estimated_mean, class1_training_points_count) x2_ml_estimated_mean = ml.estimate_mean_ml(class2_train_points, len(class2_train_points[0])) x2_ml_estimated_cov = ml.estimate_cov_ml(class2_train_points, x2_ml_estimated_mean, class2_training_points_count) ml_class1_accuracy, ml_class2_accuracy, total_accuracy = test_classifier(class1_test_points, class2_test_points, x1_ml_estimated_cov, x2_ml_estimated_cov, x1_ml_estimated_mean, x2_ml_estimated_mean, class1_testing_points_count, class2_testing_points_count, p1, p2) # print(ml_class1_accuracy, ml_class2_accuracy) test_results_ml_class1 = np.append(test_results_ml_class1, ml_class1_accuracy) test_results_ml_class2 = np.append(test_results_ml_class2, ml_class2_accuracy) accuracies = np.append(accuracies, total_accuracy) print('\nML Average Accuracy:', np.mean(accuracies)) return test_results_ml_class1, test_results_ml_class2
def bl_expected_mean(x1_training_points, x2_training_points, sigma_x1, sigma_x2, m1, m2): w1_plot_points = [[], [], []] w1_plot_points_index = [] w1_mean_values = [[], [], []] w1_mean_difference = [] w1_ml_mean_difference = [] w1_ml_cov_difference = [] sigma0 = np.identity(len(x1_training_points[0])) m1_0 = np.array([[1], [1], [1]]) w2_plot_points = [[], [], []] w2_plot_points_index = [] w2_mean_values = [[], [], []] w2_mean_difference = [] w2_ml_mean_difference = [] w2_ml_cov_difference = [] m2_0 = np.array([[1], [1], [1]]) for n in range(1, len(x1_training_points[0]), 1): # for class 1 w1_plot_points_index = np.append(w1_plot_points_index, n) x1_ml_estimated_mean = ml.estimate_mean_ml(x1_training_points, n) x1_ml_estimated_cov = ml.estimate_cov_ml(x1_training_points, x1_ml_estimated_mean, n) w1_ml_mean_difference = np.append( w1_ml_mean_difference, np.absolute(np.linalg.norm(x1_ml_estimated_mean - m1))) w1_ml_cov_difference = np.append( w1_ml_cov_difference, np.absolute(np.linalg.norm(x1_ml_estimated_cov - sigma_x1))) m1_0 = estimate_mean_bl(x1_training_points, m1_0, sigma0, sigma_x1, n) w1_mean_values = np.append(w1_mean_values, m1_0, axis=1) w1_plot_points = np.append(w1_plot_points, x1_training_points[:, [n]], axis=1) w1_mean_difference = np.append(w1_mean_difference, np.absolute(np.linalg.norm(m1_0 - m1))) for n in range(1, len(x2_training_points[0]), 1): # for class 2 w2_plot_points_index = np.append(w2_plot_points_index, n) x2_ml_estimated_mean = ml.estimate_mean_ml(x2_training_points, n) x2_ml_estimated_cov = ml.estimate_cov_ml(x2_training_points, x2_ml_estimated_mean, n) w2_ml_mean_difference = np.append( w2_ml_mean_difference, np.absolute(np.linalg.norm(x2_ml_estimated_mean - m2))) w2_ml_cov_difference = np.append( w2_ml_cov_difference, np.absolute(np.linalg.norm(x2_ml_estimated_cov - sigma_x2))) m2_0 = estimate_mean_bl(x2_training_points, m2_0, sigma0, sigma_x2, n) w2_mean_values = np.append(w2_mean_values, m2_0, axis=1) w2_plot_points = np.append(w2_plot_points, x2_training_points[:, [n]], axis=1) w2_mean_difference = np.append(w2_mean_difference, np.absolute(np.linalg.norm(m2_0 - m2))) return m1_0, m2_0,
mat_plt.legend(loc=2) mat_plt.grid(linestyle='dotted') mat_plt.show() np.set_printoptions(suppress=True) instances_count = class1_instances_count + class2_instances_count ############################################################################### ############################################################################### """ Create the ML Classifier """ # estimate parameter for both classes class1_ml_est_mean = ml.estimate_mean_ml(class1_data, class1_instances_count) class2_ml_est_mean = ml.estimate_mean_ml(class2_data, class2_instances_count) class1_ml_est_cov = ml.estimate_cov_ml(class1_data, class1_ml_est_mean, class1_instances_count) class2_ml_est_cov = ml.estimate_cov_ml(class2_data, class2_ml_est_mean, class2_instances_count) # print("\nBEFORE DIAGONALIZING:") # print('\nClass 1 mean using ML:') # print(class1_ml_est_mean) # print('\nClass 2 mean using ML:') # print(class2_ml_est_mean) # print('\nClass 1 cov using ML:') # print(class1_ml_est_cov) # print('\nClass 2 cov using ML:') # print(class2_ml_est_cov)
def fd_k_cross_validation(class1_data, class2_data, k, n1, n2, w, p1, p2): test_results_fd_class1 = [] test_results_fd_class2 = [] accuracies = [] for i in range(0, k, 1): print('Cross:' + str(i + 1)) class1_testing_points_count = int(n1 / k) class1_training_points_count = int(n1 - n1 / k) class1_start = int(n1 * i / k) class1_end = int((i + 1) * n1 / k) class2_testing_points_count = int(n2 / k) class2_training_points_count = int(n2 - n2 / k) class2_start = int(n2 * i / k) class2_end = int((i + 1) * n2 / k) class1_test_points = class1_data[:, class1_start: class1_end] class1_train_points = class1_data[:, 0:class1_start] class1_train_points = np.append(class1_train_points, class1_data[:, class1_end:], axis=1) class2_test_points = class2_data[:, class2_start: class2_end] class2_train_points = class2_data[:, 0:class2_start] class2_train_points = np.append(class2_train_points, class2_data[:, class2_end:], axis=1) class1_ml_est_mean = ml.estimate_mean_ml(class1_train_points, len(class1_train_points[0])) class1_ml_est_cov = ml.estimate_cov_ml(class1_train_points, class1_ml_est_mean, class1_training_points_count) class2_ml_est_mean = ml.estimate_mean_ml(class2_train_points, len(class2_train_points[0])) class2_ml_est_cov = ml.estimate_cov_ml(class2_train_points, class2_ml_est_mean, class2_training_points_count) fd_mean1 = w.transpose() @ class1_ml_est_mean fd_mean2 = w.transpose() @ class2_ml_est_mean fd_cov1 = w.transpose() @ class1_ml_est_cov @ w fd_cov2 = w.transpose() @ class2_ml_est_cov @ w class1_test_points = np.array(class1_test_points).transpose() class2_test_points = np.array(class2_test_points).transpose() class1_true = 0 class1_false = 0 class2_true = 0 class2_false = 0 for x in class1_test_points: x_test = w.transpose() @ x classification = fd.classify(x_test, fd_mean1, fd_mean2, fd_cov1, fd_cov2, p1, p2) if classification == 1: class1_true = class1_true + 1 else: class1_false = class1_false + 1 for x in class2_test_points: x_test = w.transpose() @ x classification = fd.classify(x_test, fd_mean1, fd_mean2, fd_cov1, fd_cov2, p1, p2) if classification == 2: class2_true = class2_true + 1 else: class2_false = class2_false + 1 class1_accuracy = (class1_true / len(class1_test_points)) * 100 class2_accuracy = (class2_true / len(class2_test_points)) * 100 test_results_fd_class1 = np.append(test_results_fd_class1, class1_accuracy) test_results_fd_class2 = np.append(test_results_fd_class2, class2_accuracy) accuracy = (class1_true + class2_true) * 100 / (len(class1_test_points) + len(class2_test_points)) accuracies = np.append(accuracies, accuracy) print(accuracy) print('\nFisher\'s Disc. Average Accuracy:', np.mean(accuracies)) return test_results_fd_class1, test_results_fd_class2
def bl_k_cross_validation(class1_data, class2_data, p1, p2, k, n1, n2): test_results_bl_class1 = [] test_results_bl_class2 = [] accuracies = [] for i in range(0, k, 1): print('Cross:' + str(i + 1)) class1_testing_points_count = int(n1 / k) class1_training_points_count = int(n1 - n1 / k) class1_start = int(n1 * i / k) class1_end = int((i + 1) * n1 / k) class2_testing_points_count = int(n2 / k) class2_training_points_count = int(n2 - n2 / k) class2_start = int(n2 * i / k) class2_end = int((i + 1) * n2 / k) # print("start:", class1_start, "\tend:", class1_end) # print("start:", class2_start, "\tend:", class2_end) class1_test_points = class1_data[:, class1_start: class1_end] class1_train_points = class1_data[:, 0:class1_start] class1_train_points = np.append(class1_train_points, class1_data[:, class1_end:], axis=1) class2_test_points = class2_data[:, class2_start: class2_end] class2_train_points = class2_data[:, 0:class2_start] class2_train_points = np.append(class2_train_points, class2_data[:, class2_end:], axis=1) class1_ml_est_mean = ml.estimate_mean_ml(class1_train_points, len(class1_train_points[0])) class1_ml_est_cov = ml.estimate_cov_ml(class1_train_points, class1_ml_est_mean, class1_training_points_count) class2_ml_est_mean = ml.estimate_mean_ml(class2_train_points, len(class2_train_points[0])) class2_ml_est_cov = ml.estimate_cov_ml(class2_train_points, class2_ml_est_mean, class2_training_points_count) # Estimating the means using BL class1_bl_initial_mean = np.ones((len(class1_data), 1)) class1_bl_initial_cov = np.identity(len(class1_data)) class2_bl_initial_mean = np.ones((len(class2_data), 1)) class2_bl_initial_cov = np.identity(len(class2_data)) class1_bl_est_mean = bl.estimate_mean_bl(class1_train_points, class1_bl_initial_mean, class1_bl_initial_cov, class1_ml_est_cov, len(class1_train_points[0])) class2_bl_est_mean = bl.estimate_mean_bl(class2_train_points, class2_bl_initial_mean, class2_bl_initial_cov, class2_ml_est_cov, len(class2_train_points[0])) bl_class1_accuracy, bl_class2_accuracy, total_accuracy = test_classifier(class1_test_points, class2_test_points, class1_ml_est_cov, class2_ml_est_cov, class1_bl_est_mean, class2_bl_est_mean, class1_testing_points_count, class2_testing_points_count, p1, p2) # print(bl_class1_accuracy, bl_class2_accuracy) test_results_bl_class1 = np.append(test_results_bl_class1, bl_class1_accuracy) test_results_bl_class2 = np.append(test_results_bl_class2, bl_class2_accuracy) accuracies = np.append(accuracies, total_accuracy) print('\nBL Average Accuracy:', np.mean(accuracies)) return test_results_bl_class1, test_results_bl_class2