-
Notifications
You must be signed in to change notification settings - Fork 20
/
preprocess_test_data.py
executable file
·67 lines (55 loc) · 2.25 KB
/
preprocess_test_data.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
#!/usr/bin/python
import numpy as np
import cPickle as pickle
import os
import random
from scipy import delete
from missing_data_imputation import Imputer
from processing import impute
from params import adult_params, scalers_folder
from params import feats_test_folder, labels_test_folder
from params import rand_num_seed
def set_trace():
from IPython.core.debugger import Pdb
import sys
Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)
np.random.seed(rand_num_seed)
random.seed(rand_num_seed)
# load features and labels
x = np.genfromtxt('data/adult-test-raw', delimiter=', ', dtype=object)
# binarize labels
labels = (np.array(x[:, -1]) == '>50K').astype(int)
labels = labels.reshape((-1, 1))
# save labels in binary and one-hot representations
labels.dump(os.path.join(labels_test_folder, 'labels_bin_test.np'))
# remove redundant education-number and labels features
x = delete(x, (4, 14), 1)
# instantiate Imputer
imp = Imputer()
for imp_method in adult_params['imp_methods']:
print 'Imputing with {}'.format(imp_method)
data = impute(x, imp, imp_method, adult_params)
# load respective scaler
scaler_path = os.path.join(scalers_folder,
"{}_scaler".format(imp_method))
scaler_dict = pickle.load(open(scaler_path, "rb"))
for name, scaler in scaler_dict.items():
print 'Scaling with {}'.format(name)
# scale and binarize, adding one col for missing value in all categ vars
data_scaled = np.copy(data)
data_scaled[:, adult_params['non_cat_cols']] = scaler.transform(
data[:, adult_params['non_cat_cols']].astype(float))
data_scaled_bin = imp.binarize_data(data_scaled,
adult_params['cat_cols'],
adult_params['miss_data_symbol'])
# convert to float
data_scaled_bin = data_scaled_bin.astype(float)
# add labels as last column
path = os.path.join(feats_test_folder,
'{}_bin_scaled_test.np'.format(name))
data_scaled_bin = np.hstack((data_scaled_bin, labels))
print "\tSaving imputed data to {}".format(path)
data_scaled_bin.dump(path)
del data
del data_scaled
del data_scaled_bin