def plot_confusion_matrix(test_data, predicted_data, normalize=False, figure=None, subplot_indices=224): classes = np.unique(test_data) # compute confusion matrix cm = confusion_matrix(test_data, predicted_data) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Showing normalized confusion matrix") else: print('Showing confusion matrix, without normalization') # print(cm) if figure is not None: figure.add_subplot(subplot_indices) plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues) plt.title('Confusion matrix') plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=90) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") # plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label')
def plot_confusion_matrix(cm, classes, normalize=False, title="Confusion matrix", cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ fig = plt.figure(figsize=(10, 8)) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel("True label") plt.xlabel("Predicted label") plt.savefig("output/nicr/confusion.png") plt.close()
def plot_confusion_matrix_(self, cm, normalize=False, title='Confusion matrix'): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ classes = ['Non-sites', 'Sites'] general_plot_strings = [] general_plot_strings.append(self.form_plot_string('plt.imshow', cm, interpolation='nearest', cmap="YlGnBu")) general_plot_strings.append(self.form_plot_string('plt.colorbar')) tick_marks = np.arange(len(classes)) general_plot_strings.append(self.form_plot_string('plt.xticks', tick_marks, classes, rotation=45)) general_plot_strings.append(self.form_plot_string('plt.yticks', tick_marks, classes)) general_plot_strings.append(self.form_plot_string('plt.title', title)) cm_not_normalized = deepcopy(cm) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] thresh = 2* cm_not_normalized.max() / 3. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): colour="white" if (cm_not_normalized[i, j] > thresh) else "black" general_plot_strings.append(self.form_plot_string('plt.text', j, i, round(cm[i, j],2), horizontalalignment="center", color=colour)) general_plot_strings.append(self.form_plot_string('plt.tight_layout')) general_plot_strings.append(self.form_plot_string('plt.ylabel', 'True label')) general_plot_strings.append(self.form_plot_string('plt.xlabel', 'Predicted label')) return general_plot_strings
def plot_confusion_matrix(self, cm, classes, normalize=False): # Taken from http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html # Select the colormap. cmap = plot.cm.Blues plot.imshow(cm, interpolation='nearest', cmap=cmap) plot.title('confusion matrix') plot.colorbar() tick_marks = np.arange(len(classes)) plot.xticks(tick_marks, classes, rotation=45) plot.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plot.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plot.tight_layout() plot.ylabel('True label') plot.xlabel('Predicted label') plot.show()
def plot_confusion_matrix(y_true, y_pred, fig=None, ax=None, classes=None, normalize=False, title=None, cmap=plt.cm.Blues, saving_path=None, figsize=(10, 10), color_bar=False, plot=True): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ # Only use the labels that appear in the data if classes is None: classes = np.unique(y_true) # Compute confusion matrix cm = confusion_matrix(y_true, y_pred, labels=classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] if ax is None: fig, ax = plt.subplots(figsize=figsize) im = ax.imshow(cm, interpolation='nearest', cmap=cmap) if color_bar: ax.figure.colorbar(im, ax=ax) # We want to show all ticks... ax.set(xticks=np.arange(cm.shape[1]), yticks=np.arange(cm.shape[0]), # ... and label them with the respective list entries xticklabels=classes, yticklabels=classes, title=title, ylabel='True label', xlabel='Predicted label') ax.grid(False) # Rotate the tick labels and set their alignment. plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") # Loop over data dimensions and create text annotations. fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i in range(cm.shape[0]): for j in range(cm.shape[1]): # remove this condition if a 0 in each cell with no confusion is wanted if cm[i, j] > 0: ax.text(j, i, format(cm[i, j], fmt), ha="center", va="center", color="white" if cm[i, j] > thresh else "black") if fig is not None: fig.tight_layout() if saving_path is not None: fig.savefig(saving_path, bbox_inches='tight', dpi=200) if plot: plt.show()
def plot_confusion_matrix(cm, classes, img_name, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): plt.rcParams["font.family"] = 'DejaVu Sans' if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=90) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') plt.savefig("./plots/conf_matrix_" + classifier + img_name) plt.show()
def CM_ROC(x_test, y_test, loaded_model): #WORKS but not needed (I need to add ROC separately) # ------------------------------------------------------------------------------ # Evaluate classificaiton # Outputs the confusion matrix and ROC curve vith its AUC # ------------------------------------------------------------------------------ # predict prob = loaded_model.predict(x_test) pred = (prob > 0.5).astype('int32') # measure confusion labels = [0, 1] cm = metrics.confusion_matrix(y_test, pred, labels=labels) cm = cm.astype('float') cm_norm = cm / cm.sum(axis=1)[:, np.newaxis] print("cm", cm) print("cm_norm", cm_norm) fpr, tpr, thresholds = metrics.roc_curve(y_test, prob, pos_label=1) auc = metrics.roc_auc_score(y_test, prob) np.save('images/auc.npy', auc) print("AUC:", auc) #plotting fig = plt.figure() ax = fig.add_subplot(111) cax = ax.matshow(cm) plt.title('Confusion matrix', y=1.08) fig.colorbar(cax) ax.set_xticklabels([''] + labels) ax.set_yticklabels([''] + labels) plt.xlabel('Predicted') plt.ylabel('True') fmt = '.2f' thresh = cm_norm.max() / 2. for i in range(cm_norm.shape[0]): for j in range(cm_norm.shape[1]): ax.text(j, i, format(cm_norm[i, j], fmt), ha="center", va="center", color="white" if cm_norm[i, j] < thresh else "black") pl.savefig('images/conf.pdf') plt.show() #ROC figsize = (5, 5) fig, axis1 = plt.subplots(figsize=figsize) x_onetoone = y_onetoone = [0, 1] plt.plot(fpr, tpr, 'r-') plt.plot(x_onetoone, y_onetoone, 'k--', label="1-1") plt.legend(loc=0) plt.title("Receiver Operator Characteristic (ROC)") plt.xlabel("False Positive (1 - Specificity)") plt.ylabel("True Positive (Selectivity)") plt.tight_layout() pl.savefig('images/ROC.pdf') return prob, pred, auc
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') print(cm) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) # plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label')
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.GnBu): fig = plt.figure(figsize=[10, 10]) ax = plt.axes() plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar(fraction=0.046, pad=0.04) tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') #print(cm) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.grid(False) plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') return fig
def calcConfusionMatrix(y_train, y_pred): labels = [ 'angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral' ] tick_marks = np.array(range(len(labels))) + 0.5 cm = confusion_matrix(y_train, y_pred) np.set_printoptions(precision=2) cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print(cm_normalized) plt.figure(figsize=(12, 8), dpi=120) ind_array = np.arange(len(labels)) x, y = np.meshgrid(ind_array, ind_array) for x_val, y_val in zip(x.flatten(), y.flatten()): c = cm_normalized[y_val][x_val] if c > 0.01: plt.text(x_val, y_val, "%0.2f" % (c, ), color='red', fontsize=7, va='center', ha='center') plt.gca().set_xticks(tick_marks, minor=True) plt.gca().set_yticks(tick_marks, minor=True) plt.gca().xaxis.set_ticks_position('none') plt.gca().yaxis.set_ticks_position('none') plt.grid(True, which='minor', linestyle='-') plt.gcf().subplots_adjust(bottom=0.15) plotConfusionMatrix(cm_normalized, labels, title='Normalized confusion matrix') plt.savefig('confusion_matrix.png', format='png') plt.show()
def plot_confusion(clf, balanced): try: ids = clf.ids except: pass #todo X, y_target = getData(n_rows=None, type="all", ratio=1, split=True, balanced=balanced, complete=False, shuffle=True, ids=ids, return_ids=False) y_pred = clf.predict(X) cm = confusion_matrix(y_target, y_pred) cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.imshow(cm_normalized, interpolation='nearest', cmap=plt.cm.Blues) plt.title("Confusion matrix for classifier ...") plt.colorbar() plt.xticks(range(7)) plt.xticks(range(7)) plt.tight_layout() plt.ylabel("Target class") plt.ylabel("Predicted class") plt.show()
def gen_accuracy_mag_plot( filepath, df, y_true, y_pred, mag_min=16, mag_max=25, bins=4): df = df[(df.ndet==12)&(df.photoflag==0)&(df.split=='val')].reset_index() intervals = np.linspace(mag_min, mag_max, bins*(mag_max-mag_min)+1) mags = [] acc = [[], [], []] yp_arg = np.argmax(y_pred, axis=1) yt_arg = np.argmax(y_true, axis=1) plt.clf() for ix in range(len(intervals)-1): idx = df.r.between(intervals[ix], intervals[ix+1]) yt = yt_arg[idx] yp = yp_arg[idx] if len(np.unique(yt)) != n_classes: print('skipping', intervals[ix]) continue cm = metrics.confusion_matrix(yt, yp) cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] if cm.shape[0]==n_classes: mags.append(intervals[ix]) for c in range(n_classes): acc[c].append(cm[c,c]) for c in range(n_classes): plt.plot(mags, acc[c], c=colors[c]) plt.legend(classes) plt.ylabel('ACCURACY') plt.xlabel('MAGNITUDE (R)') plt.tight_layout() plt.savefig(f'{filepath}.png')
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Purples): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ if normalize: cm = cm.astype('float').T / cm.sum(axis=1).astype('float') cm = cm.T cm = np.floor(cm * 100.).astype(int) print(cm) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) #plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=90) plt.yticks(tick_marks, classes) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") #plt.tight_layout() plt.ylabel('True label')
def plot_confusion_matrix(model, test_set, y_test, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues, ax=None): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ np.set_printoptions(precision=2) y_pred = model.predict(test_set) try: classes = model.classes_ except: classes = [0, 1] cm = confusion_matrix(y_test, y_pred) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label')
def plot_confmat(cm, classes, normalized, \ title = 'Confusion matrix', cmap = plt.cm.Blues): if normalized: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print('Normalized confusion matrix:') else: print('Confusion matrix, without normalization:') print(cm) print() plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) fmt = '.2f' if normalized else 'd' threshold = cm.max() / 2 for i, j in product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), \ horizontalalignment = 'center', \ color = 'white' if cm[i, j] > threshold else 'black') plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label')
def plot_confusion_matrix(cm, classes, title='Confusion matrix', cmap=plt.cm.Blues): np.set_printoptions(precision=2) plt.figure() cm = 100 * cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], '.2f'), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') plt.show()
def plot_confusion_matrix(cm, classes, normalize=True, title='Confusion Matrix', cmap=plt.cm.Blues): plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized Confusion Matrix") else: print('Confusion Matrix without Normalization') print(cm) thresh = cm.max() / 2.0 for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): #plt.text(j,i, int(cm[i j]*100)/100.0, horizontalalignment = "center", color = "white" if (cm[i,j]) > thresh else "black") plt.text(j, i, int(cm[i, j] * 100) / 100.0, horizontalalignment="center", color="white" if (cm[i, j]) > thresh else "black") #plt.text(j,i, int(cm[i, j]*100)/100.0, horizontalalignment = "center", color = "black") plt.tight_layout() plt.ylabel('True Labels') plt.xlabel('Predicted Labels')
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') print(cm) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label')
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.figure() plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() bottom, top = plt.ylim() #plt.ylim(bottom + 0.5, top - 0.5) plt.ylabel('True label') plt.xlabel('Predicted label') plt.show()
def plot_confusion_matrix_(y_true, y_pred, classes, normalize=False, title=None, cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ plt.figure(figsize=(20,20)) if not title: if normalize: title = 'Normalized confusion matrix' else: title = 'Confusion matrix, without normalization' # Compute confusion matrix cm = confusion_matrix(y_true, y_pred) # Only use the labels that appear in the data classes = classes[unique_labels(y_true, y_pred)] if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] # print("Normalized confusion matrix") # else: # print('Confusion matrix, without normalization') #print(cm) fig, ax = plt.subplots() im = ax.imshow(cm, interpolation='nearest', cmap=cmap) ax.figure.colorbar(im, ax=ax) # We want to show all ticks... ax.set(xticks=np.arange(cm.shape[1]), yticks=np.arange(cm.shape[0]), # ... and label them with the respective list entries xticklabels=classes, yticklabels=classes, title=title, ylabel='True label', xlabel='Predicted label') # Rotate the tick labels and set their alignment. plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") # Loop over data dimensions and create text annotations. fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i in range(cm.shape[0]): for j in range(cm.shape[1]): ax.text(j, i, format(cm[i, j], fmt), ha="center", va="center", color="white" if cm[i, j] > thresh else "black") fig.tight_layout() return ax
def plot_confusion_matrix(y_test, y_pred, class_names, training_size, normalize=False, save_dir=None, meta=None): """ *** ONLY WORKS FOR NON-MULTICLASS *** This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ #title='Confusion matrix (Training size:%d)' % training_size title = 'Confusion matrix' if meta is not None: title += " | " + meta cmap = plt.cm.Blues #fig,ax = plt.subplots() #fig.suptitle(title,) # Compute confusion matrix cm = confusion_matrix(y_test, y_pred) # Plot normalized confusion matrix plt.figure(figsize=(20, 20), dpi=120) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(class_names)) plt.xticks(tick_marks, class_names, rotation=45, fontsize=10) plt.yticks(tick_marks, class_names, fontsize=10) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') if save_dir != None: plt.savefig(os.path.join(save_dir, "prediction-heatmap-%s.png" % meta), bbox_inches='tight', dpi=200) plt.close() else: plt.show()
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Reds, **kwargs): filename = kwargs.get('filename', None) """ reference: http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html """ fig = plt.figure() import itertools plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] # print("Normalised confusion matrix") # else: # print('Confusion matrix, without normalization') # print(cm) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): # plt.text(j, i, round(cm[i,j],2), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") if (i == 1) and (j == 1): plt.text(j, i, round(cm[i, j], 2), horizontalalignment="center", color="white") elif (i == 0) and (j == 0): plt.text(j, i, round(cm[i, j], 2), horizontalalignment="center", color="black") else: plt.text(j, i, round(cm[i, j], 2), horizontalalignment="center", color="black") plt.ylabel('True label') plt.xlabel('Predicted label') if filename: plt.savefig(filename, dpi=300, bbox_inches='tight') return fig
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
def make_and_plot_confusion_matrix(test_lebel, best_preds): import matplotlib.pyplot as plt import numpy as np import pandas as pd import matplotlib.cm as cm from sklearn.metrics import confusion_matrix malware_dict = { 1: 'Ramnit', 2: 'Lollipop', 3: 'Kelihos_ver3', 4: 'Vundo', 5: 'Simba', 6: 'Tracur', 7: 'Kelihos_ver1', 8: 'Obfuscator.ACY', 9: 'Gatak' } names = list(malware_dict.values()) cm = confusion_matrix(test_lebel, best_preds) cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] norm_conf = [] for i in cm: a = 0 tmp_arr = [] a = sum(i, 0) for j in i: tmp_arr.append(float(j) / float(a)) norm_conf.append(tmp_arr) fig = plt.figure(figsize=(10, 10)) plt.clf() ax = fig.add_subplot(111) ax.set_aspect(1) res = ax.imshow(np.array(norm_conf), cmap=plt.cm.Blues, interpolation='nearest') width = len(cm) height = len(cm[0]) for x in range(width): for y in range(height): ax.annotate(str(format(round(cm[x][y], 2))), xy=(y, x), horizontalalignment='center', verticalalignment='center') plt.title('Konfuzijska matrica') cb = fig.colorbar(res) plt.xticks(range(width), names) plt.yticks(range(height), names) plt.grid(False)
def plot_confusion_matrix(y_true, y_pred, classes, normalize=False, title=None, cmap=plt.cm.Blues): if not title: if normalize: title = 'Matriz de confusión normalizada' else: title = 'Matriz de confusión sin normalizar' # Matriz de confusión cm = confusion_matrix(y_true, y_pred) # Clases classes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Matriz de confusión normalizada") else: print('Matriz de confusión sin normalizar') print(cm) fig, ax = plt.subplots() im = ax.imshow(cm, interpolation='nearest', cmap=cmap) ax.figure.colorbar(im, ax=ax) # We want to show all ticks... ax.set(xticks=np.arange(cm.shape[1]), yticks=np.arange(cm.shape[0]), xticklabels=classes, yticklabels=classes, title=title, ylabel='Etiquetas verdaderas', xlabel='Etiquetas predichas') # Rotar las etiquetas para su posible lectura plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") # Creación de anotaciones fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i in range(cm.shape[0]): for j in range(cm.shape[1]): ax.text(j, i, format(cm[i, j], fmt), ha="center", va="center", color="white" if cm[i, j] > thresh else "black") fig.tight_layout() return ax
def plot_confusion_matrix(cm, classes): # Modified form of https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ # Create figure if len(classes) < 5: plt.figure(figsize=(7, 7)) else: plt.figure(figsize=(20, 20)) # Plot non-normalized confusion matrix plt.subplot(121) plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues) plt.title('Confusion matrix') tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=90) plt.yticks(tick_marks, classes) fmt = 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.ylabel('True label') plt.xlabel('Predicted label') plt.tight_layout(w_pad=8) # Plot normalized confusion matrix plt.subplot(122) cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues) plt.title('Normalized confusion matrix') tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=90) plt.yticks(tick_marks, classes) fmt = '.2f' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.ylabel('True label') plt.xlabel('Predicted label') plt.tight_layout(w_pad=8)
def plot_confusion_matrix(cm, mytitle=None, classes=None, saveas=None): """ # print Confusion matrix with blue gradient colours # cm = metrics.confusion_matrix(y_expected, y_predicted) # plot_confusion_matrix(cm, mytitle="Some Title", saveas="SomeImage.png") """ from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable if mytitle == None: mytitle = "Confusion Matrix" if classes == None: classes = ['0', '1'] cmap = plt.cm.Blues if type(cm) == list: cm = np.array(cm) cm_orig = cm.copy() cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.rcParams["figure.figsize"] = (4, 4) # (width, height) fig, ax = plt.subplots() # _ = ax.scatter(x1,x2, marker='.') im = ax.imshow(cm, interpolation='nearest', cmap=cmap) ax.set_title(mytitle) divider = make_axes_locatable(ax) cax = divider.append_axes('right', size='5%', pad=0.5) fig.colorbar(im, cax=cax, orientation='vertical') ax.set_xticks([0, 1]) ax.set_xticklabels(classes) ax.set_yticks([0, 1]) ax.set_yticklabels(classes) fmt = '.1%' thresh = 0.5 * (cm.max() + cm.min()) for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): mytext = str(cm_orig[i, j]) + ' / ' + format(cm[i, j], fmt) ax.text( j, i, mytext, horizontalalignment="center", color="white" if cm[i, j] > thresh else "black" # , backgroundcolor="navy" if cm[i, j] > thresh else "aliceblue" # , weight="bold" ) plt.tight_layout() ax.set_ylabel('Actual (True, Exptected)') ax.set_xlabel('Predicted (Model)') plt.show() if saveas != None: plt.savefig(saveas, dpi=100)
def plot_confusion_matrix(truth, pred, classes=None, title='', xlabel='Prediction', ylabel='Truth', cmap_name='Blues', show_colorbar=False, relative=False, show_ratio=True, ratio_precision=2, threshold=0.5): cm = confusion_matrix(truth, pred) cm_normed = cm.astype(np.float32) / cm.sum(axis=1)[:, None] if classes is None: classes = np.arange(max(cm.shape[0], cm.shape[1])) if relative: plt.imshow(cm_normed, interpolation='nearest', cmap=plt.get_cmap(cmap_name)) else: plt.imshow(cm_normed, interpolation='nearest', cmap=plt.get_cmap(cmap_name), vmin=0.0, vmax=1.0) if title: plt.title(title) if show_colorbar: plt.colorbar() if show_ratio: fmt = '{{}}\n({{:.{}f}})'.format(ratio_precision) else: fmt = '{}' for i in range(cm.shape[0]): for j in range(cm.shape[1]): plt.text( j, i, fmt.format(cm[i, j], cm_normed[i, j]), color='white' if cm_normed[i, j] >= threshold else 'black', horizontalalignment='center', verticalalignment='center') tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes) plt.yticks(tick_marks, classes) plt.tight_layout() if xlabel: plt.xlabel(xlabel) if ylabel: plt.ylabel(ylabel)
def plot_confusion_matrix(y_true, y_pred, classes, normalize=False, cmap=plt.cm.Blues): """ Function copied from the scikit-learn examples (https://scikit-learn.org/stable/) This function plots a confusion matrix. Normalization can be applied by setting `normalize=True`. """ # compute confusion matrix cm = confusion_matrix(y_true, y_pred) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') # plot confusion matrix fig, ax = plt.subplots(figsize=(6, 6), tight_layout=True) im = ax.imshow(cm, interpolation='nearest', cmap=cmap) # ax.figure.colorbar(im, ax=ax) ax.set(xticks=np.arange(cm.shape[1]), yticks=np.arange(cm.shape[0]), ylim=[3.5, -0.5], xticklabels=classes, yticklabels=classes, ylabel='True label', xlabel='Predicted label') # rotate the tick labels and set their alignment. plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") # loop over data dimensions and create text annotations. fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i in range(cm.shape[0]): for j in range(cm.shape[1]): ax.text(j, i, format(cm[i, j], fmt), ha="center", va="center", color="white" if cm[i, j] > thresh else "black", fontsize=12) fig.tight_layout() return ax
def plot_confusion_matrix(cm, classes, normalize=False, filename='../eval.pdf', cmap=plt.cm.Blues, figsize=(22, 20), ft_size=25, tick_size=30, label_size=35): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] cm = cm[~np.any(np.isnan(cm), axis=1)] # print(cnf_matrix) print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') print(cm) plt.rcParams.update({'font.size': ft_size}) plt.rcParams.update({'figure.autolayout': True}) plt.rc('xtick', labelsize=tick_size) plt.rc('ytick', labelsize=tick_size) plt.figure(figsize=figsize) im = plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.colorbar(im, fraction=0.046, pad=0.04) tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True ID', fontsize=label_size) plt.xlabel('Predicted ID', fontsize=label_size) plt.savefig(filename)
def confusion_matrix_plot(truevalues, fittedvalues, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """Generate confusion matrix table, for use in classification modeling diagnostics. Parameters ---------- truevalues : pd.Series True classes. fittedvalues : pd.Series Estimated classes (must be discrete, corresponding to class labels, not probabilities) normalize : boolean Whether to normalize the confusion matrix (represent as percentages rather than counts) title : str Title to give the plot. cmap : Matplotlib colormap Colormap to use for table coloring. Returns ------- plt : Matplotlib figure object. Confusion matrix table. """ cm = confusion_matrix(truevalues, fittedvalues) classes = set(truevalues.values) plt.figure() plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label') return plt
def plot_confusion(clf, balanced): try: ids=clf.ids except: pass #todo X,y_target = getData(n_rows=None, type="all", ratio=1, split=True, balanced=balanced, complete=False, shuffle=True, ids=ids, return_ids=False) y_pred = clf.predict(X) cm = confusion_matrix(y_target, y_pred) cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.imshow(cm_normalized, interpolation='nearest', cmap=plt.cm.Blues) plt.title("Confusion matrix for classifier ...") plt.colorbar() plt.xticks(range(7)) plt.xticks(range(7)) plt.tight_layout() plt.ylabel("Target class") plt.ylabel("Predicted class") plt.show()
def compute_confusion_matrix(y_test, y_pred, flag_plot, plot_title): # Compute confusion matrix cm = confusion_matrix(y_test, y_pred) np.set_printoptions(precision=2) plt.figure() plot_confusion_matrix(cm) # Normalize the confusion matrix by row (i.e by the number of samples in # each class) cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] plt.figure() plot_confusion_matrix( cm_normalized, title='Normalized confusion matrix for ' + plot_title) if flag_plot == 1: plt.savefig('Confusion_Matrix_' + plot_name + '.png', bbox_inches='tight') diag_sum = 0 for i in range(6): diag_sum += cm_normalized[i][i] cm_normalized_sum = sum(sum(cm_normalized)) accuracy = diag_sum / cm_normalized_sum return (cm, cm_normalized, accuracy)