def k_nearest_neighbor_classifier(train_img, train_labels, test_img, test_labels, N_TRAIN, N_TEST, K): confusion_matrix = np.zeros((10, 10), dtype=int) start = time.time() # Iterating through every test image for i in range(N_TEST): distance_vec = [] for j in range(N_TRAIN): d = distance.euclidean(test_img[i], train_img[j]) distance_vec.append(d) #Using k nearest clusters to get prediction prediction = get_majority_vote(distance_vec, train_labels, K) confusion_matrix[test_labels[i]][prediction] += 1 end = time.time() print(f"Runtime of program is {(end-start)/60} minutes.") df_cm = DataFrame( confusion_matrix, index=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], columns=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]) pretty_plot_confusion_matrix( df_cm, title= 'Confusion matrix - 7NN Classifier with clustering - 10000 test 640 clusters', cmap="Oranges", pred_val_axis='x') return
def confusionMatGraph(tn, fp, fn, tp, filepath): array = np.array([[tp, fn], [fp, tn]]) df_cm = pd.DataFrame(array, index=['fraud', 'non-fraud'], columns=['fraud', 'non-fraud']) cmap = 'PuRd' pretty_plot_confusion_matrix(df_cm, cmap=cmap, filepath=filepath)
def confusion_from_numpy(confusion_matrix, save_destination=None): df_cm = pd.DataFrame(confusion_matrix, index=range(1, confusion_matrix.shape[0] + 1), columns=range(1, confusion_matrix.shape[1] + 1)) #colormap: see this and choose your more dear cmap = 'PuRd' pretty_plot_confusion_matrix(df_cm, cmap=cmap) if save_destination is not None: plt.savefig(save_destination) plt.close('all')
def task2_remove_feature(train, test, N_CLASS, N_FEAT, alpha, N_ITER, train_size, test_size): # Remove feature "sepal width" W = np.zeros([N_CLASS, N_FEAT], dtype=float) train = np.delete(train, 1, 1) test = np.delete(test, 1, 1) W, _ = train_linear_classifier(train, W, N_ITER, alpha, train_size) conf1_train = get_confusion_matrix(W, train, N_CLASS, train_size) conf1_test = get_confusion_matrix(W, test, N_CLASS, test_size) # Remove feature "sepal length" W = np.zeros([N_CLASS, N_FEAT - 1], dtype=float) train = np.delete(train, 0, 1) test = np.delete(test, 0, 1) W, _ = train_linear_classifier(train, W, N_ITER, alpha, train_size) conf2_train = get_confusion_matrix(W, train, N_CLASS, train_size) conf2_test = get_confusion_matrix(W, test, N_CLASS, test_size) # Remove feature "petal length" W = np.zeros([N_CLASS, N_FEAT - 2], dtype=float) train = np.delete(train, 0, 1) test = np.delete(test, 0, 1) W, _ = train_linear_classifier(train, W, N_ITER, alpha, train_size) conf3_train = get_confusion_matrix(W, train, N_CLASS, train_size) conf3_test = get_confusion_matrix(W, test, N_CLASS, test_size) # Plotting confusion matrices conf_arr = [[conf1_train, conf1_test], [conf2_train, conf2_test], [conf3_train, conf3_test]] labels = [ "sepal width", "sepal width, sepal length", "sepal width, sepal length, petal length" ] for i, (train, test) in enumerate(conf_arr): df_cm = DataFrame(train, index=["setosa", "versicolor", "virginica"], columns=["setosa", "versicolor", "virginica"]) pretty_plot_confusion_matrix(df_cm, title='Training set - Removed ' + str(labels[i]), cmap="Oranges", pred_val_axis='x') df_cm = DataFrame(test, index=["setosa", "versicolor", "virginica"], columns=["setosa", "versicolor", "virginica"]) pretty_plot_confusion_matrix(df_cm, title='Test set - Removed ' + str(labels[i]), cmap="Oranges", pred_val_axis='x') return
def nearest_neighbor_classifier(train_img, train_labels, test_img, test_labels, N_TRAIN, N_TEST): correct_pred = [] # Array of set of indeces of correct predictions failed_pred = [] # Array of set of indeces of incorrect predictions confusion_matrix = np.zeros((10, 10), dtype=int) start = time.time() # Iterating through every test image for i in range(N_TEST): prediction = 0 pred_img_index = 0 min = float('inf') # Comparing distance of test image to every training image for j in range(N_TRAIN): d = distance.euclidean(test_img[i], train_img[j]) #d = euclid_distance(test_img[i], train_img[j]) if d < min: min = d pred_img_index = j prediction = train_labels[j] if prediction == test_labels[i]: correct_pred.append([i, pred_img_index]) confusion_matrix[test_labels[i]][test_labels[i]] += 1 else: confusion_matrix[test_labels[i]][prediction] += 1 failed_pred.append([i, pred_img_index]) end = time.time() print(f"Runtime of program is {(end-start)/60} minutes.") df_cm = DataFrame( confusion_matrix, index=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], columns=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]) pretty_plot_confusion_matrix( df_cm, title= 'Confusion matrix - 1NN Classifier without clustering - 10000 test 60000 train', cmap="Oranges", pred_val_axis='x') return
def main(): ### Task 1 ### #Generate empty weights W = np.zeros([N_CLASS, N_FEAT + 1], dtype=float) # Train linear classifier W, _ = train_linear_classifier(train, W, N_ITER, alpha, train_size) # Compute confusion matrices confusion_train = get_confusion_matrix(W, train, N_CLASS, train_size) confusion_test = get_confusion_matrix(W, test, N_CLASS, test_size) # Plot confusion matrix for training set df_cm = DataFrame(confusion_train, index=["setosa", "versicolor", "virginica"], columns=["setosa", "versicolor", "virginica"]) pretty_plot_confusion_matrix(df_cm, title='Training set', cmap="Oranges", pred_val_axis='x') # Plot confusion matrix for test set df_cm = DataFrame(confusion_test, index=["setosa", "versicolor", "virginica"], columns=["setosa", "versicolor", "virginica"]) pretty_plot_confusion_matrix(df_cm, title='Test set', cmap="Oranges", pred_val_axis='x') ### Task 2 ### task2_remove_feature(train, test, N_CLASS, N_FEAT, alpha, N_ITER, train_size, test_size) return
plt.title('Wnut Loss BiLSTM-CRF') plt.savefig('results/wnut_loss_curve.png') # plt.show() if not parameters['to_train']: # reload the best model saved from training model.load_state_dict(torch.load(model_name)) model.train(False) _, _, _, conf_matrix = evaluating(model, test_data, 0, len(tag_to_id), "Test", True) list_of_labels = list(id_to_tag.values())[:-2] list_of_labels.append('Total') df_cm = DataFrame(conf_matrix) pretty_plot_confusion_matrix(df_cm, list_of_labels, 'results/wnut_conf_matrix.png', cmap='Oranges') # parameters lower = parameters['lower'] # prediction predictions = [] print("Prediction:") print("word : tag") count = 0 for data in test_data: words = data['str_words'] chars2 = data['chars'] ground_truth = data['tags'] d = {}
def plot_CM(_y_cm, _y_predict, _class_names, _title): cm = confusion_matrix(_y_cm, _y_predict) cm = cm.tolist() df_cm = pd.DataFrame(cm, index=[i for i in _class_names], columns=[i for i in _class_names]) pretty_plot_confusion_matrix(df_cm,_title=_title)
class2index = valid_generator.class_indices index2class = dict((v, k) for k, v in class2index.items()) labels = list(index2class.values()) true_index = valid_generator.classes true_label = list(map(lambda x: index2class[x], true_index)) pred_index = np.argmax(pred_probs, axis=1) pred_label = list(map(lambda x: index2class[x], pred_index)) conf_mat = confusion_matrix(true_label, pred_label, labels=labels) cm_df = pd.DataFrame(conf_mat, index=labels, columns=labels) confusion_matrix_pretty_print.pretty_plot_confusion_matrix(cm_df, cmap='PuRd', cbar=True, show_null_values=2, fz=9, figsize=[6, 5]) #%% Plot result fig = plt.figure(figsize=(8, 3)) ax1 = plt.subplot(1, 2, 1) ax1.plot(history.history['acc'], marker='.') ax1.plot(history.history['val_acc'], marker='.') ax1.set_title('Model accuracy') ax1.set_ylabel('Accuracy') ax1.set_xlabel('Epoch') ax1.grid(color='#CCCCCC00') ax1.legend(['Train', 'Test'], loc='upper left')
import numpy as np import matplotlib.pyplot as plt import matplotlib.font_manager as fm from matplotlib.collections import QuadMesh import seaborn as sn sys.path.insert(0, r".\pretty-print-confusion-matrix-master") from confusion_matrix_pretty_print import pretty_plot_confusion_matrix file = os.path.join(r'E:\sampleData4test', "confusion_matrices.mat") x = loadmat(file) #'blockFusion_M', 'blockS1_M', 'blockS2_M', 'randomFusion_M','randomS1_M', 'randomS2_M', 'c10_fusion_M', 'c10_S1_M', 'c10_S2_M' for saveName in {'blockFusion_M', 'randomFusion_M', 'c10_fusion_M'}: #saveName = 'randomFusion_M' randomFusion_M = x[saveName] #print(randomFusion_M.shape) #method 1: summary included # get pandas dataframe df_cm = DataFrame(randomFusion_M, index=range(1, 18), columns=range(1, 18)) cmap = 'Blues'#'PuRd' pretty_plot_confusion_matrix(df_cm, cmap=cmap, fz=64, figsize=[50, 50], pred_val_axis='x', file='E:/sampleData4test/'+saveName + '_.png') #method 2: normal #target_names = ['1','2','3','4','5','6','7','8','9','10','A','B','C','D','E','F','G'] #plot_confusion_matrix(randomFusion_M, target_names, file=saveName + '.png')#, normalize=False