/
preprocess.py
165 lines (148 loc) · 4.39 KB
/
preprocess.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
import os.path
import numpy as np
from sklearn.preprocessing import LabelEncoder
import pandas
# generate the absolute path for /ai folder
def abspath(path):
return os.path.normpath(os.path.join(os.path.dirname(__file__), '../', path))
# reshape numpy tensor T to ndim = d
def np_to_ndim(T, d):
npT = np.array(T)
diff = d - npT.ndim
if diff == 0:
return npT
elif diff < 0:
npT.flatten()
else:
npT = np.expand_dims(npT, axis=0)
return np_to_ndim(npT, d)
# transform X, y into proper format in usage by deploy
def deploy_transform(mle, X, y=None):
# check dim, make X into table
npX = np.array(X)
if npX.ndim == 1:
X = [X]
X = pandas.DataFrame(X, columns=mle.header)
X = MultiFillna(X)
X = mle.fit_transform(X)
if y is None:
return X
else:
# ensure y is a list
y = np_to_ndim(y, 1)
return X, y
# check if a list x contains string
def has_str(x):
for v in x:
if isinstance(v, str):
return True
elif np.isnan([v]):
pass
else:
return False
def str_columns(X):
'''
find the names of columns of str
'''
header = list(X)
str_headers = []
for h in header:
is_string_arr = has_str(X[h])
if is_string_arr:
str_headers.append(h)
return str_headers
def MultiFillna(X, str_val='NA', num_val=0):
'''
Apply panda's fillna too all columns
if a list is of string, fill with str_val (default 'NA')
if a list is of numbers, fill with num_val (default 0)
'''
header = list(X)
fillna_dict = {}
for h in header:
is_string_arr = has_str(X[h])
if is_string_arr:
fillna_dict[h] = str_val
return X.fillna(fillna_dict).fillna(num_val)
class MultiLabelEncoder:
'''
Apply sklearn.preprocessing.LabelEncoder to all columns
Used to encode string categories into integers, e.g. ['male', 'female'] -> [0, 1]
'''
def __init__(self,columns = None):
self.header = None
self.columns = columns # array of column names to encode
self.encoders = {}
def save(self,model_path):
'''
Save the LabelEncoder classes, effectively this whole multiencoder under model_path as npz file.
Save the X header too.
'''
path = model_path + '/encoder.npz'
h_path = model_path + '/header.npz'
class_dict = {}
for k, v in self.encoders.items():
class_dict[k] = v.classes_
np.savez(path, **class_dict)
np.savez(h_path, header=self.header)
return self
def restore(self,model_path):
'''
Restore a saved multiencoder from path using npz file, by reconstructing the LabelEncoders with the classes.
Restore the X header too.
'''
path = model_path + '/encoder.npz'
h_path = model_path + '/header.npz'
npzfile = np.load(path)
h_npzfile = np.load(h_path)
self.header = h_npzfile['header']
self.encoders = {}
for k,v in npzfile.items():
le = LabelEncoder()
le.classes_ = v
self.encoders[k] = le
self.columns = list(self.encoders.keys())
return self
def fit(self,X,y=None):
return self # not relevant here
def transform(self,X):
output = X.copy()
# save the ordered headers if haven't yet
if self.header is None:
self.header = list(X)
# to automatically aim for str
str_headers = str_columns(X)
if self.columns is not None:
# will always transform str columns
self.columns = list(set(self.columns) | set(str_headers))
for colname in self.columns:
le = LabelEncoder()
output[colname] = le.fit_transform(output[colname])
self.encoders[colname] = le
else:
for colname,col in output.iteritems():
le = LabelEncoder()
output[colname] = le.fit_transform(col)
self.encoders[colname] = le
self.columns = list(self.encoders.keys())
return output
def fit_transform(self,X,y=None):
'''
The transform method that is compatible with Pipeline.
'''
return self.fit(X,y).transform(X)
def inverse_transform(self,X):
'''
Inverse of transform() above, using the self.le LabelEncoder of this class instance.
'''
output = X.copy()
if self.columns is not None:
for colname in self.columns:
le = self.encoders[colname]
output[colname] = le.inverse_transform(output[colname])
else:
for colname,col in output.iteritems():
le = self.encoders[colname]
output[colname] = le.inverse_transform(col)
self.columns = list(self.encoders.keys())
return output