Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

evaluate.py 1.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  1. import sys
  2. import os
  3. import pickle
  4. import json
  5. import sklearn.metrics as metrics
  6. if len(sys.argv) != 6:
  7. sys.stderr.write('Arguments error. Usage:\n')
  8. sys.stderr.write('\tpython evaluate.py model features scores prc roc\n')
  9. sys.exit(1)
  10. model_file = sys.argv[1]
  11. matrix_file = os.path.join(sys.argv[2], 'test.pkl')
  12. scores_file = sys.argv[3]
  13. prc_file = sys.argv[4]
  14. roc_file = sys.argv[5]
  15. with open(model_file, 'rb') as fd:
  16. model = pickle.load(fd)
  17. with open(matrix_file, 'rb') as fd:
  18. matrix = pickle.load(fd)
  19. labels = matrix[:, 1].toarray()
  20. x = matrix[:, 2:]
  21. predictions_by_class = model.predict_proba(x)
  22. predictions = predictions_by_class[:, 1]
  23. precision, recall, prc_thresholds = metrics.precision_recall_curve(labels, predictions)
  24. fpr, tpr, roc_thresholds = metrics.roc_curve(labels, predictions)
  25. avg_prec = metrics.average_precision_score(labels, predictions)
  26. roc_auc = metrics.roc_auc_score(labels, predictions)
  27. with open(scores_file, 'w') as fd:
  28. json.dump({'avg_prec': avg_prec, 'roc_auc': roc_auc}, fd, indent=4)
  29. with open(prc_file, 'w') as fd:
  30. json.dump({'prc': [{
  31. 'precision': p,
  32. 'recall': r,
  33. 'threshold': t
  34. } for p, r, t in zip(precision, recall, prc_thresholds)
  35. ]}, fd, indent=4)
  36. with open(roc_file, 'w') as fd:
  37. json.dump({'roc': [{
  38. 'fpr': fp,
  39. 'tpr': tp,
  40. 'threshold': t
  41. } for fp, tp, t in zip(fpr, tpr, roc_thresholds)
  42. ]}, fd, indent=4)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...