Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. def get_points(L, scores, P, N):
  2. scores_idx = []
  3. scores_sort = np.sort(scores, axis=0)[::-1].tolist()
  4. for i in scores_sort:
  5. scores_idx.append(scores.index(i))
  6.  
  7. tp = 0
  8. fp = 0
  9. tp_rate = []
  10. fp_rate = []
  11. f_prev = -1
  12.  
  13. for i in range(len(L)):
  14. if scores_sort[i] != f_prev:
  15. tp_rate.append(tp/P)
  16. fp_rate.append(fp/N)
  17. f_prev = scores_sort[i]
  18.  
  19. if L[scores_idx[i]] == 1:
  20. tp = tp + 1
  21. else:
  22. fp = fp + 1
  23.  
  24. tp_rate.append(tp/P)
  25. fp_rate.append(fp/N)
  26. return fp_rate, tp_rate
  27.  
  28.  
  29. groundtruth = [1, 0, 1, 1, 1, 0, 0, 0]
  30.  
  31. c1_scores = [0.5, 0.3, 0.6, 0.22, 0.4, 0.51, 0.2, 0.33]
  32. c2_scores = [0.04, 0.1, 0.68, 0.22, 0.4, 0.11, 0.8, 0.53]
  33. th1 = 0.33
  34. th2 = 0.1
  35. P = 4
  36. N = 4
  37.  
  38. fpr1, tpr1 = get_points(groundtruth, c1_scores, P, N)
  39. fpr2, tpr2 = get_points(groundtruth, c2_scores, P, N)
  40.  
  41. c3_classification = []
  42. c4_classification = []
  43.  
  44. for i in range(len(groundtruth)):
  45. if c1_scores[i] > th1 and c2_scores[i] > th2:
  46. c3_classification.append(1)
  47. else:
  48. c3_classification.append(0)
  49. if c1_scores[i] > th1 or c2_scores[i] > th2:
  50. c4_classification.append(1)
  51. else:
  52. c4_classification.append(0)
  53.  
  54. fpr3 = 0
  55. tpr3 = 0
  56. fpr4 = 0
  57. tpr4 = 0
  58. TP = 0
  59. FP = 0
  60. for i in range(len(groundtruth)):
  61. if groundtruth[i]==1 and c3_classification[i]==1:
  62. TP = TP + 1
  63. if groundtruth[i]==0 and c3_classification[i]==1:
  64. FP = FP + 1
  65.  
  66. fpr3 = FP/N
  67. tpr3 = TP/N
  68.  
  69. TP = 0
  70. FP = 0
  71. for i in range(len(groundtruth)):
  72. if groundtruth[i]==1 and c4_classification[i]==1:
  73. TP = TP + 1
  74. if groundtruth[i]==0 and c4_classification[i]==1:
  75. FP = FP + 1
  76.  
  77. fpr4 = FP/N
  78. tpr4 = TP/N
  79.  
  80. plt.figure()
  81. plt.xlabel("FPR")
  82. plt.ylabel("TPR")
  83. plt.plot(fpr1, tpr1, color="green")
  84. plt.plot(fpr2, tpr2)
  85. plt.plot(fpr3, tpr3, 'ro')
  86. plt.plot(fpr4, tpr4, 'ro')
  87. plt.plot()
  88. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement