-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_io.py
286 lines (259 loc) · 12.3 KB
/
data_io.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
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
#Based on kaggle sample code https://github.com/benhamner/JobSalaryPrediction.git
import csv
import json
import os
import pickle
from os.path import join as path_join
from os.path import isfile
import joblib
import numpy as np
from sklearn.preprocessing import LabelEncoder
from scipy.io import mmread
from sklearn.metrics import mean_absolute_error
from sklearn.base import clone
from scipy.sparse import coo_matrix, hstack
try:
import cloud
on_cloud = cloud.running_on_cloud()
except Exception as e:
on_cloud = False
class DataIO(object):
def __init__(self, paths_name, cache=True):
self.paths_name = paths_name
self.paths = self._get_paths()
if not on_cloud and cache:
memory = joblib.Memory(cachedir=self.cache_dir)
self.get_le_features = memory.cache(self.get_le_features)
self.get_features = memory.cache(self.get_features)
self.is_log = False
def _get_paths(self):
paths = json.loads(open(self.paths_name).read())
data_path = os.path.expandvars(paths["data_path"])
for key in paths:
paths[key] = os.path.join(data_path, os.path.expandvars(paths[key]))
self.data_dir = paths["data_path"]
self.cache_dir = paths["cache_dir"]
self.prediction_dir = paths["prediction_dir"]
self.models_dir = paths["models_dir"]
self.submission_path = paths["submission_path"]
self.train_file = paths["train_data_path"]
#self.valid_file = paths["valid_data_path"]
#self.test_file = paths["test_data_path"]
return paths
def _check_type_n(self, type_n):
if type_n == "train":
file_id = "train_data_path"
elif type_n == "train_and_valid":
file_id = "train_and_valid"
elif type_n == "train_and_test":
file_id = "train_and_test"
elif type_n == "test_full":
file_id = "test_data_path"
elif type_n == "train_full":
if "train_full_data_path" in self.paths:
file_id = "train_full_data_path"
else:
file_id = "train_data_path"
elif type_n == "valid" or type_n == "valid_full":
file_id = "valid_data_path"
elif type_n == "testno" or type_n == "valid_classes" or type_n == "train_classes":
file_id = "uknown"
else:
raise ValueError("Unknown type_n: %s" % type_n)
return file_id
def get_le_features(self, columns, type_n):
file_id = self._check_type_n(type_n)
le_features = map(lambda x: self.label_encode_column_fit(
x, file_id=file_id, type_n=type_n), columns)
return le_features
def make_counts(self, preprocessor, short_id, column_names, type_n, type_v):
#count_vector_titles = CountVectorizer(
#read_column(train_filename, column_name),
#max_features=200)
file_id = self._check_type_n(type_n)
valid_file_id = self._check_type_n(type_v)
name = "%s_%s_%s_%s"
for column_name in column_names:
vocabulary_path = path_join(self.cache_dir, name % (column_name, type_n, short_id, "vocabulary"))
stop_words_path = path_join(self.cache_dir, name % (column_name, type_n, short_id, "stop_words"))
valid_path = path_join(self.cache_dir, name % (column_name, type_v, short_id, "matrix"))
cur_preprocessor = clone(preprocessor)
print "Working on %s" % column_name
if isfile(vocabulary_path) and isfile(stop_words_path):
print "vocabulary exists"
vocabulary = joblib.load(vocabulary_path)
stop_words = joblib.load(stop_words_path)
cur_preprocessor.set_params(vocabulary=vocabulary)
cur_preprocessor.set_params(stop_words=stop_words)
else:
print "Fitting train"
cur_preprocessor.set_params(input=self.read_column(file_id, column_name))
titles = cur_preprocessor.fit_transform(self.read_column(file_id, column_name))
joblib.dump(cur_preprocessor.vocabulary_, vocabulary_path)
joblib.dump(cur_preprocessor.stop_words_, stop_words_path)
print joblib.dump(titles, path_join(self.cache_dir, name % (column_name, type_n, short_id, "matrix")))
if not isfile(valid_path):
print "Fitting valid"
titles_valid = cur_preprocessor.transform(
self.read_column(valid_file_id, column_name))
print joblib.dump(titles_valid, valid_path)
def join_features(self, filename_pattern, column_names, additional_features=[], sparse=False):
#filename = "%strain_count_vector_matrix_max_f_100"
extracted = []
print("Extracting features and training model")
for column_name in column_names: # ["Title", "FullDescription", "LocationRaw", "LocationNormalized"]:
print "Extracting: ", column_name
fea = joblib.load(path_join(self.cache_dir, filename_pattern % column_name))
print fea.shape
if not sparse and hasattr(fea, "toarray"):
extracted.append(fea.toarray())
else:
extracted.append(fea)
#import ipdb; ipdb.set_trace()
#print map(len, additional_features)
# changes arrays in additional features to numpy arrays with shape(len(x), 1)
additional_features = map(lambda x: np.reshape(np.array(x), (len(x), 1)), additional_features)
if sparse:
additional_features = map(coo_matrix, additional_features)
extracted.extend(additional_features)
if len(extracted) > 1:
if sparse:
return hstack(extracted)
else:
return np.concatenate(extracted, axis=1)
else:
return extracted[0]
def get_features(self, columns, type_n, le_features):
file_id = self._check_type_n(type_n)
extra_features = map(lambda (le, name): self.label_encode_column_transform(le, name, file_id=file_id, type_n=type_n), zip(le_features, columns))
return extra_features
def read_column(self, filename_or_path, column_name):
"""returns generator with values in column_name in filename"""
if filename_or_path in self.paths:
filename = self.paths[filename_or_path]
elif isfile(filename_or_path):
filename = filename_or_path
else:
raise Exception("filename_or_path: '%s' not found" % filename_or_path)
csv_file = csv.reader(open(filename, 'r'))
header = csv_file.next()
#print header
if column_name not in header:
raise Exception("Column '%s' is not in header! Header: %s" % (column_name, ",".join(header)))
column_index = header.index(column_name)
for line in csv_file:
yield line[column_index]
def label_encode_column_fit_transform(self, column_name, file_id="train_data_path", type_n="train"):
"""Returns LabelEncoder and transformation for column
Parameters
----------
column_name: string
Which column
file_id: string
Id of file in paths. Gets filepath
type_n: string
train, test, valid. Same file id gets different files in different types. For caching
"""
le = LabelEncoder()
transformation = le.fit_transform(list(self.read_column(file_id, column_name)))
#print "classes:", list(le.classes_)
return le, transformation
def label_encode_column_fit(self, column_name, file_id="train_data_path", type_n="train"):
le = LabelEncoder()
le.fit(list(self.read_column(file_id, column_name)))
#print "classes:", list(le.classes_)
return le
def label_encode_column_transform(self, le, column_name, file_id="valid_data_path", type_n="valid"):
return le.transform(list(self.read_column(file_id, column_name)))
def read_gensim_corpus(self, filename):
"""Reads corpus in MMX format and returns Sparse scipy matrix
It assumes the corpus is in cache directory"""
corpus_csc = mmread(open(path_join(self.cache_dir, filename), "r"))
return corpus_csc
def get_salaries(self, type_n, log=False):
file_id = self._check_type_n(type_n)
salaries = np.array(list(self.read_column(file_id, "SalaryNormalized"))).astype(np.float64)
if log:
self.is_log = True
salaries = np.log(salaries)
return salaries
def compare_valid_pred(self, valid, pred):
if self.is_log:
mexp = np.exp
else:
mexp = lambda x: x
print mexp(valid[1:10])
print mexp(pred[1:10])
def error_metric(self, y_true, y_pred):
if self.is_log:
mexp = np.exp
else:
mexp = lambda x: x
return mean_absolute_error(mexp(y_true), mexp(y_pred))
#TODO: Why this doesn't work?
#if self.is_log:
#def log_mean_absolute_error(y_true, y_pred):
#return mean_absolute_error(np.exp(y_true), np.exp(y_pred))
#return log_mean_absolute_error
#else:
#def local_mean_absolute_error(y_true, y_pred):
#return mean_absolute_error(y_true, y_pred)
#return local_mean_absolute_error
def save_prediction(self, model_name, predictions, type_n):
self._check_type_n(type_n)
if on_cloud:
joblib.dump(predictions, model_name + "_prediction_" + type_n, compress=5)
cloud.bucket.put(model_name + "_prediction_" + type_n, prefix="prediction")
else:
joblib.dump(predictions, path_join(self.prediction_dir, model_name + "_prediction_" + type_n), compress=5)
def load_model(self, model_name):
in_path = path_join(self.models_dir, model_name + ".pickle")
return pickle.load(open(in_path))
def save_model(self, model, model_name, mae=None, mae_cv=None, parameters=None):
"""Saves model in model_name.pickle file
also creates model_name.txt with model parameters and
mae value on validation set if provided
:param mae MAE validation float
:param mae_cv MAE CV string
:param parameters model parameters string"""
if model_name is None:
raise ValueError("Model name is empty!")
else:
filepath = path_join(self.models_dir, model_name)
with open(filepath + '.txt', 'wb') as infofile:
infofile.write(str(model))
infofile.write("\n")
if mae is not None:
infofile.write("\nMAE validation: %f\n" % mae)
if mae_cv is not None:
infofile.write("\nMAE CV: %s\n" % mae_cv)
if parameters is not None:
infofile.write("\nParameters: %s\n" % (parameters, ))
if on_cloud:
cloud.bucket.put(filepath + '.txt', prefix="models")
out_path = filepath + ".pickle"
pickle.dump(model, open(out_path, "w"))
if on_cloud:
cloud.bucket.put(out_path, prefix="models")
def get_prediction(self, prediction_name=None, model_name=None, type_n=None):
if prediction_name is not None:
filename = prediction_name
elif model_name is not None and type_n is not None:
filename = model_name + "_prediction_" + type_n
return joblib.load(path_join(self.prediction_dir, filename))
def write_submission(self, submission_name, predictions=None, prediction_name=None, model_name=None, type_n=None, unlog=False):
submission_file_path = path_join(self.submission_path, submission_name)
writer = csv.writer(open(submission_file_path, "w"), lineterminator="\n")
valid = self.read_column("test_data_path", "Id")
if predictions is None:
predictions = self.get_prediction(prediction_name=prediction_name,
model_name=model_name,
type_n=type_n)
if unlog:
predictions = np.exp(predictions)
else:
if self.is_log:
predictions = np.exp(predictions)
rows = [x for x in zip(valid, predictions.flatten())]
writer.writerow(("Id", "SalaryNormalized"))
writer.writerows(rows)