-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.py
More file actions
36 lines (32 loc) · 1.63 KB
/
Copy pathtemp.py
File metadata and controls
36 lines (32 loc) · 1.63 KB
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
def kfold_stats_feature(df_tr, df_te, feats, n_splits):
folds = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=2020) # 这里最好和后面模型的K折交叉验证保持一致
df_tr['fold'] = None
for fold_, (trn_idx, val_idx) in enumerate(folds.split(df_tr, df_tr['isDefault'])):
df_tr.loc[val_idx, 'fold'] = fold_
kfold_features = []
for feat in feats:
nums_columns = ['isDefault']
for f in nums_columns:
colname = feat + '_' + f + '_kfold_mean'
kfold_features.append(colname)
df_tr[colname] = None
for fold_, (trn_idx, val_idx) in enumerate(folds.split(df_tr, df_tr['isDefault'])):
tmp_trn = df_tr.iloc[trn_idx]
order_label = tmp_trn.groupby([feat])[f].mean()
tmp = df_tr.loc[df_tr.fold == fold_, [feat]]
df_tr.loc[df_tr.fold == fold_, colname] = tmp[feat].map(order_label)
# fillna
global_mean = df_tr[f].mean()
df_tr.loc[df_tr.fold == fold_, colname] = df_tr.loc[df_tr.fold == fold_, colname].fillna(global_mean)
df_tr[colname] = df_tr[colname].astype(float)
for f in nums_columns:
colname = feat + '_' + f + '_kfold_mean'
df_te[colname] = None
order_label = df_tr.groupby([feat])[f].mean()
df_te[colname] = df_te[feat].map(order_label)
# fillna
global_mean = df_tr[f].mean()
df_te[colname] = df_te[colname].fillna(global_mean)
df_te[colname] = df_te[colname].astype(float)
del df_tr['fold']
return df_tr, df_te