-
Notifications
You must be signed in to change notification settings - Fork 14
/
data_transform.py
479 lines (372 loc) · 15.1 KB
/
data_transform.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 1 13:32:30 2020
@author: wu
"""
# COPYRIGHT:
# AUTH: jian.wu
# Email:fengyuguohou2010@hotmail.com
# 2020-08-02
'''
Life is short, You need Python~
'''
from sklearn.preprocessing import LabelEncoder
import numpy as np
from numpy import linalg
from numpy.linalg import norm
from scipy.spatial.distance import squareform, pdist
from sklearn.decomposition import NMF
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
# We import sklearn.
import sklearn
from sklearn.manifold import TSNE
from sklearn.datasets import load_digits
from sklearn.preprocessing import scale
from time import time
import seaborn as sns
from sklearn import ensemble
from sklearn.preprocessing import OneHotEncoder
def diss(a,b):
'''
define distance
'''
return np.sum((a-b)**2,axis=1)
def scatter(x,colors,n):
# We choose a color palette with seaborn.
palette = np.array(sns.color_palette("hls", n))
# We create a scatter plot.
f = plt.figure(figsize=(8, 8))
ax = plt.subplot(aspect='equal')
if type(colors)!=type(None):
sc = ax.scatter(x[:,0], x[:,1], lw=0, s=40,
c=palette[colors.astype(np.int)])
else:
sc = ax.scatter(x[:,0], x[:,1], lw=0, s=40)
plt.xlim(-25, 25)
plt.ylim(-25, 25)
ax.axis('off')
ax.axis('tight')
# We add the labels for each digit.
txts = []
if type(colors)!=type(None):
for i in range(n):
# Position of each label.
xtext, ytext = np.median(x[colors == i, :], axis=0)
txt = ax.text(xtext, ytext, str(i), fontsize=24)
# txt.set_path_effects([
# PathEffects.Stroke(linewidth=5, foreground="w"),
# PathEffects.Normal()])
txts.append(txt)
return None
class Unsupervised():
"""Unsupervised feature engineering(Kmeans, PCA, NMF and Tsen)
Parameters
----------
Data :Import pandas table include the original features.
y : target, can be None.
col: Name of the original features.
"""
def __init__(self,Data,col=None,y=None):
self.Data = Data
self.y = y
self.col = col
self.uk_p=None
self.kpre=None
self.pca=None
self.nmf=None
self.digits_proj=None
self.tsne_p=None
self.tpre=None
def _check(self,col,data):
if col==None:
raise ValueError('No import features')
else:
return data[col].values
def unkmeans(self):
"""create the kmeans
"""
X=self._check(self.col,self.Data)
estimator = KMeans(n_clusters=2)#build Kmeans
estimator.fit(X)#kmeans
centroids = estimator.cluster_centers_ #center of it
self.kpre=estimator.predict(X)
# ac0,ac1=diss(X,centroids[0]),diss(X,centroids[1])#distance
# aw0,aw1=int(np.where(ac0==np.min(ac0))[0]),int(np.where(ac1==np.min(ac1))[0])
self.uk_p=centroids
return self
def _var(self,var,Data,col):
"""create the feature
"""
X=self._check(col,Data)
var0,var1=diss(X,var[0]),diss(X,var[1])
return var0,var1
def _unk_var(self):
"""create the feature of kmeans
"""
if type(self.uk_p)==type(None):
raise ValueError('NO Kmeans have been built')
else:
self.Data['k0'],self.Data['k1']=self._var(self.uk_p,self.Data,self.col)
return self
def unk_var(self,Data):
"""create the feature of kmeans
"""
if type(self.uk_p)==type(None):
raise ValueError('NO Kmeans have been built')
else:
Data['k0'],Data['k1']=self._var(self.uk_p,Data,self.col)
return Data
def unPca(self):
"""create the Pca
"""
X=self._check(self.col,self.Data)
self.pca = PCA(n_components=2)
self.pca.fit(X)
return self
def unNmf(self):
"""create the Nmf
"""
X=self._check(self.col,self.Data)
for l in self.col:
self.Data[l+str('e')]=self.Data[l].apply(np.exp)
self.coll=[l+str('e') for l in self.col]
X=self._check(self.coll,self.Data)
self.nmf = NMF(n_components=2)
self.nmf.fit(X)
return self
def untsne(self):
"""create the Tsne
"""
X=self._check(self.col,self.Data)
self.digits_proj = TSNE(random_state=2018).fit_transform(X)
estimator = KMeans(n_clusters=2)#构造聚类器
estimator.fit(self.digits_proj)#聚类
self.tpre=estimator.predict(self.digits_proj)
centroids = estimator.cluster_centers_ #获取聚类中心
ac0,ac1=diss(self.digits_proj,centroids[0]),diss(self.digits_proj,centroids[1])#distance
aw0,aw1=int(np.where(ac0==np.min(ac0))[0]),int(np.where(ac1==np.min(ac1))[0])
self.tsne_p=[X[aw0],X[aw1]]
def _unt_var(self):
"""create the feature of kmeans
"""
if type(self.tsne_p)==type(None):
raise ValueError('NO tsen have been built')
else:
self.Data['t0'],self.Data['t1']=self._var(self.tsne_p,self.Data,self.col)
return self
def unt_var(self,Data):
"""create the feature of kmeans
"""
if type(self.tsne_p)==type(None):
raise ValueError('NO tsen have been built')
else:
Data['k0'],Data['k1']=self._var(self.tsne_p,Data,self.col)
return Data
def unsum(self):
X=self._check(self.col,self.Data)
kmeans=self.unkmeans()
pca=self.unPca()
nmf=self.unNmf()
tsne=self.untsne()
self._unk_var()
self._unt_var()
X1=self._check(self.coll,self.Data)
pcae=self.pca.transform(X)
nmfe=self.nmf.transform(X1)
self.Data['p0'],self.Data['p1']=pcae[:,0],pcae[:,1]
self.Data['n0'],self.Data['n1']=nmfe[:,0],nmfe[:,1]
if type(self.y)==type(None):
return self.Data[['k0','k1','p0','p1','n0','n1','t0','t1']]
else:
return self.Data[['k0','k1','p0','p1','n0','n1','t0','t1',self.y]]
def make_re(self,da):
X=self._check(self.col,da)
for l in self.col:
da[l+str('e')]=da[l].apply(np.exp)
X1=self._check(self.coll,da)
pcae=self.pca.transform(X)
nmfe=self.nmf.transform(X1)
da['p0'],da['p1']=pcae[:,0],pcae[:,1]
da['n0'],da['n1']=nmfe[:,0],nmfe[:,1]
da['t0'],da['t1']=self._var(self.tsne_p,da,self.col)
da['k0'],da['k1']=self._var(self.uk_p,da,self.col)
if type(self.y)==type(None):
return da[['k0','k1','p0','p1','n0','n1','t0','t1']]
else:
return da[['k0','k1','p0','p1','n0','n1','t0','t1',self.y]]
def t_plot(self):
"""plot the Tsen with y
"""
return scatter(self.digits_proj,self.Data[self.y].values,2)
def t_plot_1(self):
"""plot the Tsen with itself
"""
return scatter(self.digits_proj,self.tpre,2)
def k_plot(self):
"""plot the kmeans with y
"""
return scatter(self.Data[['k0','k1']].values,self.Data[self.y].values,2)
def k_plot_1(self):
"""plot the kmeasn with itself
"""
return scatter(self.Data[['k0','k1']].values,self.kpre,2)
class feature_eng(object):
def __init__(self,col1,col2,y, Data,Test_data=None):
'''
input:
Data
col1 数值型变量的list
col2 非数值型变量list
'''
self.col1=col1
self.col2=col2
self.col=col1+col2
self.y=y
self.data=Data[self.col+[y]]
if self.check(Test_data):
self.test_data=Test_data[self.col+[y]]
else:
self.test_data=None
def check(self,x):
if x is not None:
return True
else:
return False
def drop_missing(self,axis=0):
'''
drop missing col
'''
self.data = self.data.dropna(axis=axis,inplace=False)
if self.check(self.test_data):
self.test_data = self.test_data.dropna(axis=axis,inplace=False)
return self
#数值型变量填充
def na_fill(self,drop=True):
'''
col is the feature list want to fill na
drop=Ture drop the org feature
'''
# data_copy = self.data.copy(deep=True)
for i in self.col:
if self.data[i].isnull().sum()>0:
self.data[i+'_mean'] = self.data[i].fillna(self.data[i].mean())
self.data[i+'_median'] = self.data[i].fillna(self.data[i].median())
if self.test_data is not None:
self.test_data[i+'_mean'] = self.test_data[i].fillna(self.test_data[i].mean())
self.test_data[i+'_median'] = self.test_data[i].fillna(self.test_data[i].median())
if drop:
del self.data[l]
del self.test_data[l]
return self
#离散变量处理,label_encode
def label_encode(self):
def la(da,test,l):
l_encoder = LabelEncoder()
l_encoder.fit(da[l].values)
da[l+'l_encode'] = l_encoder.transform(da[l])
if test is not None:
test[+'l_encode'] = l_encoder.transform(test[l])
return da[l+'l_encode'],test[l+'l_encode']
else:
return da[l+'l_encode']
for i in self.col2:
if self.test_data is not None:
self.data[i+'_l_encode'],self.test_data[i+'_l_encode']=la(self.data,self.test_data,i)
else:
self.data[i+'_l_encode']=la(self.data,self.test_data,i)
return self
#离散变量按照计算woe处理
def woe_encode(self):
def Cal_WOE(df, x, y):
grouped = df[y].groupby(df[x])
df_agg = grouped.agg(['sum', 'count'])
t1 = df_agg['sum'].sum()
t0 = df_agg['count'].sum() - t1
n_c = len(df_agg['sum'])
x_woe = list(range(n_c))
for i in range(n_c):
t1_i = float(df_agg.iloc[i, 0])
t0_i = float(df_agg.iloc[i, 1] - t1_i)
if t1_i == 0 and t0_i != 0:
x_woe[i] = -1
elif t1_i != 0 and t0_i == 0:
x_woe[i] = 1
elif t1_i == 0 and t0_i == 0:
x_woe[i] = 0
else:
x_woe[i] = np.log( (t1_i/t1) / (t0_i/t0) )
df_agg[x + '_woe'] = x_woe
return df_agg
for x in self.col2:
df_agg = Cal_WOE(self.data, x, self.y)
if self.check(self.test_data):
nn=list(self.test_data[x].unique())
inde=list(df_agg.index)
na=[l for l in nn if l not in inde]
inde=inde+na
ll=list(df_agg[x + '_woe'])+[np.nan]*len(na)
self.data[x+'_woe_encode'] = self.data[x].replace(inde, ll)
self.test_data[x+'_woe_encode'] = self.test_data[x].replace(inde, ll)
else:
self.data[x+'_woe_encode'] = self.data[x].replace(df_agg.index, df_agg[x + '_woe'])
return self
#根据逾期密度的tif-idf,有时候很好用,慎用
def y_tf_idf(self):
def cal_tfidf(df,x,y):
grouped = df[y].groupby(df[x])
df_agg = grouped.agg(['sum', 'count'])
t1 = df_agg['sum'].sum()
t0 = df_agg['count'].sum()
df_agg['tf']=df_agg['sum']/float(t1)
df_agg['idf']=(float(t0)/df_agg['count']).apply(np.log)
df_agg['tf_idf']=df_agg['tf']*df_agg['idf']
return df_agg
for x in self.col2:
df_agg = cal_tfidf(self.data, x, self.y)
if self.check(self.test_data):
nn=list(self.test_data[x].unique())
inde=list(df_agg.index)
na=[l for l in nn if l not in inde]
inde=inde+na
ll=list(df_agg['tf_idf'])+[np.nan]*len(na)
self.data[x+'_y_tfidf'] = self.data[x].replace(inde, ll)
self.test_data[x+'_y_tfidf'] = self.test_data[x].replace(inde, ll)
else:
self.data[x+'_y_tfidf'] = self.data[x].replace(df_agg.index, df_agg['tf_idf'])
return self
#根据四种无监督变量衍生方法(基于Kmeans, PCA, NMF and T-sne)
def uns(self,col):
self.unsp= Unsupervised(self.data,col,self.y)
self.data[['k0','k1','p0','p1','n0','n1','t0','t1']]=self.unsp.unsum()[['k0','k1','p0','p1','n0','n1','t0','t1']]
if self.check(self.test_data):
self.test_data[['k0','k1','p0','p1','n0','n1','t0','t1']]=self.unsp.make_re(self.test_data)[['k0','k1','p0','p1','n0','n1','t0','t1']]
return self
#gbdt embeding
def gbdt_e(self,col,nEst = 20,depth = 2,learnRate = 0.005,subSamp = 0.5,onehot=False):
MModel = ensemble.GradientBoostingRegressor(n_estimators=nEst,max_depth=depth,learning_rate=learnRate,subsample = subSamp,loss='ls')
MModel.fit(self.data[col],self.data['y'])
mm=MModel.apply(self.data[col])
if onehot:
enc = OneHotEncoder()
enc.fit(MModel.apply(self.data[col]))#将位置码转化为01码
new_feature_train=enc.transform(MModel.apply(self.data[col]))
shap=new_feature_train.shape[1]
ccol=[str(i)+'gbdt_onehot' for i in range(shap)]
for i in range(len(ccol)):
self.data[ccol[i]]=new_feature_train[:,i]
if self.check(self.test_data):
nnew=nc.transform(MModel.apply(self.test_data[col]))
for i in range(len(ccol)):
self.test_data[ccol[i]]=nnew[:,i]
else:
new_feature_train=MModel.apply(self.data[col])
shap=new_feature_train.shape[1]
ccol=[str(i)+'gbdt' for i in range(shap)]
for i in range(len(ccol)):
self.data[ccol[i]]=new_feature_train[:,i]
if self.check(self.test_data):
nnew=MModel.apply(self.test_data[col])
for i in range(len(ccol)):
self.test_data[ccol[i]]=nnew[:,i]