-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpreprocess.py
159 lines (128 loc) · 4.82 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
import datetime
import CONSTANT
from util import log, timeit
from sklearn.preprocessing import PolynomialFeatures
from sklearn.feature_selection import VarianceThreshold, SelectKBest, f_classif
import pandas as pd
import numpy as np
FEATURE_MASK_INDEX=[]
@timeit
def feature_select_predict(X, k=100):
new_feature = np.zeros((X.shape[0], k))
feature_num = X.shape[1]
for i in range(k):
index = FEATURE_MASK_INDEX[i]
if index < feature_num:
new_feature[:, i] = np.array(X[X.columns[index]])
else:
index_i = (index - feature_num)//feature_num
index_j = (index - feature_num)%feature_num
new_feature[:, i] = np.array(X[X.columns[index_i]])*np.array(X[X.columns[index_j]])
return pd.DataFrame(new_feature)
@timeit
def cross_feature_and_select_poly2(X, y, k=100):
scores = list()
indexes = list()
feature_num = len(X.columns)
new_feature = np.zeros((X.shape[0], k))
## generate cross features
# first order feature
for i in range(feature_num):
column = X.columns[i]
F, P = f_classif(np.array(X[column]).reshape(-1, 1), np.array(y))
if len(scores) < k:
new_feature[:,len(scores)] = np.array(X[column])
scores.append(F[0])
indexes.append(i)
else:
if F[0]>min(scores):
index = scores.index(min(scores))
new_feature[:, index] = np.array(X[column])
scores[index] = F[0]
indexes[index] = i
## second order feature
for i in range(feature_num):
for j in range(feature_num):
cross_feature = np.array(X[X.columns[i]])*np.array(X[X.columns[j]])
F, P = f_classif(cross_feature.reshape(-1, 1), np.array(y))
if len(scores) < k:
cross_index = i*feature_num+j+feature_num
new_feature[:,len(scores)] = cross_feature
scores.append(F[0])
indexes.append(cross_index)
else:
if F[0]>min(scores):
cross_index = i*feature_num+j+feature_num
index = scores.index(min(scores))
new_feature[:, index] = cross_feature
scores[index] = F[0]
indexes[index] = cross_index
global FEATURE_MASK_INDEX
FEATURE_MASK_INDEX = indexes
return pd.DataFrame(new_feature)
@timeit
def polyfeatures(X):
poly = PolynomialFeatures(degree=2, include_bias=False, interaction_only=False)
X_poly = poly.fit_transform(X)
X = pd.DataFrame(X_poly, columns=poly.get_feature_names())
return X
@timeit
def clean_tables(tables):
for tname in tables:
log(f"cleaning table {tname}")
clean_df(tables[tname])
@timeit
def clean_df(df):
fillna(df)
@timeit
def fillna(df):
for c in [c for c in df if c.startswith(CONSTANT.NUMERICAL_PREFIX)]:
df[c].fillna(-1, inplace=True)
for c in [c for c in df if c.startswith(CONSTANT.CATEGORY_PREFIX)]:
df[c].fillna("0", inplace=True)
for c in [c for c in df if c.startswith(CONSTANT.TIME_PREFIX)]:
df[c].fillna(datetime.datetime(1970, 1, 1), inplace=True)
for c in [c for c in df if c.startswith(CONSTANT.MULTI_CAT_PREFIX)]:
df[c].fillna("0", inplace=True)
for c in [c for c in df if "mul_feature_" in c]:
df[c].fillna("0", inplace=True)
@timeit
def remove_trivial_features_in_tables(tables):
for tname in tables:
log(f"Processing table {tname}")
remove_trivial_features(tables[tname])
@timeit
def remove_trivial_features(df):
count = 0
for c in df.columns:
if c.split('_')[0] == 'm':
continue
if len(df[c].unique()) == 1:
count += 1
df.drop(c, axis=1, inplace=True)
#diff = df.max() - df.min()
#df = df[df.columns[diff>threshold]]
#df.drop(df.columns[diff>threshold], axis=1, inplace=True)
print("There are %d columns of trivial features"%(count))
#return df
@timeit
def feature_engineer(df, config):
transform_categorical_hash(df)
transform_datetime_jinnian(df, config)
#transform_datetime(df, config)
@timeit
def transform_datetime_jinnian(df, config):
for c in [c for c in df if c.startswith(CONSTANT.TIME_PREFIX)]:
df[c] = df[c].values.astype('float32')
#print(df[c])
@timeit
def transform_datetime(df, config):
for c in [c for c in df if c.startswith(CONSTANT.TIME_PREFIX)]:
df.drop(c, axis=1, inplace=True)
@timeit
def transform_categorical_hash(df):
for c in [c for c in df if c.startswith(CONSTANT.CATEGORY_PREFIX)]:
df[c] = df[c].apply(lambda x: int(x))
# for c in [c for c in df if c.startswith(CONSTANT.MULTI_CAT_PREFIX)]:
# df["mul_feature_" + c] = df[c].str.split(",")
# df[c] = df["mul_feature_" + c].apply(lambda x: int(x[0]))