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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
- #!/usr/bin/env python3
- """
- Modified liberally from https://dagshub.com/docs/experiment-tutorial/2-data-versioning/
- The goal of this repository is not to learn how to create an NLP classifier but to learn how to use DVC, therefore the ML code is unimportant.
- The training process has been seperated into distinct stages in order to demonstrate DVC's pipeline and experiment-tracking features.
- """
- import sys
- import yaml
- import pickle
- import pandas as pd
- import scipy.sparse as sp
- from sklearn.model_selection import train_test_split
- from sklearn.feature_extraction.text import TfidfVectorizer
- from sklearn.linear_model import SGDClassifier
- from sklearn.metrics import roc_auc_score, average_precision_score, accuracy_score, precision_score, recall_score, f1_score
- _params = None
- def read_param(name, filename="params.yaml"):
- global _params
- if _params == None:
- _params = yaml.safe_load(open(filename))
- obj = _params
- while "." in name:
- key, name = name.split(".", 1)
- obj = obj[key]
- return obj[name]
-
- def split(dataset_path=read_param("paths.dataset"),
- train_df_path=read_param("paths.train_df"),
- test_df_path=read_param("paths.test_df"),
- random_state=read_param("split.seed")):
-
- print(f"Loading {dataset_path}")
- df = pd.read_csv(dataset_path)
- df['MachineLearning'] = df['Tags'].str.contains('machine-learning').fillna(False)
- train_df, test_df = train_test_split(df, random_state=random_state, stratify=df['MachineLearning'])
-
- print(f"Saving {train_df_path}, {test_df_path}")
- train_df.to_csv(train_df_path)
- test_df.to_csv(test_df_path)
- def featurize(train_df_path=read_param("paths.train_df"),
- test_df_path=read_param("paths.test_df"),
- train_df_featurized_path=read_param("paths.train_df_featurized"),
- test_df_featurized_path=read_param("paths.test_df_featurized")):
-
- def feature_engineering(df):
- """Stolen directly from DAGsHub tutorial"""
- df['CreationDate'] = pd.to_datetime(df['CreationDate'])
- df['CreationDate_Epoch'] = df['CreationDate'].astype('int64') // 10 ** 9
- df = df.drop(columns=['Id', 'Tags'])
- df['Title_Len'] = df.Title.str.len()
- df['Body_Len'] = df.Body.str.len()
- # Drop the correlated features
- df = df.drop(columns=['FavoriteCount'])
- df['Text'] = df['Title'].fillna('') + ' ' + df['Body'].fillna('')
- return df
-
- print(f"Featurizing {train_df_path} to {train_df_featurized_path}")
- feature_engineering(pd.read_csv(train_df_path)).to_csv(train_df_featurized_path)
-
- print(f"Featurizing {test_df_path} to {test_df_featurized_path}")
- feature_engineering(pd.read_csv(test_df_path)).to_csv(test_df_featurized_path)
- def tfidf(train_df_featurized_path=read_param("paths.train_df_featurized"),
- test_df_featurized_path=read_param("paths.test_df_featurized"),
- train_tfidf_path=read_param("paths.train_tfidf"),
- test_tfidf_path=read_param("paths.test_tfidf"),
- tfidf_path=read_param("paths.tfidf"),
- max_features=read_param("tfidf.max_features")):
-
- print(f"Loading {train_df_featurized_path} and {test_df_featurized_path}")
- train_df = pd.read_csv(train_df_featurized_path)
- test_df = pd.read_csv(test_df_featurized_path)
-
- print(f"Training TF-IDF vectorizer with max_features={max_features}")
- tfidf = TfidfVectorizer(max_features=max_features)
- tfidf.fit(train_df['Text'])
-
- print(f"Transforming {train_df_featurized_path} to {train_tfidf_path}")
- train_tfidf = tfidf.transform(train_df['Text'])
- sp.save_npz(train_tfidf_path, train_tfidf)
-
- print(f"Transforming {test_df_featurized_path} to {test_tfidf_path}")
- test_tfidf = tfidf.transform(test_df['Text'])
- sp.save_npz(test_tfidf_path, test_tfidf)
-
- print(f"Writing tfidf vectorizer to {tfidf_path}")
- pickle.dump(tfidf, open(tfidf_path, 'wb'))
- def _eval_model(model, X, y):
- """Stolen directly from DAGsHub tutorial"""
- y_proba = model.predict_proba(X)[:, 1]
- y_pred = model.predict(X)
- return {
- 'roc_auc': float(roc_auc_score(y, y_proba)),
- 'average_precision': float(average_precision_score(y, y_proba)),
- 'accuracy': float(accuracy_score(y, y_pred)),
- 'precision': float(precision_score(y, y_pred)),
- 'recall': float(recall_score(y, y_pred)),
- 'f1': float(f1_score(y, y_pred)),
- }
- def train(train_df_featurized_path=read_param("paths.train_df_featurized"),
- train_tfidf_path=read_param("paths.train_tfidf"),
- loss=read_param("train.loss"),
- random_state=read_param("train.seed"),
- model_path=read_param("paths.model"),
- train_metrics_path=read_param("paths.train_metrics")):
-
- print(f"Loading {train_df_featurized_path} and {train_tfidf_path}")
- train_df = pd.read_csv(train_df_featurized_path)
- train_tfidf = sp.load_npz(train_tfidf_path)
-
- print(f"Training SGDClassifier model with loss={loss}, random_state={random_state}")
- model = SGDClassifier(loss=loss, random_state=random_state)
- model.fit(train_tfidf, train_df['MachineLearning'])
-
- print(f"Writing model to {model_path}")
- pickle.dump(model, open(model_path, 'wb'))
-
- print(f"Calculating and saving metrics to {train_metrics_path}")
- metrics = _eval_model(model, train_tfidf, train_df['MachineLearning'])
- yaml.safe_dump(metrics, open(train_metrics_path, 'w'))
- def test(test_df_featurized_path=read_param("paths.test_df_featurized"),
- test_tfidf_path=read_param("paths.test_tfidf"),
- model_path=read_param("paths.model"),
- test_metrics_path=read_param("paths.test_metrics")):
-
- print(f"Loading {test_df_featurized_path} and {test_tfidf_path}")
- test_df = pd.read_csv(test_df_featurized_path)
- test_tfidf = sp.load_npz(test_tfidf_path)
-
- print(f"Loading model from {model_path}")
- model = pickle.load(open(model_path, 'rb'))
-
- print(f"Calculating and saving metrics to {test_metrics_path}")
- metrics = _eval_model(model, test_tfidf, test_df['MachineLearning'])
- yaml.safe_dump(metrics, open(test_metrics_path, 'w'))
- if __name__ == '__main__':
- import sys
- if len(sys.argv) < 2:
- sys.exit(f"Usage: python3 {sys.argv[0]} [split|featurize|tfidf|train|test]")
- elif sys.argv[1] == "split":
- split()
- elif sys.argv[1] == "featurize":
- featurize()
- elif sys.argv[1] == "tfidf":
- tfidf()
- elif sys.argv[1] == "train":
- train()
- elif sys.argv[1] == "test":
- test()
- else:
- sys.exit(f"Invalid operation {sys.argv[1]}\nUsage: python3 {sys.argv[0]} [split|tfidf|train|test]")
|