Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. import seaborn as sns
  4. from sklearn.metrics import confusion_matrix
  5. np.set_printoptions(suppress=True, precision=2)
  6.  
  7. def get_confusion_matrix(dist, network, mode, data_split, normalize=True):
  8. GR = {'DNO': [2, 6, 8], 'DUC': [1, 3, 4, 5, 7]}
  9. results = pd.read_csv(
  10. f'8_classes_training/{dist}/{network}/csv_results/{mode}_{network}_{data_split}.csv')
  11. cm = confusion_matrix(results.true, results.pred)
  12. if normalize:
  13. cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
  14. # Convert to dataframe to move columns and rows
  15. cm = pd.DataFrame(cm, index=range(1,9), columns=range(1,9))
  16. cm = cm[GR['DNO']+GR['DUC']]
  17. cm = cm.loc[GR['DNO']+GR['DUC']]
  18. return cm
  19.  
  20.  
  21. def plot_confusion_matrix(cm, figsize=(5, 5), c1=1.5, c2=5.5):
  22. plt.figure(figsize=figsize)
  23. ax = plt.subplot()
  24. sns.heatmap(cm, annot=True, cmap=plt.cm.Blues,
  25. ax=ax, cbar=False, fmt='.1%')
  26. ax.set_ylabel('Prediction')
  27. ax.set_xlabel('Actual')
  28. ax.axvline(3, color="black", lw=4)
  29. ax.axhline(3, color="black", lw=4)
  30. plt.text(c1,-0.1, "DNO", horizontalalignment='center')
  31. plt.text(c2,-0.1, "DUC", horizontalalignment='center')
  32.  
  33. plt.text(len(cm)+.2,c1-.1, "DNO", horizontalalignment='center', rotation="vertical")
  34. plt.text(len(cm)+.2,c2-.1, "DUC", horizontalalignment='center', rotation="vertical")
  35. plt.show()
  36.  
  37.  
  38. cm1 = get_confusion_matrix('dist_1','resnet50','A','test')
  39. cm2 = get_confusion_matrix('dist_2','resnet50','A','test')
  40. cm3 = get_confusion_matrix('dist_3','resnet50','A','test')
  41.  
  42.  
  43. plot_confusion_matrix(cm1)
  44. plot_confusion_matrix(cm2)
  45. plot_confusion_matrix(cm3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement