├── README.md ├── Confusion_Matrix_Result.py ├── Confusion_Matrix_Plot.py └── Confusion_Matrix.py /README.md: -------------------------------------------------------------------------------- 1 | # Confusion_Matrix 2 | Confusion Matrix function. 3 | -------------------------------------------------------------------------------- /Confusion_Matrix_Result.py: -------------------------------------------------------------------------------- 1 | from sklearn import metrics 2 | from sklearn.metrics import classification_report 3 | from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score, confusion_matrix 4 | 5 | def CM_result(y_dev, y_pred,pos=1,report_digits=4): 6 | accuracy = accuracy_score(y_dev, y_pred) 7 | precision = precision_score(y_dev, y_pred,pos_label=pos) 8 | recall = recall_score(y_dev, y_pred,pos_label=pos) 9 | f1 = f1_score(y_dev, y_pred,pos_label=pos) 10 | CM_matrix = confusion_matrix(y_dev, y_pred) 11 | report = classification_report(y_dev, y_pred,digits=report_digits) 12 | 13 | print('Accuracy = ' + str(accuracy) +'\nPrecision = ' + str(precision) +'\nRecall = ' + str(recall)) 14 | return accuracy, precision, recall, f1, report, CM_matrix 15 | -------------------------------------------------------------------------------- /Confusion_Matrix_Plot.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import itertools 3 | 4 | def CM_plot(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): 5 | """ 6 | This function prints and plots the confusion matrix. 7 | Normalization can be applied by setting `normalize=True`. 8 | """ 9 | if normalize: 10 | cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] 11 | print("Normalized confusion matrix") 12 | else: 13 | print('Confusion matrix, without normalization') 14 | 15 | print(cm) 16 | 17 | plt.imshow(cm, interpolation='nearest', cmap=cmap) 18 | plt.title(title,fontsize = 50) 19 | #plt.colorbar() 20 | tick_marks = np.arange(len(classes)) 21 | plt.xticks(tick_marks, classes, rotation=45,fontsize = 40) 22 | plt.yticks(tick_marks, classes,fontsize = 40) 23 | 24 | fmt = '.2f' if normalize else 'd' 25 | thresh = cm.max() / 2. 26 | for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): 27 | plt.text(j, i, format(cm[i, j], fmt), 28 | horizontalalignment="center",fontsize = 60, 29 | color="white" if cm[i, j] > thresh else "black") 30 | 31 | plt.tight_layout() 32 | plt.ylabel('True label',fontsize = 40) 33 | plt.xlabel('Predicted label',fontsize = 40) -------------------------------------------------------------------------------- /Confusion_Matrix.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import itertools 3 | from sklearn import metrics 4 | from sklearn.metrics import classification_report 5 | from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score, confusion_matrix 6 | 7 | def CM_plot(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): 8 | """ 9 | This function prints and plots the confusion matrix. 10 | Normalization can be applied by setting `normalize=True`. 11 | """ 12 | if normalize: 13 | cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] 14 | print("Normalized confusion matrix") 15 | else: 16 | print('Confusion matrix, without normalization') 17 | 18 | print(cm) 19 | 20 | plt.imshow(cm, interpolation='nearest', cmap=cmap) 21 | plt.title(title,fontsize = 50) 22 | #plt.colorbar() 23 | tick_marks = np.arange(len(classes)) 24 | plt.xticks(tick_marks, classes, rotation=45,fontsize = 40) 25 | plt.yticks(tick_marks, classes,fontsize = 40) 26 | 27 | fmt = '.2f' if normalize else 'd' 28 | thresh = cm.max() / 2. 29 | for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): 30 | plt.text(j, i, format(cm[i, j], fmt), 31 | horizontalalignment="center",fontsize = 60, 32 | color="white" if cm[i, j] > thresh else "black") 33 | 34 | plt.tight_layout() 35 | plt.ylabel('True label',fontsize = 40) 36 | plt.xlabel('Predicted label',fontsize = 40) 37 | 38 | def CM_result(y_dev, y_pred,pos=1,report_digits=4): 39 | accuracy = accuracy_score(y_dev, y_pred) 40 | precision = precision_score(y_dev, y_pred,pos_label=pos) 41 | recall = recall_score(y_dev, y_pred,pos_label=pos) 42 | f1 = f1_score(y_dev, y_pred,pos_label=pos) 43 | CM_matrix = confusion_matrix(y_dev, y_pred) 44 | report = classification_report(y_dev, y_pred,digits=report_digits) 45 | 46 | print('Accuracy = ' + str(accuracy) +'\nPrecision = ' + str(precision) +'\nRecall = ' + str(recall)) 47 | return accuracy, precision, recall, f1, report, CM_matrix --------------------------------------------------------------------------------