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
|
- import os
- data_dir = os.path.join(os.path.dirname(__file__), '../data/')
- outputs_dir = os.path.join(os.path.dirname(__file__), '../outputs/')
- raw_data = os.path.join(data_dir, 'CrossValidated-Posts.csv')
- train_data = os.path.join(data_dir, 'train-raw.csv')
- test_data = os.path.join(data_dir, 'test-raw.csv')
- train_processed = os.path.join(data_dir, 'train-processed.pkl')
- test_processed = os.path.join(data_dir, 'test-processed.pkl')
- classifier_pkl = os.path.join(outputs_dir, 'classifier.pkl')
- pipeline_pkl = os.path.join(outputs_dir, 'pipeline.pkl')
- col_id = 'Id'
- col_text = 'Text'
- col_title = 'Title'
- col_body = 'Body'
- col_tags = 'Tags'
- col_label = 'IsTaggedML'
- extra_feature_cols = ['Score','ViewCount','AnswerCount','CommentCount','FavoriteCount']
- text_cols = [col_title, col_body]
- all_raw_cols = ['Id','Title','Body','Tags','CreationDate','Score','ViewCount','AnswerCount','CommentCount','FavoriteCount','IsTaggedML']
- def save(obj, path):
- import pickle
- with open(path, 'wb') as f:
- pickle.dump(obj, f)
- def load(path):
- import pickle
- with open(path, 'rb') as f:
- return pickle.load(f)
- def load_labels(path=train_data):
- import pandas as pd
- return pd.read_csv(path, usecols=[col_label])[col_label]
- def compute_metrics(clf, X, y, prefix):
- from sklearn.metrics import accuracy_score, f1_score, recall_score, precision_score, roc_auc_score, balanced_accuracy_score, auc, precision_recall_curve
- preds = clf.predict(X)
- probas = clf.predict_proba(X)[:,1]
- pr_curve = precision_recall_curve(y, probas)
- return {
- f"{prefix}_accuracy_score": accuracy_score(y, preds),
- f"{prefix}_f1_score": f1_score(y, preds),
- f"{prefix}_recall_score": recall_score(y, preds),
- f"{prefix}_precision_score": precision_score(y, preds),
- f"{prefix}_roc_auc_score": roc_auc_score(y, probas),
- f"{prefix}_pr_auc_score": auc(pr_curve[1], pr_curve[0]),
- f"{prefix}_balanced_accuracy_score": balanced_accuracy_score(y, preds)
- }
- def load_data_and_labels(processed_path):
- X = load(processed_path)
- y = X[col_label]
- X = X.drop(columns=[col_id,col_label])
- return X, y
|