-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_filler.py
202 lines (157 loc) · 8.06 KB
/
data_filler.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
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
import csv
import numpy as np
from datetime import datetime, date
from pandas import read_csv
assignments = ['T\xc3\xa9l\xc3\xa9phonie', 'Finances PCX', 'RTC', 'Gestion Renault', 'Nuit', 'Gestion - Accueil Telephonique',
'Regulation Medicale', 'Services', 'Tech. Total', 'Gestion Relation Clienteles', 'Crises', 'Japon', 'M\xc3\xa9dical',
'Gestion Assurances', 'Domicile', 'Gestion', 'SAP', 'Medicine', 'LifeStyle', 'Technical', 'TAI - RISQUE SERVICES',
'RENAULT', 'TAI - CARTES', 'TAI - SERVICE', 'TAI - RISQUE', 'TAI - PNEUMATIQUES', 'Gestion Amex',
'Maroc - G\xc3\xa9n\xc3\xa9riques', 'TPA', 'Tech. Inter', 'A DEFINIR', 'Technique Belgique', 'Technique International',
'Gestion Clients', 'Manager', 'Tech. Axa', 'DOMISERVE', 'Truck Assistance', 'NL Technique', 'R\xc3\xa9ception', 'CAT',
'Gestion DZ', 'NL M\xc3\xa9dical', 'M\xc3\xa9canicien', 'TAI - PANNE MECANIQUE', 'FO Remboursement', 'CMS',
'Maroc - Renault', 'Divers', 'Prestataires', 'AEVA', 'Evenements', 'KPT', 'IPA Belgique - E/A MAJ', 'Juridique']
DAYS_OF_WEEK_IDX = [0, 1, 3, 4, 5, 6, 2]
DAYS_OF_WEEK = ['Dimanche', 'Lundi', 'Samedi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi']
NB_ASS = len(assignments)
COL_1 = np.ones(NB_ASS)
ID_ASS = np.identity(NB_ASS)
NB_SLOTS = 39090 # estimation
# Construct X_raw
def raw_train_data(phone_datafile='sums.csv', weather_datafile='meteo_cleaned.csv'):
beginning = datetime.now()
X_raw = np.zeros((NB_SLOTS * NB_ASS, 5 + 7 + NB_ASS + 282 + 1))
# cols : year, month, day, hour, minute, day of week (7 booleans), assignments (NB_ASS booleans), 282 weather data, nb calls
print("Taille de X_raw : " + str(X_raw.shape))
# Fill X_raw
with open(phone_datafile) as f:
reader = csv.reader(f)
print("Lecture du fichier CSV de données téléphoniques")
beginning_phone = datetime.now()
l = 0
for idx, row in enumerate(reader):
l += 1
# Date
d = datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S.000")
X_raw[idx*NB_ASS:(idx+1)*NB_ASS, 0] = d.year * COL_1
X_raw[idx*NB_ASS:(idx+1)*NB_ASS, 1] = d.month * COL_1
X_raw[idx*NB_ASS:(idx+1)*NB_ASS, 2] = d.day * COL_1
X_raw[idx*NB_ASS:(idx+1)*NB_ASS, 3] = d.hour * COL_1
X_raw[idx*NB_ASS:(idx+1)*NB_ASS, 4] = d.minute * COL_1
# Day of week (booleans)
for i in range(7):
X_raw[idx*NB_ASS:(idx+1)*NB_ASS, 5+i] = int(row[2+i]) * COL_1
# Which assignment?
X_raw[idx*NB_ASS:(idx+1)*NB_ASS, 12:12+NB_ASS] = ID_ASS
# How many calls for this (date, assignment)?
for i in range(NB_ASS):
X_raw[idx*NB_ASS+i, -1] = row[9+i]
X_raw = X_raw[:l*NB_ASS, :]
print("Nombre de lignes lues : %d" % l)
ending_phone = datetime.now()
print(" durée : " + str(ending_phone - beginning_phone))
print("")
# Fill weather data
with open(weather_datafile) as f:
print("Lecture du fichier CSV de données météo")
beginning_weather = datetime.now()
X_weather = read_csv(f, sep=",", index_col=0, header=None, parse_dates=True)
for i in range(l):
d = datetime(int(X_raw[i*NB_ASS, 0]), int(X_raw[i*NB_ASS, 1]), int(X_raw[i*NB_ASS, 2]), int(X_raw[i*NB_ASS, 3]))
if len(X_weather[d.strftime("%Y-%m-%d %H:%M:%S")].values) > 0:
data_weather = X_weather[d.strftime("%Y-%m-%d %H:%M:%S")].values[0]
X_raw[i*NB_ASS:(i+1)*NB_ASS, 12+NB_ASS:12+NB_ASS+282] = np.dot(COL_1.reshape((NB_ASS,1)), data_weather.reshape((1,282)))
ending_weather = datetime.now()
print(" durée : " + str(ending_weather - beginning_weather))
print("")
ending = datetime.now()
print("Durée de chargement des données d'entrainement : " + str(ending-beginning))
print("")
return X_raw, X_weather
# Construct X_test, train estimator and predict
def test_data(X_raw, X_weather, estimator, submission_file='submission.txt', scaler=None, output_file='output'):
results = []
beginning_total = datetime.now()
with open(submission_file) as f:
reader = csv.reader(f, delimiter='\t')
print("Lecture du fichier " + submission_file)
reader.next()
current_line = reader.next()
initial_date_str = current_line[0]
initial_date = datetime.strptime(initial_date_str, "%Y-%m-%d %H:%M:%S.000")
current_date_str = initial_date_str
current_date = initial_date
# Iterate while there is a line
finished = False
while not finished:
# Iterate until date changes
print("Date : " + current_date_str)
beginning_date = datetime.now()
X_test = np.zeros((2*24 * NB_ASS, 5 + 7 + NB_ASS + 282))
# cols : year, month, day, hour, minute, day of week (7 booleans), assignments (NB_ASS booleans), 282 weather data
idx = 0
while initial_date.date() == current_date.date():
# Date
X_test[idx, 0] = current_date.year
X_test[idx, 1] = current_date.month
X_test[idx, 2] = current_date.day
X_test[idx, 3] = current_date.hour
X_test[idx, 4] = current_date.minute
# Day of week
for i in range(7):
X_test[idx, 5+i] = 1 if i == DAYS_OF_WEEK_IDX[int(current_date.strftime("%w"))] else 0
# Assignment
for i in range(NB_ASS):
X_test[idx, 12+i] = 1 if assignments[i] == current_line[1] else 0
idx += 1
try:
current_line = reader.next()
current_date_str = current_line[0]
current_date = datetime.strptime(current_date_str, "%Y-%m-%d %H:%M:%S.000")
except StopIteration:
finished = True
break
# Extract filled cells from X_test
X_test = X_test[:idx, :]
print("Taille de X_test : " + str(X_test.shape))
# Add weather data
for r in X_test:
d = datetime(int(r[0]), int(r[1]), int(r[2]), int(r[3]))
if len(X_weather[d.strftime("%Y-%m-%d %H:%M:%S")].values) > 0:
r[12+NB_ASS:12+NB_ASS+282] = X_weather[d.strftime("%Y-%m-%d %H:%M:%S")].values[0]
# Select training data from X_raw
X_train = np.zeros(X_raw.shape)
rr = 0
for r in X_raw:
if r[0] == 0.0:
continue
if date(int(r[0]), int(r[1]), int(r[2])) < current_date.date():
X_train[rr, :] = r
rr += 1
X_train = X_train[:rr, :]
X_train, y_train = X_train[:, :-1], X_train[:, -1]
# Train clf with good data
if scaler is not None:
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
beginning_training = datetime.now()
print("Début de l'entrainement")
estimator.fit(X_train, y_train)
ending_training = datetime.now()
y_test = estimator.predict(X_test)
ending_prediction = datetime.now()
results.append(y_test)
print("Taille de y_test : " + str(y_test.shape))
with open(output_file, 'a') as of:
for i in range(y_test.shape[0]):
print(y_test[i])
of.write("%f\n" % i)
print("")
print("Durée d'entrainement : " + str(ending_training - beginning_training))
print("Durée de prédiction : " + str(ending_prediction - ending_training))
print("Durée totale pour cette date : " + str(ending_prediction - beginning_date))
initial_date = current_date
ending_total = datetime.now()
print("Durée totale pour toutes les dates : " + str(ending_total - beginning_total))
return results