Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. #Finding accuracy and other metrics over a range of cut off values
  2.  
  3. def test_accuracy_metrics_plot(test_mtx):
  4.  
  5.  
  6. cache = []
  7. cache_same = [] #Cache that collects all values in diagonal region, corresponding to same individuals
  8. cache_diff = [] #Cache that collects all values rest region, corresponding to diff individuals
  9. means =[] #means and max values are used to set range over which accuracies are calculated
  10. maxs = []
  11. counts = int(test_mtx.shape[0]/10)
  12. for i in range(counts):
  13. for j in range(counts):
  14.  
  15. var_name = 'new' + str(i) +str(j)
  16.  
  17. var_name_mtx = test_mtx[i*10:i*10 + 10, j*10:j*10 +10]
  18.  
  19. cache.append([var_name, var_name_mtx])
  20. if i ==j:
  21. cache_same.append([var_name, var_name_mtx])
  22. means.append(np.mean(var_name_mtx))
  23. maxs.append(np.max(var_name_mtx))
  24. else:
  25. cache_diff.append([var_name, var_name_mtx])
  26.  
  27. lower_bound = np.min(means)
  28. upper_bound = np.max(maxs)
  29. steps = (upper_bound - lower_bound)/100
  30.  
  31. #list of cut off values for which all metrics calculated, used to select best margin
  32. margins = list(np.arange(lower_bound, upper_bound, steps))
  33.  
  34.  
  35. metrics = []
  36. samples = test_mtx.shape[0]*test_mtx.shape[0]
  37. a_true = np.sqrt(samples)*10 #Actual true, here 300/900 are actual true
  38. a_neg = samples - a_true #Actual false, here 600/900 are actually false
  39.  
  40. for j in range(len(margins)):
  41.  
  42. f_pos = 0 #false pos
  43. for i in range(len(cache_diff)):
  44. f_pos += np.sum(cache_diff[i][1] < margins[j])
  45.  
  46. true_test_pos = 0 #true pos
  47. for i in range(len(cache_same)):
  48. true_test_pos += np.sum(cache_same[i][1] < margins[j])
  49.  
  50.  
  51. cor_neg = a_neg-f_pos #correctly identified negative or true negative
  52. accu = (true_test_pos + cor_neg)/samples #(true pos + true neg) / sample size
  53.  
  54. f_neg = a_true - true_test_pos #false neg
  55. prec= true_test_pos/(true_test_pos+f_pos) #precision
  56. rec = true_test_pos/(true_test_pos +f_neg) #reacall
  57. f1_score = 2 * prec * rec/(prec+rec) #f1 score
  58. metrics.append([margins[j], accu, prec, rec, f1_score])
  59.  
  60.  
  61.  
  62. return (metrics)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement