-
Notifications
You must be signed in to change notification settings - Fork 0
/
kutils.py
1578 lines (1397 loc) · 69.3 KB
/
kutils.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 1 11:35:45 2018
@author: admin123
"""
import math
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
#from plotnine import *
import lightgbm as lgb
from lightgbm import LGBMClassifier
from xgboost import XGBClassifier
from catboost import CatBoostClassifier, Pool
import gc
from sklearn.metrics import roc_auc_score, precision_recall_curve, roc_curve
from sklearn.model_selection import KFold, StratifiedKFold
from sklearn.linear_model import LogisticRegressionCV
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier, \
GradientBoostingClassifier
from sklearn.neighbors import NearestNeighbors
import os, warnings, pickle, random
import time, datetime, json
from itertools import combinations
from contextlib import contextmanager
gc.enable()
#Path for input data files from Kaggle. All CSVs here will be imported
#PATH = 'C:\\Users\\alonb\\Desktop\\MS Malware 2019\\data\\'
#PATH = 'C:\\Users\\alonb\\Desktop\\MS Malware 2019\\data\\'
PATH = os.path.join(os.getcwd(), 'data')
target_var = 'HasDetections'
id_var = 'MachineIdentifier'
@contextmanager
def timer(title):
t0 = time.time()
yield
print("{}: {:.0f}s".format(title, time.time() - t0))
#Kaggle-compatible log
def log(msg):
print(msg)
#Echo for commit log
if '/kaggle' in os.getcwd():
os.system('echo %s' % msg)
# One-hot encoding for categorical columns with get_dummies
def one_hot_encoder(df, nan_as_category = True):
original_columns = list(df.columns)
categorical_columns = [col for col in df.columns if df[col].dtype == 'object']
df = pd.get_dummies(df, columns= categorical_columns, dummy_na= nan_as_category)
new_columns = [c for c in df.columns if c not in original_columns]
return df, new_columns
def reduce_mem_usage(df, cols = None):
""" iterate through all the columns of a dataframe and modify the data type
to reduce memory usage.
"""
start_mem = df.memory_usage().sum() / 1024**2
if cols is None: cols = df.columns
for col in cols:
#print(col)
col_type = df[col].dtype
#print(col_type)
if col_type.name in ('object','category'):
df[col] = df[col].astype('category')
elif ('datetime' in col_type.name):
pass
else:
#Number
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
#Integer
if df[col].nunique() < 5:
df[col] = df[col].astype('category')
elif c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
df[col] = df[col].astype(np.int8)
elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
df[col] = df[col].astype(np.int16)
elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
df[col] = df[col].astype(np.int32)
elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
df[col] = df[col].astype(np.int64)
else:
#Float
if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
df[col] = df[col].astype(np.float16)
elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
df[col] = df[col].astype(np.float32)
else:
df[col] = df[col].astype(np.float64)
end_mem = df.memory_usage().sum() / 1024**2
print('Memory {0:.1f} --> {1:.1f} MB ({2:.1f}%)'.format(start_mem,end_mem,100 * (end_mem - start_mem) / start_mem))
return df
def import_data(file):
"""create a dataframe and optimize its memory usage"""
#file = 'D:\Documents-Alon\Kaggle\Home Credit\data\HomeCredit_columns_description.csv'
print('Importing ' + file + ':')
df = pd.read_csv(file, parse_dates=True, keep_date_col=True)
df = reduce_mem_usage(df)
return df
#Load datasets
def dir_csv2pickle(path):
data = {}
import os
for file in os.listdir(path):
fname, ext = os.path.splitext(file)
if ext == '.csv':
data[fname] = import_data(path+file)
with open(path+ '\\' + fname + '.pickle', 'wb') as handle:
pickle.dump(data[fname], handle, protocol=pickle.HIGHEST_PROTOCOL)
return data;
def missing_data(data):
total = data.isnull().sum().sort_values(ascending = False)
percent = (data.isnull().sum()/data.isnull().count()*100).sort_values(ascending = False)
return pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
def outliers_iqr(ys,thresh=1.5):
quartile_1, quartile_3 = np.percentile(ys, [25, 75])
iqr = quartile_3 - quartile_1
lower_bound = quartile_1 - (iqr * thresh)
lower_bound = max(ys.min(),lower_bound)
upper_bound = quartile_3 + (iqr * thresh)
upper_bound = min(ys.max(),upper_bound)
return lower_bound, upper_bound;
def plot_target(df,var,target=[target_var]):
df = df[[var,target]]
col_type = df[var].dtype.name
if (col_type == 'category') or (df[var].nunique()<30):
#Categorical target
d2 = df.groupby([target,var]).size() / \
df.groupby([target]).size()
d2 = d2.reset_index()
d2.columns = [target,var,'Freq']
with warnings.catch_warnings():
warnings.simplefilter("ignore")
g1 = (ggplot(d2, aes(x=var, y='Freq', fill=target)) +
geom_bar(stat='identity', position='dodge') +
theme(axis_text_x = element_text(angle = 90, hjust = 1)))
else:
#Numerical target: Detect, remove outliers
xmin, xmax = outliers_iqr(df[var])
rows = df[var].shape[0]
outliers = df[ (df[var] < xmin) | (df[var] > xmax) ].shape[0]
if outliers > 0:
print('\n\nRemoved {0:2.1f}% outliers in {1}: {2:,}\n\n'.format( \
outliers/rows*100,var,outliers))
else:
xmin = df[var].min()
xmax = df[var].max()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
g1 = (ggplot(df,
aes(x=var,group=[target_var],color=[target_var])) +
geom_density() +
xlim(xmin, xmax) +
theme_seaborn())
# Calculate correlation, medians for repaid vs not repaid
corr = df[target].corr(df[var])
avg_repaid = df.loc[df[target] == 0, var].median()
avg_not_repaid = df.loc[df[target] == 1, var].median()
# print correlation + medians
print('The correlation between %s and the TARGET is %0.4f' % (var, corr))
print('Median value for loan that was not repaid = %0.4f' % avg_not_repaid)
print('Median value for loan that was repaid = %0.4f' % avg_repaid)
return g1;
def load_to_sqlite(data,path=PATH):
import sqlite3
import pandas as pd #IO
#import sqlalchemy
conn = sqlite3.connect(PATH + 'HCredit.db')
#cursor = conn.cursor()
for file in list(data):
print('Loading '+file+' to sqlite')
data[file].to_sql(file, conn, index = False,if_exists='replace')
return;
def dummies(df,exclude=[[id_var],[target_var]]):
"""Convert all categorical variables to dummies / 1-hot encoding"""
num_cols = list(df._get_numeric_data().columns)
cat_cols = list(set(df.columns) - set(num_cols) - set(exclude))
cat_cols = [c for c in cat_cols if df[c].nunique() > 2]
df2 = df.drop(columns=cat_cols)
df3 = pd.get_dummies(df[cat_cols],prefix=cat_cols)
return pd.concat([df2,df3], axis=1);
def safe_div(x,y):
if y == 0:
return 0
return x / y
def summary(df):
"""summary stats for a dataframe"""
#df = data['bureau']
df3 = df.describe(include='all',percentiles=[.01,.25,.5,.75,.99]).transpose().reset_index()
df4 = pd.DataFrame(df.dtypes).reset_index()
df4.columns = ['index','type']
df5 = pd.merge(left=df4,right=df3,on='index',how='outer')
missing = missing_data(df).reset_index()
missing = missing[['index','Percent']]
df5 = df5.merge(missing,on='index',how='left')
df5['missing'] = df5['Percent'].map('{:.1f}%'.format)
del df5['Percent']
if 'freq' in df5.columns:
freq = df5['freq'] / df.shape[0]
df5['Top1freq'] = (100*freq).map('{:.1f}%'.format)
unique = df.agg(['nunique','median']).transpose().reset_index()
df5 = df5.merge(unique,on='index')
if 'freq' in df5.columns:
cols = ['index',
'type',
'missing',
'nunique',
'top',
'Top1freq',
'min',
'1%',
'25%',
'50%',
'mean',
'median',
'75%',
'99%',
'max',
'std']
else:
cols = ['index',
'type',
'missing',
'nunique',
'min',
'1%',
'25%',
'50%',
'mean',
'median',
'75%',
'99%',
'max',
'std']
df5[cols].to_clipboard(sep=',', index=False)
return df5[cols];
def prefix(df,prefix,exclude=["SK_ID_CURR","SK_ID_PREV","TARGET"]):
cols = df.columns
return np.where(cols.isin(exclude),cols,prefix+'_'+cols);
def round_precision(x):
if math.isnan(x): return np.nan;
if x == 0: return 0;
for digits in range(6):
y = x % 10 ** digits
if (y != 0): break
return digits-1;
def fix_days_365(df):
"""Fix "DAYS" columns with outliers at 365243 --> nan"""
#df = data['previous_application'].copy()
shape_start = df.shape[1]
day_cols = [c for c in df.columns if 'DAYS' in c]
for col in day_cols:
if df[col].max() == 365243:
#print(col)
newcol = col + '_365'
df[newcol] = (df[col] == 365243)
#print((prev[col] == 365243).sum() / prev.shape[0])
df[col].replace(365243, np.nan, inplace= True)
newcols = df.shape[1] - shape_start
if newcols > 0: print('Added {} column(s) for 365 fix'.format(newcols))
return df;
#directory = 'D:\\Documents-Alon\\Kaggle\\Home Credit\\experiment\\1533067622 LGBMClassifier, AUC=79.59 ensemble'
#result = read_pickle(directory + '\\result.pickle')
#feature_importance_df_ = result['features']
#display_importances(feature_importance_df,directory)
def display_importances(feature_importance_df_, directory):
cols = feature_importance_df_[["feature", "importance"]
].groupby("feature").mean().sort_values(by="importance", ascending=False)[:50].index
best_features = feature_importance_df_.loc[feature_importance_df_.feature.isin(cols)]
plt.figure(figsize=(8, 10))
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False))
plt.title('LightGBM Features (avg over folds)')
plt.tight_layout()
plt.savefig(directory + '\\lgbm_importances.png')
plt.close('all')
def display_roc(y, train, oof_preds, folds, directory):
# Plot ROC curves
plt.figure(figsize=(6,6))
scores = []
for n_fold, (_, val_idx) in enumerate(folds.split(train,y)):
# Plot the roc curve
fpr, tpr, thresholds = roc_curve(y.iloc[val_idx], oof_preds[val_idx])
score = roc_auc_score(y.iloc[val_idx], oof_preds[val_idx])
scores.append(score)
plt.plot(fpr, tpr, lw=1, alpha=0.3, label='ROC fold %d (AUC = %0.4f)' % (n_fold + 1, score))
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Luck', alpha=.8)
fpr, tpr, thresholds = roc_curve(y, oof_preds)
score = roc_auc_score(y, oof_preds)
plt.plot(fpr, tpr, color='b',
label='Avg ROC (AUC = %0.4f $\pm$ %0.4f)' % (score, np.std(scores)),
lw=2, alpha=.8)
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('LightGBM ROC Curve')
plt.legend(loc="lower right")
plt.tight_layout()
plt.savefig(directory + '\\roc_curve.png')
# Plot ROC curves
plt.figure(figsize=(6,6))
precision, recall, thresholds = precision_recall_curve(y, oof_preds)
score = roc_auc_score(y, oof_preds)
plt.plot(recall, precision, color='b',
label='Avg ROC (AUC = %0.4f $\pm$ %0.4f)' % (score, np.std(scores)),
lw=2, alpha=.8)
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('LightGBM Recall / Precision')
plt.legend(loc="lower right")
plt.tight_layout()
plt.savefig(directory + '\\recall_precision_curve.png')
plt.close('all')
def bag_model(df_train, clf, df_test=None, submission=None,
test_split=True,
seed=42, bags=3, verbose=False,
foldername='', early_stopping_rounds=50, bootstrap=1,
recode=True, random_state=32587, savedata=True,
target_var='HasDetections', id_var='MachineIdentifier'):
from sklearn.model_selection import train_test_split
with timer('bag_model'):
model_type = type(clf).__name__
directory = save_df(df_train, savedata=savedata)
assert(all(df_train[target_var].notnull()))
feats = [c for c in df_train.columns if c not in [id_var,target_var]]
if df_test is not None:
assert(all(df_test[target_var].notnull()))
X_train, y_train = df_train[feats], df_train[target_var]
X_test, y_test = df_test[feats], df_test[target_var]
y_test = y_test.astype('float64') #Otherwise AUC overflows
X_test.index = df_test[id_var]
elif test_split:
X_train, X_test, y_train, y_test = train_test_split( \
train[feats], train[target_var], test_size=0.1, random_state=seed)
X_test.index = df_test[id_var]
else:
X_train, y_train = df_train[feats], df_train[target_var]
X_test, y_test = pd.DataFrame(), pd.Series()
print("Train shape: {}, test shape: {}".format(X_train.shape, X_test.shape))
test_preds = np.zeros((y_test.shape[0],bags))
scores = []
feature_importance_df = pd.DataFrame()
if submission is not None:
sub_preds = np.zeros((submission.shape[0],bags))
for bag in range(bags):
if 'random_state' in clf.get_params().keys():
clf.set_params(random_state = bag * 1000)
if bootstrap > 1:
indices = np.random.choice(X_train.index,
X_train.shape[0] * bootstrap)
print('bootstrapping: X_train shape={}'.format(
X_train.loc[indices].shape))
else:
indices = X_train.index
if len(X_test)>0:
if model_type in ['LGBMClassifier']:
clf.fit(X_train.loc[indices], y_train.loc[indices],
eval_set=[(X_train.loc[indices], y_train.loc[indices]),
(X_test, y_test)],
eval_metric= 'auc', verbose=verbose,
early_stopping_rounds= early_stopping_rounds)
test_preds[:,bag] = clf.predict_proba(X_test, num_iteration=clf.best_iteration_)[:, 1]
if submission is not None:
sub_preds[:,bag] = clf.predict_proba(submission[feats], num_iteration=clf.best_iteration_)[:, 1]
else:
clf.fit(X_train.loc[indices], y_train.loc[indices])
test_preds[:,bag] = clf.predict_proba(X_test)[:, 1]
if submission is not None:
sub_preds[:,bag] = clf.predict_proba(submission[feats])[:, 1]
bag_score = roc_auc_score(y_test, test_preds[:,bag])
bag_score_cum = roc_auc_score(y_test, test_preds.mean(axis=1))
if 'best_iteration_' in dir(clf):
best_iter = clf.best_iteration_
else:
best_iter = None
scores.append([bag, bag_score, bag_score_cum, best_iter])
print('Bag %d AUC: %2.4f| Cumulative AUC: %2.4f' % \
(bag, bag_score * 100, bag_score_cum * 100) )
else:
#No test set - training on full data
clf.fit(X_train.loc[indices], y_train.loc[indices])
if submission is not None:
sub_preds[:,bag] = clf.predict_proba(submission[feats])[:, 1]
print('Bag %d of %d' % (bag, bags) )
filename = directory+ '\\clf%d.pickle' % (bag)
with open(filename, 'wb') as handle:
pickle.dump(clf, handle, protocol=pickle.HIGHEST_PROTOCOL)
if ('feature_importances_' in dir(clf)):
bag_importance_df = pd.DataFrame()
bag_importance_df["feature"] = feats
bag_importance_df["importance"] = clf.feature_importances_
bag_importance_df["bag"] = bag + 1
feature_importance_df = pd.concat([feature_importance_df, bag_importance_df], axis=0)
#Save everything
with open(directory+ '\\model_params.txt', 'w') as file:
file.write(str(clf.get_params()))
#file.write(json.dumps(clf.get_params())) # use `json.loads` to do the reverse
if len(X_test)>0:
scores = pd.DataFrame(data=scores,columns=['bag','bag_score','bag_score_cum','best_iteration'])
scores.to_csv(directory + '\\scores.csv', index=False)
if ('feature_importances_' in dir(clf)):
ft = feature_importance_df.pivot(index='feature', columns='bag', values='importance')
ft['Avg'] = ft.mean(axis=1)
ft.sort_values(by=['Avg'],ascending=False,inplace=True)
ft['RankPct']= ft['Avg'].rank(pct=True)
ft = ft.reset_index()
ft.to_csv(directory + '\\feature_importance' + str(random.randint(1,1000)) + \
'.csv', index=False)
#Save submission file
if submission is not None:
bagged_sub_preds = sub_preds.mean(axis=1)
out_df = pd.DataFrame({id_var : submission[id_var],
target_var : bagged_sub_preds })
out_df.to_csv(directory + '\\submission.csv', index=False)
#Save oofs
if len(X_test)>0:
oof = pd.DataFrame({id_var: X_test.index,
target_var: test_preds.mean(axis=1) })
oof.to_csv(directory + '\\oof.csv', index=False)
if len(X_test)>0:
os.rename(directory,directory + foldername + ', AUC=%2.2f' % (bag_score_cum*100))
else:
os.rename(directory,directory + foldername + ', full train')
return scores;
def kfold_model2(df, clf, num_folds=2, bags=1, CV='kfold', recode=True,
early_stopping_rounds= 200, verbose=200, foldername=None,
resume_experiment = None, fold_only=None,
random_state=32587, fulldata=False, extraseed=456,
savedata=True, submission=True, fm_xsample = 0.1,
target_var='HasDetections',id_vars='MachineIdentifier'):
import pickle, json
from time import strftime
from datetime import datetime
if "fm.Model" in str(type(clf)):
model_type = "FM"
else:
model_type = type(clf).__name__
if recode:
if (model_type == 'LGBMClassifier') or \
(str(type(clf)) == "<class 'models.nffm.Model'>") :
df = df #Do nothing
elif (model_type in ['CatBoostClassifier','XGBClassifier']):
df, cat_cols = no_cat(df)
df.columns = [c.translate(str.maketrans('][<', '___')) for c in df.columns]
else:
df = no_miss(df) #Includes remove categories
directory = save_df(df,savedata=savedata,resume_experiment=resume_experiment)
# Divide in training/validation and test data
if df[target_var].dtype.name != 'float64':
df.loc[:,target_var] = df.loc[:,target_var].astype('float64') #So AUC doesn't overflow
train_df = df[df[target_var].notnull()]
test_df = df[df[target_var].isnull()]
feats = [f for f in train_df.columns if f not in [target_var,id_vars]]
log("Starting " + model_type + ". Train shape: {}, test shape: {}".format(train_df.shape, test_df.shape))
# Create arrays and dataframes to store results
scores = []
best_iter = []
feature_importance_df = pd.DataFrame()
oof_preds = np.zeros((train_df.shape[0],bags))
sub_preds = np.zeros((test_df.shape[0],bags))
#Start bag loop
for bag in range(bags):
#if 'random_state' in clf.get_params().keys():
try:
clf.set_params(random_state = (random_state * bag + extraseed))
except:
log('Could not set random state for model. Using random.seed and np.random.seed')
random.seed(random_state * bag + extraseed)
np.random.seed(random_state * bag + extraseed)
# Cross validation model
if CV=='stratified':
folds = StratifiedKFold(n_splits= num_folds, shuffle=True, random_state=(random_state))
split1 = folds.split(train_df[feats], train_df[target_var])
elif CV=='kfold':
folds = KFold(n_splits= num_folds, shuffle=True, random_state=(random_state))
split1 = folds.split(train_df[feats], train_df[target_var])
elif CV=='adversarial':
#Adversarial, Scheme A
folds = StratifiedKFold(n_splits= num_folds, shuffle=True, random_state=(random_state))
try:
split1 = folds.split(train_df[feats], is_test_preds['predictions'].values)
except NameError:
filepath = os.path.join(PATH, 'df_adversarial.pickle')
is_test_preds = read_pickle(filepath) #P(obs in train)
assert(len(is_test_preds)==len(train_df))
split1 = folds.split(train_df[feats], is_test_preds['predictions'].values)
else:
raise Exception('Unknown CV: %s' % CV)
break;
#Start fold loop
for n_fold, (train_idx, valid_idx) in enumerate(split1):
#In Kaggle cloud, compute one fold only
if (fold_only is not None) and (n_fold != fold_only):
log('Skipping fold %d' % n_fold)
continue;
train_x, train_y = train_df[feats].iloc[train_idx], train_df[target_var].iloc[train_idx]
valid_x, valid_y = train_df[feats].iloc[valid_idx], train_df[target_var].iloc[valid_idx]
if (model_type == "LGBMClassifier"):
#print('trace1')
#Try loading the model instead of executing it
model_name = '{}_bag{}_fold{}.pickle'.format(model_type,bag,n_fold+1)
if os.path.isfile(directory+model_name):
log('Loaded %s. Skipping fit for this fold' % (directory+model_name))
clf = read_pickle(directory+model_name)
else:
log('Fitting %s' % (model_name))
#Traditional fit disabled for now. No early stopping to accelerate the learning process.
clf.fit(train_x, train_y, eval_set=[(train_x, train_y), (valid_x, valid_y)],
eval_metric= 'auc', verbose= verbose, early_stopping_rounds= early_stopping_rounds)
#Train 20k rounds, no early stopping
#clf.fit(train_x, train_y)
elif (model_type == "XGBClassifier"):
#print('trace')
clf.fit(train_x, train_y, eval_set=[(valid_x, valid_y)],
eval_metric= 'auc', verbose= 50, early_stopping_rounds= 50)
clf.fit(train_x, train_y, eval_set=[(train_x, train_y), (valid_x, valid_y)],
eval_metric= 'auc', verbose= verbose, early_stopping_rounds= early_stopping_rounds)
elif (model_type == "CatBoostClassifier"):
bad_nan_cols = [c for c in train_x.columns if \
(valid_x[c].isnull().sum()>0) &
(train_x[c].isnull().sum() == 0)]
if len(bad_nan_cols)>0:
log(bad_nan_cols)
train_x[bad_nan_cols] = train_x[bad_nan_cols].fillna(-999)
valid_x[bad_nan_cols] = valid_x[bad_nan_cols].fillna(-999)
cat_cols_idx = [train_x.columns.get_loc(col) for col in cat_cols]
#print('catboost fit start')
clf.fit(train_x, train_y,
eval_set=[(valid_x, valid_y)],
#verbose=verbose, metric_period=verbose,
#verbose=5, metric_period=5,
early_stopping_rounds = 4,
use_best_model=True, cat_features=cat_cols_idx)
#print('catboost fit end')
#elif (str(type(clf)) == "<class 'models.nffm.Model'>"): #From ctrNet
elif (model_type=="FM"): #From ctrNet
import ctrNet
#Reset the model at each fold
hparams = clf.hparams
clf = ctrNet.build_model(hparams)
#Sample train_x to accelerate model build
valid_idx_sample = np.random.choice(valid_idx,size=int(fm_xsample*len(valid_idx)))
valid_x_sample, valid_y_sample = train_df[feats].iloc[valid_idx_sample], train_df[target_var].iloc[valid_idx_sample]
#train_x = train_x.sample(frac=fm_xsample,random_state=(random_state))
#train_y = train_y.sample(frac=fm_xsample,random_state=(random_state))
#Then train
log("Fold %d; train_x: %s; valid_x_sample: %s" % \
(n_fold,train_x.shape, valid_x_sample.shape))
clf.train(train_data=(train_x,train_y), dev_data=(valid_x_sample,valid_y_sample))
log('train complete')
else:
clf.fit(train_x, train_y)
#Score OOFs and submission
if (model_type == "LGBMClassifier"):
#Requires "best_iteration" argument
oof_preds[valid_idx,bag] = clf.predict_proba(valid_x, num_iteration=clf.best_iteration_)[:, 1]
if submission:
sub_preds[:,bag] += clf.predict_proba(test_df[feats], num_iteration=clf.best_iteration_)[:, 1] / folds.n_splits
elif (model_type=="FM"): #From ctrNet
oof_preds[valid_idx,bag] = clf.infer(dev_data=(valid_x,valid_y))
if submission:
sub_preds[:,bag] += clf.infer(dev_data=(test_df[feats], test_df[target_var])) / folds.n_splits
else: #elif (model_type == "XGBClassifier"):
oof_preds[valid_idx,bag] = clf.predict_proba(valid_x)[:, 1]
if submission:
sub_preds[:,bag] += clf.predict_proba(test_df[feats])[:, 1] / folds.n_splits
if ('best_iteration_' in dir(clf)):
best_iter.append(clf.best_iteration_)
if ('feature_importances_' in dir(clf)):
if (model_type == "CatBoostClassifier"):
fold_importance_df = pd.DataFrame(
list(zip(train_x.dtypes.index,
clf.get_feature_importance(
Pool(train_x, label=train_y, cat_features=cat_cols_idx)
))),
columns=['feature','importance'])
missing_feats = [c for c in feats if c not in
fold_importance_df.feature.values]
missing_df = pd.DataFrame({'feature':missing_feats,'importance':0})
fold_importance_df = pd.concat([fold_importance_df,missing_df])
else:
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = feats
fold_importance_df["importance"] = clf.feature_importances_
fold_importance_df["fold"] = n_fold + 1
fold_importance_df["bag"] = bag
feature_importance_df = pd.concat(
[feature_importance_df, fold_importance_df], axis=0)
model_name = '{}_bag{}_fold{}'.format(model_type,bag,n_fold+1)
try:
filepath = os.path.join(directory, model_name + '.pickle')
with open(filepath, 'wb') as handle:
pickle.dump(clf, handle, protocol=pickle.HIGHEST_PROTOCOL)
log('Saved pickle to: ' + filepath)
filepath = os.path.join(directory, model_name + '_params.txt')
with open(filepath, 'w') as file:
file.write(str(clf.get_params()))
except:
log('Could not save model')
#file.write(json.dumps(clf.get_params())) # use `json.loads` to do the reverse
fold_score = roc_auc_score(valid_y, oof_preds[valid_idx,bag])
log('%s: Fold %d/%d AUC: %2.4f' %
(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
n_fold + 1, folds.n_splits, fold_score * 100)
)
#End fold loop
bag_score = roc_auc_score(train_df[target_var], oof_preds[:,bag])
bag_score_cum = roc_auc_score(train_df[target_var], oof_preds.mean(axis=1))
scores.append([bag, bag_score, bag_score_cum])
log('%s: Bag %d/%d AUC: %2.4f| Cumulative AUC: %2.4f' % \
(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
bag+1, bags, bag_score * 100, bag_score_cum * 100)
)
#End bag loop
#Save remaining results
scores = pd.DataFrame(data=scores,columns=['bag','bag_score','bag_score_cum'])
filepath = os.path.join(directory, 'scores.csv')
scores.to_csv(filepath, index=False)
#Save submission
if submission:
out_df = pd.DataFrame({id_vars : test_df[id_vars],
target_var: sub_preds.mean(axis=1) })
filepath = os.path.join(directory, 'submission.csv')
out_df.to_csv(filepath, index=False)
oof = pd.DataFrame({id_vars: train_df.index,
target_var: oof_preds.mean(axis=1) })
filepath = os.path.join(directory, 'oof.csv')
oof.to_csv(filepath, index=False)
if ('feature_importances_' in dir(clf)):
ft = feature_importance_df.pivot_table( \
index='feature', columns=['fold','bag'], values='importance')
ft['Avg'] = ft.mean(axis=1)
ft.sort_values(by=['Avg'],ascending=False,inplace=True)
ft['RankPct']= ft['Avg'].rank(pct=True)
ft = ft.reset_index()
filepath = 'feature_importance%d.csv' % random.randint(1,1000)
filepath = os.path.join(directory, filepath)
ft.to_csv(filepath, index=False)
#display_importances(feature_importance_df,directory)
else:
ft = pd.DataFrame()
new_dirname = '%s_%s_AUC=%2.2f' % (directory, foldername, bag_score_cum*100)
#Assemble results and return
result = {'folds' : folds,
'features' : ft, #feature_importance_df
'oof_preds' : pd.DataFrame(data=oof_preds,index=train_df.index),
'sub_preds' : pd.DataFrame(data=sub_preds,index=test_df.index),
'directory' : new_dirname}
filepath = os.path.join(directory, 'result.pickle')
with open(filepath, 'wb') as handle:
pickle.dump(result, handle, protocol=pickle.HIGHEST_PROTOCOL)
if (resume_experiment is None) and ('/kaggle/working' not in os.getcwd()):
os.rename(directory, new_dirname)
#Run the model on the full dataset after folds are done:
if fulldata:
best_iter_mean = np.mean(best_iter)
clf.set_params(n_estimators = int(best_iter_mean)+10)
log('Running model on full dataset')
with timer('Running model on full dataset'):
scores = bag_model(df,clf,verbose=200,bags=bags,seed=random_state,
test_split=False)
return result;
def no_cat(df,exclude_cols = ['MachineIdentifier','HasDetections']):
#Factorize categories to 0/1/2... for xgb model
#col_target = [target_var]
#col_id = [id_var]
col_cat = df.select_dtypes(include=['category','bool','object']).columns.values
col_cat = list(set(col_cat) - set(exclude_cols))
if len(col_cat) == 0:
print('No categorical columns found.')
else:
print('Factorizing ' + str(len(col_cat)) + ' columns')
for col in col_cat:
#print(col + ': ' + str(merged[col].nunique()))
df[col], uniques = pd.factorize(df[col])
return df, col_cat;
#df = df2.sample(10000) #.iloc[:,:1000]
def no_miss(df,exclude_cols = ['MachineIdentifier','HasDetections']):
df, cat_col = no_cat(df, exclude_cols = exclude_cols)
feats = list(set(list(df)) - set(exclude_cols))
f_nulls = df[feats].isnull().any()
if any(f_nulls):
f_nulls = list(f_nulls[f_nulls].index)
print('Imputing means for missing data in %d features' % len(f_nulls))
df.loc[:,f_nulls] = df[f_nulls].fillna(df[f_nulls].mean())
m = df[f_nulls].mean().isnull()
if any(m):
print('Dropping %d features with null means' % m[m].shape[0])
df = df.drop(columns=(m[m].index.values))
feats = list(set(list(df)) - set(exclude_cols))
f_inf = np.isinf(df[feats].select_dtypes(['number'])).any()
#x = list(df[feats].select_dtypes(['number']))
#[c for c in feats if c not in x]
if any(f_inf):
f_inf = list(f_inf[f_inf].index)
print('Imputing Inf/-Inf values for %d features' % len(f_inf))
df.loc[:,f_inf] =df[f_inf].astype(np.float32).clip(-1e11,1e11)
assert(all(df[feats].notnull()))
assert(all(np.isfinite(df[feats])))
return df
#----------------------------
# Compare datasets
#----------------------------
def col_comp(df1, df2, sample_n=1000,index_col=[id_var]):
#Find uncorrelated columns from new dataset
#df1 = reduce_mem_usage(df1)
#df2 = reduce_mem_usage(df2)
df1 = no_cat(df1)
df2 = no_cat(df2)
df1.index = df1[index_col]
df2.index = df2[index_col]
samp = np.random.choice(df1.index, sample_n, replace=False)
df1_s = df1.loc[samp]
df2_s = df2.loc[samp]
assert all(df1_s.index == df2_s.index)
#First pass: Correlate with equality
print('pass 1: exact match')
matches = []
for col_df1 in df1.columns:
for col_df2 in df2.columns:
if (df1_s[col_df1].dtype.name == df2_s[col_df2].dtype.name):
if (df1_s[col_df1].dtype == df2_s[col_df2].dtype):
if (df1_s[col_df1].equals(df2_s[col_df2])):
#print('sample match: ' + col_df1 + '=' + col_df2)
if (df1[col_df1].corr(df2[col_df2]) > 0.9999):
print('match 1: ' + col_df1 + '=' + col_df2)
matches.append((col_df1,col_df2,'corr'))
break
matches = pd.DataFrame(data=matches,columns=['df1','df2','match'])
new_cols = list(set(list(df1.columns)) - set(matches.df1))
#Second pass: Correlate without equality
print('\n\npass 2: Fuzzy match')
for col_df1 in df1_s[new_cols].columns:
#print(col_df1)
for col_df2 in df2_s.columns:
if (df1_s[col_df1].corr(df2_s[col_df2]) > 0.9999):
#print('sample correlated: ' + col_df1 + '=' + col_df2)
if (df1[col_df1].corr(df2[col_df2]) > 0.9999):
print('match 2: ' + col_df1 + '=' + col_df2)
matches = matches.append(
{'df1': col_df1, 'df2' : col_df2, 'match' : 'corr2'},
ignore_index=True
)
break
matches['LenDiff'] = abs(matches['df1'].str.len() - matches['df2'].str.len())
new_cols = list(set(list(df1.columns)) - set(matches.df1))
result = {'matches' : matches,
'new_cols' : new_cols}
return matches, new_cols
# add noise to y axis to avoid overlapping
def rand_jitter(arr):
nosie = .01*(max(arr)-min(arr))
return arr + np.random.randn(len(arr))
def draw_feature_distribution(df, column):
column_values = df[df[column].notna()][column]
# group by target
class_0_values = df[df[column].notna() & (df[[target_var]]==0)][column]
class_1_values = df[df[column].notna() & (df[[target_var]]==1)][column]
class_t_values = df[df[column].notna() & (df[[target_var]].isna())][column]
print('\n\n', column)
# for features with unique values >= 10
if len(df[column].value_counts().keys()) >= 10:
fig, ax = plt.subplots(1, figsize=(15, 4))
if df[column].dtype == 'object':
label_encoder = LabelEncoder()
label_encoder.fit(column_values)
class_0_values = label_encoder.transform(class_0_values)
class_1_values = label_encoder.transform(class_1_values)
class_t_values = label_encoder.transform(class_t_values)
column_values = label_encoder.transform(column_values)
plt.xticks(range(len(label_encoder.classes_)), label_encoder.classes_, fontsize=12, rotation='vertical')
ax.scatter(class_0_values, rand_jitter([0]*class_0_values.shape[0]), label='Class0', s=10, marker='o', color='#7ac143', alpha=1)
ax.scatter(class_1_values, rand_jitter([10]*class_1_values.shape[0]), label='Class1', s=10, marker='o', color='#fd5c63', alpha=1)
ax.scatter(class_t_values, rand_jitter([20]*class_t_values.shape[0]), label='Test', s=10, marker='o', color='#037ef3', alpha=0.4)
ax.set_title(column +' group by target', fontsize=16)
ax.legend(bbox_to_anchor=(1.01, 1), loc="upper left")
ax.set_title(column +' distribution', fontsize=16)
else:
all_categories = list(df[df[column].notna()][column].value_counts().keys())
bar_width = 0.25
fig, ax = plt.subplots(figsize=(20, 4))
ax.set_title(column, fontsize=16)
plt.xlabel('Categories', fontsize=16)
plt.ylabel('Counts', fontsize=16)
value_counts = class_0_values.value_counts()
x_0 = np.arange(len(all_categories))
y_0 = [value_counts.get(categroy, 0) for categroy in all_categories]
ax.bar(x_0, y_0, color='#7ac143', width=bar_width, label='class0')
value_counts = class_1_values.value_counts()
x_1 = np.arange(len(all_categories))
y_1 = [value_counts.get(categroy, 0) for categroy in all_categories]
ax.bar(x_1+bar_width, y_1, color='#fd5c63', width=bar_width, label='class1')
value_counts = class_t_values.value_counts()
x_2 = np.arange(len(all_categories))
y_2 = [value_counts.get(categroy, 0) for categroy in all_categories]
ax.bar(x_2+2*bar_width, y_2, color='#037ef3', width=bar_width, label='test')
ax.legend(bbox_to_anchor=(1.01, 1), loc="upper left")
for i, v in enumerate(y_0):
if y_0[i]+y_1[i] == 0:
ax.text(i - .08, max(y_0)//1.25, 'Missing in Train', fontsize=14, rotation='vertical')
else:
ax.text(i - .08, max(y_0)//1.25, "{:0.1f}%".format(100*y_0[i]/(y_0[i]+y_1[i])), fontsize=14, rotation='vertical')
for i, v in enumerate(y_1):
if y_0[i]+y_1[i] == 0:
ax.text(i - .08, max(y_0)//1.25, 'Missing in Train', fontsize=14, rotation='vertical')
else:
ax.text(i + bar_width - .08, max(y_0)//1.25, "{:0.1f}%".format(100*y_1[i]/(y_0[i]+y_1[i])), fontsize=14, rotation='vertical')
for i, v in enumerate(y_2):
if y_2[i] == 0:
ax.text(i + 2*bar_width - .08, max(y_0)//1.25, 'Missing in Test', fontsize=14, rotation='vertical')
else:
ax.text(i + 2*bar_width - .08, max(y_0)//1.25, str(y_2[i]), fontsize=14, rotation='vertical')
plt.xticks(x_0 + 2*bar_width/3, all_categories, fontsize=16)
plt.show()
def find_correlated_cols(df,thresh=0.9999,n_sample=1000):
cols = [c for c in df.columns if c not in [[id_var],[target_var]]]
samp = df[cols].sample(n_sample)
cor = abs(samp.corr())
cor.loc[:,:] = np.tril(cor, k=-1)
cor = cor.stack()
cor = cor[cor > (thresh*0.98)].reset_index()
result = []
for row in range(cor.shape[0]):
col1 = cor.iloc[row]['level_0']
col2 = cor.iloc[row]['level_1']
corr1 = df[col1].corr(df[col2])
result.append(corr1)
cor['corr_full'] = result
cor = cor[cor['corr_full'] > thresh]
cols = list(set.union(set(cor['level_0']),set(cor['level_1'])))
cor = abs(df[cols].corr())
cor.loc[:,:] = np.tril(cor, k=-1)
already_in = set()
clusters = []
for col in cor:
perfect_corr = cor[col][cor[col] > thresh].index.tolist()
if perfect_corr and col not in already_in:
already_in.update(set(perfect_corr))
perfect_corr.append(col)
clusters.append(perfect_corr)
drop_cols = []
for grp in range(len(clusters)):
drop_cols.append(clusters[grp][1:])
flatten = lambda l: [item for sublist in l for item in sublist]
drop_cols = flatten(drop_cols)
return {'drop_cols' : drop_cols,
'clusters' : clusters,
'cor' : cor}
def drop_feats(df,feats, thresh_imp_rank_pct=None, thresh_imp_abs=None,
thresh_corr=None, verbose=False, tgt_var = 'HasDetections'):
#Remove columns with low importance according to previous run or with
# high correlation with other columns in same dataframe
#Check that all cols in df are covered in importance chart
#assert set(df) - set(feats['feature']) == {[id_var], [target_var]}
start_cols = df.shape[1]
# Drop if imporance < 10
drop_cols = pd.DataFrame()
if thresh_imp_rank_pct is not None:
drop_cols = feats[feats['RankPct'] <= thresh_imp_rank_pct]['feature']
thresh_str = "importance rank <= " + str(thresh_imp_rank_pct)
elif thresh_imp_abs is not None:
drop_cols = feats[feats['Avg'] <= thresh_imp_abs]['feature']
thresh_str = "importance <= " + str(thresh_imp_abs)
if drop_cols.shape[0]>0:
df.drop(columns=drop_cols,inplace=True,errors='ignore')
assert set.intersection(set(drop_cols), set(df)) == set() #No unimportant cols
new_cols = df.shape[1]
print('Dropped ' + str(start_cols - new_cols) + ' columns with ' + thresh_str)
if verbose: print(str(drop_cols))
start_cols = new_cols
# Drop if correlation > 0.99; keep higher importance-feature
if thresh_corr is not None:
corr_col = find_correlated_cols(df,thresh=thresh_corr)
drop_cols = []
for cluster in corr_col['clusters']:
#print(cluster)
feat_view = feats[feats['feature'].isin(cluster)]
if feat_view.shape[0]>0:
keep_feat_idx = feat_view['Avg'].argmax()
keep_feat = feats.iloc[keep_feat_idx]['feature']
else:
keep_feat = cluster[0]