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

shared.py 2.1 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
55
56
57
58
59
60
61
  1. import os
  2. data_dir = os.path.join(os.path.dirname(__file__), '../data/')
  3. outputs_dir = os.path.join(os.path.dirname(__file__), '../outputs/')
  4. raw_data = os.path.join(data_dir, 'CrossValidated-Posts.csv')
  5. train_data = os.path.join(data_dir, 'train-raw.csv')
  6. test_data = os.path.join(data_dir, 'test-raw.csv')
  7. train_processed = os.path.join(data_dir, 'train-processed.pkl')
  8. test_processed = os.path.join(data_dir, 'test-processed.pkl')
  9. classifier_pkl = os.path.join(outputs_dir, 'classifier.pkl')
  10. pipeline_pkl = os.path.join(outputs_dir, 'pipeline.pkl')
  11. col_id = 'Id'
  12. col_text = 'Text'
  13. col_title = 'Title'
  14. col_body = 'Body'
  15. col_tags = 'Tags'
  16. col_label = 'IsTaggedML'
  17. extra_feature_cols = ['Score','ViewCount','AnswerCount','CommentCount','FavoriteCount']
  18. text_cols = [col_title, col_body]
  19. all_raw_cols = ['Id','Title','Body','Tags','CreationDate','Score','ViewCount','AnswerCount','CommentCount','FavoriteCount','IsTaggedML']
  20. def save(obj, path):
  21. import pickle
  22. with open(path, 'wb') as f:
  23. pickle.dump(obj, f)
  24. def load(path):
  25. import pickle
  26. with open(path, 'rb') as f:
  27. return pickle.load(f)
  28. def load_labels(path=train_data):
  29. import pandas as pd
  30. return pd.read_csv(path, usecols=[col_label])[col_label]
  31. def compute_metrics(clf, X, y, prefix):
  32. from sklearn.metrics import accuracy_score, f1_score, recall_score, precision_score, roc_auc_score, balanced_accuracy_score, auc, precision_recall_curve
  33. preds = clf.predict(X)
  34. probas = clf.predict_proba(X)[:,1]
  35. pr_curve = precision_recall_curve(y, probas)
  36. return {
  37. f"{prefix}_accuracy_score": accuracy_score(y, preds),
  38. f"{prefix}_f1_score": f1_score(y, preds),
  39. f"{prefix}_recall_score": recall_score(y, preds),
  40. f"{prefix}_precision_score": precision_score(y, preds),
  41. f"{prefix}_roc_auc_score": roc_auc_score(y, probas),
  42. f"{prefix}_pr_auc_score": auc(pr_curve[1], pr_curve[0]),
  43. f"{prefix}_balanced_accuracy_score": balanced_accuracy_score(y, preds)
  44. }
  45. def load_data_and_labels(processed_path):
  46. X = load(processed_path)
  47. y = X[col_label]
  48. X = X.drop(columns=[col_id,col_label])
  49. return X, y
Tip!

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

Comments

Loading...