-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
190 lines (161 loc) · 8.17 KB
/
preprocessing.py
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
from ast import literal_eval
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler, MinMaxScaler, OneHotEncoder, MultiLabelBinarizer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
import joblib
from config import parsed_train_path, parsed_test_path, scaler_path, encoder_path, train_path, test_path, max_values, stop_words
from collections import Counter
def treat_dict_column(data, old_col_name, new_col_name, key):
data[old_col_name].fillna('{}', inplace = True)
data[old_col_name] = data[old_col_name].apply(literal_eval)
data[new_col_name] = data[old_col_name].apply(pd.Series)[key]
data.drop(old_col_name, inplace=True, axis=1)
return data
def treat_list_of_dicts_column(data, col_name, key="name"):
data[col_name].fillna('[]', inplace=True)
data[col_name] = data[col_name].apply(literal_eval)
if col_name == "crew":
data[col_name] = data[col_name].apply(lambda x: apply_filter(x,key,"job",["Producer", "Director", "Writer"]))
else:
data[col_name] = data[col_name].apply(lambda x: [d[key] for d in x] if x != [] else ["nan"])
return data
def apply_filter(x, key, filter_by, filter_list):
res = [d[key] for d in x if d[filter_by] in filter_list]
res = res if res != [] else["nan"]
return res
def encode_date(data, col, max_val):
data[col + '_sin'] = np.sin(2 * np.pi * data[col]/max_val)
data[col + '_cos'] = np.cos(2 * np.pi * data[col]/max_val)
return data
def apply_filter_most_common(x, filter_list):
res = set(x).intersection(filter_list)
res = list(res) if res != set() else["nan"]
return res
def save_most_common(data, col_name, k):
words = Counter(c for clist in data[col_name] for c in clist)
most_common = words.most_common(k)
most_common_keys = [key[0] for key in most_common]
data[col_name] = data[col_name].apply(lambda x: apply_filter_most_common(x,most_common_keys))
return data
def process_date_feature(data):
data["release_date"].fillna(method="pad", inplace=True)
data['year'] = data.release_date.dt.year
data['day_of_week'] = data.release_date.dt.dayofweek
data['quarter'] = data.release_date.dt.quarter
data['month'] = data.release_date.dt.month
data = encode_date(data, 'month', 12)
data['day'] = data.release_date.dt.day
data = encode_date(data, 'day', 365)
day_names = data.release_date.dt.day_name()
data['is_weekend'] = day_names.apply(lambda x: 1 if x in ['Saturday', 'Sunday'] else 0)
data.drop(["release_date"], inplace=True, axis=1)
return data
def treat_str(data, col_name):
data[col_name] = data[col_name].apply(lambda x: [w.lower() for w in x.split(" ") if w.lower() not in stop_words])
return data
def parse_data(data, train=True):
data_label = data["revenue"]
data_log_label = np.log1p(data["revenue"])
data['has_homepage'] = 1
data.loc[pd.isnull(data['homepage']), "has_homepage"] = 0
# remove features - unreasonable & 1 uniqe value features
data.drop(["backdrop_path", "homepage", "imdb_id", "status", "poster_path", "revenue"], inplace=True, axis=1)
numerical_columns = ["popularity", "budget", "runtime", "vote_average", "vote_count",
"month_sin", "month_cos", "day_sin", "day_cos", "month", "day",
"year", "day_of_week", "quarter", "inflationBudget", "numKeywords",
"numcast", "budgetYearRatio", "logBudget"]
dummy_columns = ["collection_name", "original_language"]
multi_dummy_columns = ['cast', 'crew', 'genres', 'spoken_languages', 'production_companies',
'production_countries', 'Keywords']
embedding_features = ["original_title", "overview", "title", "tagline"]
# Flatten nested objects
data = treat_dict_column(data, "belongs_to_collection", "collection_name", "name")
for col in multi_dummy_columns:
data = treat_list_of_dicts_column(data, col)
if col in ["Keywords", "cast"]:
data[f'num{col}'] = data[col].apply(lambda x: len(x))
if train:
data = save_most_common(data, col, max_values)
for col in embedding_features:
data[col].fillna('', inplace=True)
data = treat_str(data, col)
if train:
data = save_most_common(data, col, max_values)
multi_dummy_columns = multi_dummy_columns + embedding_features
# Convert Bool to 1 and 0
data['video'].fillna(0, inplace=True)
data['video'] = data['video'].astype(int)
# Convert release_date to month,day,month_sin,month_cos,day_sin,day_cos,weekend, year, day_of_week, quarter
data = process_date_feature(data)
data['inflationBudget'] = data['budget'] + (data['budget'] * 1.8)/(100 * (data["year"].max()+1 - data['year']))
data['logBudget'] = np.log1p(data['budget'])
data['isTitleDifferent'] = 1
data.loc[data['original_title'] == data['title'], "isTitleDifferent"] = 0
data['budgetYearRatio'] = data['budget'] / data['year']**2
if train:
# Normalized numerical features
#pipe = Pipeline([('imputer', SimpleImputer(missing_values=np.nan, strategy='mean')),
# ('standard_scaler', StandardScaler()), ('minmax_scaler', MinMaxScaler(clip=True))])
pipe = Pipeline([('imputer', SimpleImputer(missing_values=np.nan, strategy='mean'))])
pipe.fit(data[numerical_columns].to_numpy())
joblib.dump(pipe, scaler_path)
# One hot Encoders fit
encoders = {}
for col_d in dummy_columns:
enc = OneHotEncoder(handle_unknown='ignore')
enc.fit(data[col_d].to_numpy().reshape(-1, 1))
encoders[col_d] = enc
for col_m in multi_dummy_columns:
enc = MultiLabelBinarizer()
enc.fit((data[col_m]))
encoders[col_m] = enc
joblib.dump(encoders, encoder_path)
else:
pipe = joblib.load(scaler_path)
encoders = joblib.load(encoder_path)
# Normalized numerical features
data[numerical_columns] = pipe.transform(data[numerical_columns].to_numpy())
# One hot Encoders transform
data_arr_dummies = []
for col_d in dummy_columns:
enc = encoders[col_d]
data_arr_dummies.append(enc.transform(data[col_d].to_numpy().reshape(-1, 1)).toarray())
data.drop(col_d, inplace=True, axis=1)
for col_m in multi_dummy_columns:
enc = encoders[col_m]
data_arr_dummies.append(enc.transform((data[col_m])))
data.drop(col_m, inplace=True, axis=1)
data_arr = np.concatenate([data.to_numpy()] + data_arr_dummies, axis=1)
return data_arr, data_label.to_numpy(), data.index, data_log_label.to_numpy()
def create_sample(data):
data = data.head(len(data.columns))
data = data.copy(deep=True)
for i in range(len(data.columns)):
data.iloc[i, i] = None
data.to_csv("sample.tsv", sep="\t")
if __name__ == '__main__':
parse_train = True
parse_test = True
# check none values
# create_sample(pd.read_csv(test_path, sep="\t", index_col='id', parse_dates=['release_date']))
# sample_data = pd.read_csv("sample.tsv", sep="\t", index_col='id', parse_dates=['release_date'])
# parsed_sample_data, parsed_sample_label, parsed_sample_index = parse_data(sample_data, train=True)
if parse_train:
train_data = pd.read_csv(train_path, sep="\t", index_col='id', parse_dates=['release_date'])
parsed_train_data, parsed_train_label, parsed_train_index, parsed_train_log_label = parse_data(train_data, train=True)
with open(parsed_train_path, 'wb') as f:
np.save(f, parsed_train_data)
np.save(f, parsed_train_label)
np.save(f, parsed_train_log_label)
np.save(f, parsed_train_index)
print(f"Number of features {parsed_train_data.shape[1]}")
if parse_test:
test_data = pd.read_csv(test_path, sep="\t", index_col='id', parse_dates=['release_date'])
parsed_test_data, parsed_test_label, parsed_test_index, parsed_test_log_label = parse_data(test_data, train=False)
with open(parsed_test_path, 'wb') as f:
np.save(f, parsed_test_data)
np.save(f, parsed_test_label)
np.save(f, parsed_test_log_label)
np.save(f, parsed_test_index)