-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathload_test.py
More file actions
356 lines (267 loc) · 10.2 KB
/
Copy pathload_test.py
File metadata and controls
356 lines (267 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 4 19:16:46 2016
@author: Team BigInJapan
WOOP WOOP
"""
import pandas as pd
import numpy as np
import datetime as dt
from datetime import date
import os
from workalendar.europe import France
import string
cal = France()
def get_file_content(filename):
with open(filename, "r") as fichier_test:
content = fichier_test.readlines()
print("Reading File")
return content
def read_file_content(nrows, path_train, filename, rcvcall_data, preproc_data):
ass = ['CMS', 'Crises', 'Domicile', 'Gestion', 'Gestion - Accueil Telephonique',
'Gestion Assurances', 'Gestion Relation Clienteles', 'Gestion Renault', 'Japon', 'Médical',
'Nuit', 'RENAULT', 'Regulation Medicale', 'SAP', 'Services', 'Tech. Axa', 'Tech. Inter', 'Téléphonie',
'Tech. Total', 'Mécanicien', 'CAT', 'Manager', 'Gestion Clients', 'Gestion DZ', 'RTC', 'Prestataires']
print("Retrieving mean and std arrays")
# mean_ass, std_ass, rcvcalls, preproc = get_day_mean_std(nrows, path_train, ass)
rcvcalls = rcvcall_data
preproc = preproc_data
test_matrix = []
found_ass = []
content = get_file_content(filename)
skip = True
print("Starting the features extraction")
#Initializing the cols
date_list = []
notFound = 0
for line in content:
if (skip):
print("Skipping first line")
skip = False
else:
feature = []
splitted = line.split(" ")
date = extract_date(splitted[0])
date.replace(microsecond = 000)
weekday = extract_weekday(date)
hour = extract_hour(date)
month = extract_month(date)
year = extract_year(date)
day_off = is_day_off(splitted[0], weekday)
if (weekday == 5 or weekday == 6):
we = 1
else:
we = 0
jour, nuit = return_day_night(splitted[0])
assignment = splitted[1]
if (assignment not in found_ass):
found_ass.append(assignment)
try:
ass_index = ass.index(assignment)
# mean = mean_ass[ass_index][month+year]
# std = std_ass[ass_index][month+year]
# sel = preproc[ass_index].loc[preproc_test[ass_index].loc[:,"MONTH_YEAR"] == month+13*(year-2011)][preproc_test[ass_index].loc[:,"WEEKDAY"] == weekday]
# mean = mean_ass[ass_index][month+12*(year-2011)-1, weekday]
# std = std_ass[ass_index][month+12*(year-2011)-1, weekday]
week_before = date - dt.timedelta(7)
two_weeks_before = date - dt.timedelta(14)
#print(week_before)
# try:
# #test = dt.datetime(2011,1,1,0,30,0)
# alpha = rcvcalls[ass_index].loc[week_before]['CSPL_RECEIVED_CALLS']
# try:
# beta = rcvcalls[ass_index].loc[two_weeks_before]['CSPL_RECEIVED_CALLS']
# except:
# beta = 0
# #print(alpha)
#
# except KeyError:
# alpha = 0
# beta = 0
# notFound += 1
#print(alpha)
# alpha = (3*alpha + beta)/4
feature.append(date)
feature.append(day_off)
feature.append(we)
feature.append(assignment)
# feature.append(jour)
feature.append(nuit)
feature.append(weekday)
feature.append(hour)
feature.append(month)
# feature.append(mean)
# feature.append(std)
# feature.append(alpha)
date_list.append(date)
test_matrix.append(feature)
except ValueError:
print("I found smth that is not in the assignment list : %s" % assignment)
print("Done\n")
#print (found_ass)
print("There are %d vectors" % len(test_matrix))
print("We didn't find %d dates" % notFound)
dataFrame = pd.DataFrame(test_matrix, index = date_list, columns = ['DATE', 'DAY_OFF', 'WEEK_END',
'ASS_ASSIGNMENT', 'NUIT', 'WEEKDAY', 'HOUR', 'MONTH'])
test_matrix_by_ass = []
i = 0
for ass_assign in ass:
test_matrix_by_ass.append(dataFrame.loc[dataFrame.loc[:,"ASS_ASSIGNMENT"] == ass_assign, :])
RCV_7DAYS = pd.DataFrame(rcvcalls[i])
RCV_14DAYS = pd.DataFrame(rcvcalls[i])
RCV_7DAYS.loc[:,"DATE"] = rcvcalls[i].index + dt.timedelta(7)
RCV_14DAYS.loc[:,"DATE"] = rcvcalls[i].index + dt.timedelta(14)
RCV_7DAYS = RCV_7DAYS[RCV_7DAYS.loc[:,"DATE"] < max(rcvcalls[i].index)]
RCV_14DAYS = RCV_14DAYS[RCV_14DAYS.loc[:,"DATE"] < max(rcvcalls[i].index)]
test_matrix_by_ass[i] = pd.merge(test_matrix_by_ass[i], RCV_7DAYS, on = "DATE", suffixes = ('', '_x'), how = 'left')
test_matrix_by_ass[i] = pd.merge(test_matrix_by_ass[i], RCV_14DAYS, on = "DATE", suffixes = ('', '_y'), how = 'left')
test_matrix_by_ass[i] = pd.concat([test_matrix_by_ass[i], pd.get_dummies(test_matrix_by_ass[i].loc[:,'WEEKDAY'])], axis = 1, join = 'inner')
for j in range(7):
try:
test_matrix_by_ass[i].loc[:,j]
except KeyError:
test_matrix_by_ass[i].loc[:,j] = pd.DataFrame([0 for i in range(len(rcvcalls[i].index))])
test_matrix_by_ass[i].rename(index=str, columns={'CSPL_RECEIVED_CALLS': 'RCV_7DAY', 'CSPL_RECEIVED_CALLS_y': 'RCV_14DAY', 0: "Lundi", 1: "Mardi", 2: "Mercredi", 3: "Jeudi", 4: "Vendredi", 5: "Samedi", 6 : "Dimanche"}, inplace = True)
test_matrix_by_ass[i].index = test_matrix_by_ass[i].loc[:,'DATE']
test_matrix_by_ass[i].drop(['DATE', 'WEEKDAY', 'ASS_ASSIGNMENT'], 1, inplace = True)
test_matrix_by_ass[i] = test_matrix_by_ass[i].loc[:,['DAY_OFF', 'WEEK_END', 'NUIT', 'HOUR', 'MONTH', 'RCV_7DAY', 'RCV_14DAY', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dimanche']]
i+=1
a = pd.DataFrame(test_matrix_by_ass, index = ass)
#print(a.loc[ass[4], :])
return a
def return_day_night(time_full):
time = time_full.split(" ")
hour_min = (time[0].split("-"))
hour_proper = int(hour_min[0])
minutes = int(hour_min[1])
if (hour_proper >= 7 and minutes >= 30 and hour_proper < 23):
jour = 1
nuit = 0
if (hour_proper >= 23 and minutes <= 30):
jour = 1
nuit = 0
if (hour_proper >= 23 and minutes >= 30):
jour = 0
nuit = 1
if (hour_proper <= 6):
jour = 0
nuit = 1
if (hour_proper == 7 and minutes < 30):
jour = 0
nuit = 1
return jour, nuit
def is_day_off(date, weekday):
splitted = ((date.split(" "))[0]).split("-")
#print(splitted)
y = int(splitted[0])
m = int(splitted[1])
d = int(splitted[2])
if (cal.is_working_day(dt.date(y,m,d))):
return 1
else:
return 0
# Everything below allows to retrieve the array Asssignment - Weekday_Mean and Assignment - Weekday_STD
def extract_date(string):
d = dt.datetime.strptime(string, "%Y-%m-%d %H:%M:%S.%f")
return(d)
def extract_weekday(date):
return(date.weekday())
def extract_hour(date):
return(date.hour)
def extract_month(date):
return(date.month)
def extract_year(date):
return(date.year)
def get_day_mean_std(nrows, path, assignments):
col_loaded = ['DATE', 'ASS_ASSIGNMENT', 'CSPL_RECEIVED_CALLS']
data = pd.read_csv(path, sep = ";",usecols = col_loaded, nrows = nrows)
nrows = len(data.index) #In case smth went funny
data.sort_values(["ASS_ASSIGNMENT", "DATE"], inplace = True)
#print(data)
n_ass = len(assignments)
preproc_data = []
mean_ass = []
std_ass = []
rcvcalls = []
for i in range(n_ass):
assignment = assignments[i]
preproc_data.append(data.loc[data.loc[:,"ASS_ASSIGNMENT"] == assignment, :])
preproc_data[i] = preproc_data[i].drop(["ASS_ASSIGNMENT"],1)
nrows = len(preproc_data[i].index)
#print(str(nrows) + " rows to process.")
#print("Grouping by dates...")
rcvcall = preproc_data[i].groupby(["DATE"])['CSPL_RECEIVED_CALLS'].sum()
#rcvcall.set_index(['DATE'])
preproc_data[i] = preproc_data[i].groupby(["DATE"]).mean()
preproc_data[i].loc[:, 'CSPL_RECEIVED_CALLS'] = rcvcall
preproc_data[i].loc[:, 'DATE'] = preproc_data[i].index
nrows = len(preproc_data[i])
#print("Feature engineering...")
dates = preproc_data[i]['DATE'].apply(extract_date, 1)
preproc_data[i].loc[:,"WEEKDAY"] = dates.apply(extract_weekday, 1)
preproc_data[i].loc[:,"HOUR"] = dates.apply(extract_hour, 1)
preproc_data[i].loc[:,"MONTH"] = dates.apply(extract_month, 1)
preproc_data[i].loc[:,"DATE"] = dates
preproc_data[i].loc[:,"MONTH_YEAR"] = preproc_data[i].loc[:,"MONTH"] + (dates.apply(extract_year, 1) - 2011)*12 - 1
m = preproc_data[i].groupby(["MONTH_YEAR", "WEEKDAY"])["CSPL_RECEIVED_CALLS"].transform(np.mean)
s = preproc_data[i].groupby(["MONTH_YEAR", "WEEKDAY"])["CSPL_RECEIVED_CALLS"].transform(np.std)
preproc_data[i].loc[:, "WEEKDAY_MEAN"] = m
preproc_data[i].loc[:, "WEEKDAY_STD"] = s
#print preproc_data[i]
rcvcall = pd.DataFrame(rcvcall, index = dates)
# mean, std = extract_mean_std(preproc_data[i])
mean = np.zeros((36,7))
std = np.zeros((36,7))
for j in range(36):
for k in range(7):
sel = preproc_data[i].loc[preproc_data[i].loc[:,"MONTH_YEAR"] == j][preproc_data[i].loc[:,"WEEKDAY"] == k]
# print(sel)
mean[j,k] = sel["WEEKDAY_MEAN"].mean()
std[j,k] = sel["WEEKDAY_STD"].mean()
max_mean = mean.max()
max_std = std.max()
max_rcvcall = max(rcvcall['CSPL_RECEIVED_CALLS'])
if max_mean != 0:
mean_ass.append(mean/max_mean)
else:
mean_ass.append(mean)
if (max_std != 0):
std_ass.append(std/max_std)
else:
std_ass.append(std)
if (max_rcvcall != 0):
rcvcalls.append(rcvcall/max_rcvcall)
else:
rcvcalls.append(rcvcall)
#print(rcvcall)
#print (mean_ass)
#print(std_ass)
#rcvcall[3.sort_values(["DATE"], inplace = True)
#print (rcvcall)
return mean_ass, std_ass, rcvcalls, preproc_data
def extract_mean_std(data):
weekday = 0
mean = []
std = []
for i in range(len(data.index)):
if (weekday == 7):
break
if (data['WEEKDAY'].values[i] == weekday):
#print("FOUND")
mean.append(data['WEEKDAY_MEAN'].values[i])
std.append(data['WEEKDAY_STD'].values[i])
weekday += 1
while (weekday != 7):
mean.append(0)
std.append(0)
weekday += 1
return mean, std
#FOR TESTING"
if __name__ == '__main__':
ass = ['CMS', 'Crises', 'Domicile', 'Gestion', 'Gestion - Accueil Telephonique',
'Gestion Assurances', 'Gestion Relation Clienteles', 'Gestion Renault', 'Japon', 'Médical',
'Nuit', 'RENAULT', 'Regulation Medicale', 'SAP', 'Services', 'Tech. Axa', 'Tech. Inter', 'Téléphonie',
'Tech. Total', 'Mécanicien', 'CAT', 'Manager', 'Gestion Clients', 'Gestion DZ', 'RTC', 'Prestataires']
os.chdir("/home/nicolas/Documents/INF554 - Machine Learning/AXA Data Challenge")
# m_test, std_test, rcvcalls_test, preproc_test = get_day_mean_std(20000, "train_2011_2012_2013.csv", ass)
read_file_content(20000, "train_2011_2012_2013.csv", "submission.txt", rcvcall_data, preproc_data)