-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassification_data.py
More file actions
277 lines (253 loc) · 10.2 KB
/
Copy pathclassification_data.py
File metadata and controls
277 lines (253 loc) · 10.2 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import logging
from typing import Optional, Tuple
import pandas as pd
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import utils.config as config
logger = logging.getLogger(__name__)
tqdm.pandas()
class DataSelector:
NOTEBOOK_METRICS_FILTERS = {
"default": ["LOC < 1000"],
"ML_based": [
"TYPE == ML",
"LOC < 1000",
],
}
NOTEBOOK_SCORES_FILTERS = {
"default": ["TotalViews >= 500"],
"ML_based": [
"TYPE == ML",
"TotalViews >= 500",
],
}
def __init__(
self,
notebook_metrics_df_file_path: str,
notebook_scores_df_file_path: str,
expert_scores_df_file_path: Optional[str],
):
logger.info("Going to load features and scores...")
self.notebook_metrics_df = pd.read_csv(notebook_metrics_df_file_path, low_memory=False)
logger.info(
f"self.notebook_metrics_df:\n"
f"columns: {self.notebook_metrics_df.columns}\n"
f"shape: {self.notebook_metrics_df.shape}\n"
)
self.notebook_scores_df = pd.read_csv(notebook_scores_df_file_path)
logger.info(
f"self.notebook_scores_df:\n"
f"columns: {self.notebook_scores_df.columns}\n"
f"shape: {self.notebook_scores_df.shape}\n"
)
self.experts_scores_df = None
if expert_scores_df_file_path is not None:
self.experts_scores_df = pd.read_csv(expert_scores_df_file_path)
logger.info(
f"self.experts_scores_df:\n"
f"columns: {self.experts_scores_df.columns}\n"
f"shape: {self.experts_scores_df.shape}\n"
)
logger.info("Loaded features and scores successfully.")
self._clean_notebook_metrics_df()
self._clean_notebook_scores_df()
if self.experts_scores_df is not None:
self._clean_experts_scores_df()
def get_train_test_split(
self,
notebook_metrics_filters: list = [],
notebook_scores_filters: list = [],
split_factor: float = 0.7,
selection_ratio: float = 0.25,
sort_by: str = config.DEFAULT_NOTEBOOK_SCORES_SORT_BY,
include_pt: bool = True,
) -> Tuple[pd.DataFrame, pd.DataFrame, list, list]:
features_df = self.apply_filters(self.notebook_metrics_df, notebook_metrics_filters)
scores_df = self.apply_filters(self.notebook_scores_df, notebook_scores_filters)
notebooks_sorted_by_score = self._prepare_data(
features_df=features_df,
scores_df=scores_df,
sort_by=sort_by,
include_pt=include_pt,
)
return self._split_data(
notebooks_sorted_by_score=notebooks_sorted_by_score,
split_factor=split_factor,
selection_ratio=selection_ratio,
)
def get_experts_test_split(
self,
notebook_metrics_filters: list = [],
include_pt: bool = True,
) -> Tuple[pd.DataFrame, list]:
if self.experts_scores_df is None:
raise Exception("set experts_scores_df to get experts test split.")
features_df = self.apply_filters(self.notebook_metrics_df, notebook_metrics_filters)
merged_df = pd.merge(self.experts_scores_df, features_df, on="KernelId", how="inner")
if not include_pt:
merged_df.drop(columns=["PT"], axis=1, inplace=True)
ones = merged_df[merged_df["expert_score"] == 1]
zeros = merged_df[merged_df["expert_score"] == 0]
min_len = min(len(ones), len(zeros))
X = pd.concat([zeros.head(min_len), ones.head(min_len)])
X.drop(["KernelId"], axis=1, inplace=True)
X.rename(
columns={
"ALLC": "ALLCL",
},
inplace=True,
)
X_test_experts = X.drop(["expert_score"], axis=1)
y_test_experts = list(X["expert_score"])
return X_test_experts, y_test_experts
def _clean_notebook_metrics_df(self) -> None:
logger.info("Going to clean self.notebook_metrics_df...")
logger.info(
f"self.notebook_metrics_df:\n"
f"columns: {self.notebook_metrics_df.columns}\n"
f"shape: {self.notebook_metrics_df.shape}\n"
f"dtypes: {self.notebook_metrics_df.dtypes}\n"
)
self.notebook_metrics_df.drop_duplicates(inplace=True)
numeric_columns = list(self.notebook_metrics_df.columns)
numeric_columns.remove("kernel_id")
self.notebook_metrics_df[numeric_columns] = self.notebook_metrics_df[numeric_columns].apply(
pd.to_numeric, errors="coerce"
)
self.notebook_metrics_df.dropna(inplace=True)
self.notebook_metrics_df.rename(columns={"kernel_id": "KernelId"}, inplace=True)
logger.info("Cleaned self.notebook_scores_df.")
logger.info(
f"self.notebook_metrics_df:\n"
f"columns: {self.notebook_metrics_df.columns}\n"
f"shape: {self.notebook_metrics_df.shape}\n"
f"dtypes: {self.notebook_metrics_df.dtypes}\n"
)
def _clean_notebook_scores_df(self) -> None:
logger.info("Going to clean self.notebook_scores_df...")
logger.info(
f"self.notebook_scores_df:\n"
f"columns: {self.notebook_scores_df.columns}\n"
f"shape: {self.notebook_scores_df.shape}\n"
f"dtypes: {self.notebook_scores_df.dtypes}\n"
)
self.notebook_scores_df = self.notebook_scores_df[
[
"KernelId",
"TotalViews",
"TotalVotes",
"PerformanceTier_kerneluser",
"topic_score",
"score_scaled",
"vote_scaled",
"combined_score",
]
]
self.notebook_scores_df.drop_duplicates(inplace=True)
numeric_columns = ["combined_score", "TotalVotes", "score_scaled", "vote_scaled"]
self.notebook_scores_df[numeric_columns] = self.notebook_scores_df[numeric_columns].apply(
pd.to_numeric, errors="coerce"
)
self.notebook_scores_df.dropna(subset=numeric_columns, inplace=True)
logger.info("Cleaned self.notebook_scores_df.")
logger.info(
f"self.notebook_scores_df:\n"
f"columns: {self.notebook_scores_df.columns}\n"
f"shape: {self.notebook_scores_df.shape}\n"
f"dtypes: {self.notebook_scores_df.dtypes}\n"
)
def _clean_experts_scores_df(self) -> None:
logger.info("Going to clean self.experts_scores_df...")
logger.info(
f"self.experts_scores_df:\n"
f"columns: {self.experts_scores_df.columns}\n"
f"shape: {self.experts_scores_df.shape}\n"
f"dtypes: {self.experts_scores_df.dtypes}\n"
)
self.experts_scores_df = self.experts_scores_df[
[
"KernelId",
"expert_score",
]
]
self.experts_scores_df.drop_duplicates(inplace=True)
numeric_columns = ["expert_score"]
self.experts_scores_df[numeric_columns] = self.experts_scores_df[numeric_columns].apply(
pd.to_numeric, errors="coerce"
)
self.experts_scores_df.dropna(subset=numeric_columns, inplace=True)
logger.info("Cleaned self.experts_scores_df.")
logger.info(
f"self.experts_scores_df:\n"
f"columns: {self.experts_scores_df.columns}\n"
f"shape: {self.experts_scores_df.shape}\n"
f"dtypes: {self.experts_scores_df.dtypes}\n"
)
@staticmethod
def apply_filters(df: pd.DataFrame, filters: list) -> pd.DataFrame:
df = df.copy()
for filter in filters:
df = df.query(filter)
return df
def _prepare_data(
self, features_df: pd.DataFrame, scores_df: pd.DataFrame, sort_by: str, include_pt: bool = True
) -> pd.DataFrame:
logger.info(f"features_df:\n" f"columns: {features_df.columns}\n" f"shape: {features_df.shape}\n")
logger.info(f"scores_df:\n" f"columns: {scores_df.columns}\n" f"shape: {scores_df.shape}\n")
logger.info("Going to merge features and scores...")
merged_df = pd.merge(
scores_df,
features_df,
on="KernelId",
how="inner",
)
logger.info(f"Going to sort merged_df by {sort_by}...")
merged_df.sort_values(by=[sort_by], inplace=True)
# Drop scores and unique id fields
merged_df.drop(
(
[
"KernelId",
"PerformanceTier_kerneluser",
"TotalViews",
"TotalVotes",
"topic_score",
"score_scaled",
"vote_scaled",
"combined_score",
]
+ ([] if include_pt else ["PT"])
),
axis=1,
inplace=True,
)
# TODO: standardize column names
merged_df.rename(
columns={
"ALLC": "ALLCL",
},
inplace=True,
)
logger.info(f"merged_df:\n{merged_df.columns}\n{merged_df.shape}")
merged_df.info()
return merged_df
def _split_data(
self,
notebooks_sorted_by_score: pd.DataFrame,
split_factor: float = 0.7,
selection_ratio: float = 0.25,
) -> Tuple[pd.DataFrame, pd.DataFrame, list, list]:
logger.info(f"get_train_test_split df.shape: {notebooks_sorted_by_score.shape}")
X = notebooks_sorted_by_score.copy()
le = len(X)
q1 = int(selection_ratio * (split_factor) * le)
q2 = int(selection_ratio * (1 - split_factor) * le)
X = pd.concat([X.head(q1), X.tail(q2)])
y = [0 for _ in range(q1)] + [1 for _ in range(q2)]
# TODO add ground truth
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)
logger.info(f"X_train.shape:{X_train.shape}")
logger.info(f"X_test.shape:{X_test.shape}")
logger.info(f"len(y_train):{len(y_train)}")
logger.info(f"len(y_test):{len(y_test)}")
return (X_train, X_test, y_train, y_test)