Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_points(L, scores, P, N):
- scores_idx = []
- scores_sort = np.sort(scores, axis=0)[::-1].tolist()
- for i in scores_sort:
- scores_idx.append(scores.index(i))
- tp = 0
- fp = 0
- tp_rate = []
- fp_rate = []
- f_prev = -1
- for i in range(len(L)):
- if scores_sort[i] != f_prev:
- tp_rate.append(tp/P)
- fp_rate.append(fp/N)
- f_prev = scores_sort[i]
- if L[scores_idx[i]] == 1:
- tp = tp + 1
- else:
- fp = fp + 1
- tp_rate.append(tp/P)
- fp_rate.append(fp/N)
- return fp_rate, tp_rate
- groundtruth = [1, 0, 1, 1, 1, 0, 0, 0]
- c1_scores = [0.5, 0.3, 0.6, 0.22, 0.4, 0.51, 0.2, 0.33]
- c2_scores = [0.04, 0.1, 0.68, 0.22, 0.4, 0.11, 0.8, 0.53]
- th1 = 0.33
- th2 = 0.1
- P = 4
- N = 4
- fpr1, tpr1 = get_points(groundtruth, c1_scores, P, N)
- fpr2, tpr2 = get_points(groundtruth, c2_scores, P, N)
- c3_classification = []
- c4_classification = []
- for i in range(len(groundtruth)):
- if c1_scores[i] > th1 and c2_scores[i] > th2:
- c3_classification.append(1)
- else:
- c3_classification.append(0)
- if c1_scores[i] > th1 or c2_scores[i] > th2:
- c4_classification.append(1)
- else:
- c4_classification.append(0)
- fpr3 = 0
- tpr3 = 0
- fpr4 = 0
- tpr4 = 0
- TP = 0
- FP = 0
- for i in range(len(groundtruth)):
- if groundtruth[i]==1 and c3_classification[i]==1:
- TP = TP + 1
- if groundtruth[i]==0 and c3_classification[i]==1:
- FP = FP + 1
- fpr3 = FP/N
- tpr3 = TP/N
- TP = 0
- FP = 0
- for i in range(len(groundtruth)):
- if groundtruth[i]==1 and c4_classification[i]==1:
- TP = TP + 1
- if groundtruth[i]==0 and c4_classification[i]==1:
- FP = FP + 1
- fpr4 = FP/N
- tpr4 = TP/N
- plt.figure()
- plt.xlabel("FPR")
- plt.ylabel("TPR")
- plt.plot(fpr1, tpr1, color="green")
- plt.plot(fpr2, tpr2)
- plt.plot(fpr3, tpr3, 'ro')
- plt.plot(fpr4, tpr4, 'ro')
- plt.plot()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement