-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils_lgb.py
1151 lines (910 loc) · 52.4 KB
/
utils_lgb.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
"""
Utility functions for champs coompetition LGB
1. Training using LGB
2. Hyperopt
"""
import numpy as np
from numpy.linalg import svd, norm
from scipy.stats import hmean
import pandas as pd
import os
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, StratifiedKFold, KFold, GroupKFold
from sklearn.metrics import mean_absolute_error
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from sklearn import metrics
from sklearn import linear_model
import lightgbm as lgb
import time
import datetime
from functools import partial
import matplotlib.pyplot as plt
import seaborn as sns
sns.set();
import gc
from contextlib import contextmanager
def plot_feature_importance(model, features, importance_type='gain', num_features=10):
feature_importance = model.feature_importance(importance_type=importance_type)
feature_importance = pd.DataFrame({'Features': features,
'Importance': feature_importance})\
.sort_values('Importance', ascending = False)
fig = plt.figure(figsize = (5, 10))
fig.suptitle('Feature Importance', fontsize = 20)
plt.tick_params(axis = 'x', labelsize = 12)
plt.tick_params(axis = 'y', labelsize = 12)
plt.xlabel('Importance', fontsize = 15)
plt.ylabel('Features', fontsize = 15)
sns.barplot(x = feature_importance['Importance'][:num_features],
y = feature_importance['Features'][:num_features],
orient = 'h')
plt.show()
def group_mean_log_mae(y_true, y_pred, types, floor=1e-9):
"""
Fast metric computation for this competition: https://www.kaggle.com/c/champs-scalar-coupling
Code is from this kernel: https://www.kaggle.com/uberkinder/efficient-metric
"""
maes = (y_true-y_pred).abs().groupby(types).mean()
return np.log(maes.map(lambda x: max(x, floor))).mean()
def train_model_regression(X, X_test, y,
params, folds,
model_type='lgb',
eval_metric='mae',
columns=None,
plot_feature_importance=False,
model=None,
verbose=10000,
early_stopping_rounds=200,
n_estimators=50000):
"""
A function to train a variety of regression models.
Returns dictionary with oof predictions, test predictions, scores and, if necessary, feature importances.
:params: X - training data, can be pd.DataFrame or np.ndarray (after normalizing)
:params: X_test - test data, can be pd.DataFrame or np.ndarray (after normalizing)
:params: y - target
:params: folds - folds to split data
:params: model_type - type of model to use
:params: eval_metric - metric to use
:params: columns - columns to use. If None - use all columns
:params: plot_feature_importance - whether to plot feature importance of LGB
:params: model - sklearn model, works only for "sklearn" model type
"""
columns = X.columns if columns is None else columns
X_test = X_test[columns]
# to set up scoring parameters
metrics_dict = {'mae': {'lgb_metric_name': 'mae',
'catboost_metric_name': 'MAE',
'sklearn_scoring_function': metrics.mean_absolute_error},
'group_mae': {'lgb_metric_name': 'mae',
'catboost_metric_name': 'MAE',
'scoring_function': group_mean_log_mae},
'mse': {'lgb_metric_name': 'mse',
'catboost_metric_name': 'MSE',
'sklearn_scoring_function': metrics.mean_squared_error}
}
result_dict = {}
# out-of-fold predictions on train data
oof = np.zeros(len(X))
# averaged predictions on train data
prediction = np.zeros(len(X_test))
# list of scores on folds
scores = []
feature_importance = pd.DataFrame()
# split and train on folds
for fold_n, (train_index, valid_index) in enumerate(folds.split(X)):
print(f'\nFold {fold_n + 1} started at {time.ctime()}')
if type(X) == np.ndarray:
X_train, X_valid = X[columns][train_index], X[columns][valid_index]
y_train, y_valid = y[train_index], y[valid_index]
else:
X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if model_type == 'lgb':
model = lgb.LGBMRegressor(**params, n_estimators = n_estimators, n_jobs = -1)
model.fit(X_train, y_train,
eval_set=[(X_train, y_train), (X_valid, y_valid)], eval_metric=metrics_dict[eval_metric]['lgb_metric_name'],
verbose=verbose, early_stopping_rounds=early_stopping_rounds)
y_pred_valid = model.predict(X_valid)
y_pred = model.predict(X_test, num_iteration=model.best_iteration_)
if model_type == 'xgb':
train_data = xgb.DMatrix(data=X_train, label=y_train, feature_names=X.columns)
valid_data = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X.columns)
watchlist = [(train_data, 'train'), (valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=20000, evals=watchlist, early_stopping_rounds=200, verbose_eval=verbose, params=params)
y_pred_valid = model.predict(xgb.DMatrix(X_valid, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
y_pred = model.predict(xgb.DMatrix(X_test, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
if model_type == 'sklearn':
model = model
model.fit(X_train, y_train)
y_pred_valid = model.predict(X_valid).reshape(-1,)
score = metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid)
print(f'Fold {fold_n}. {eval_metric}: {score:.4f}.')
print('')
y_pred = model.predict(X_test).reshape(-1,)
if model_type == 'cat':
model = CatBoostRegressor(iterations=20000, eval_metric=metrics_dict[eval_metric]['catboost_metric_name'], **params,
loss_function=metrics_dict[eval_metric]['catboost_metric_name'])
model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=False)
y_pred_valid = model.predict(X_valid)
y_pred = model.predict(X_test)
oof[valid_index] = y_pred_valid.reshape(-1,)
if eval_metric != 'group_mae':
scores.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid))
else:
scores.append(metrics_dict[eval_metric]['scoring_function'](y_valid, y_pred_valid, X_valid['type']))
prediction += y_pred
if model_type == 'lgb' and plot_feature_importance:
# feature importance
fold_importance = pd.DataFrame()
fold_importance["feature"] = columns
fold_importance["importance"] = model.feature_importances_
fold_importance["fold"] = fold_n + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
prediction /= folds.n_splits
print('CV mean score: {0:.6f}, std: {1:.6f}.\n'.format(np.mean(scores), np.std(scores)))
result_dict['oof'] = oof
result_dict['prediction'] = prediction
result_dict['scores'] = scores
if model_type == 'lgb':
if plot_feature_importance:
feature_importance["importance"] /= folds.n_splits
cols = feature_importance[["feature", "importance"]].groupby("feature").mean().sort_values(
by="importance", ascending=False)[:50].index
best_features = feature_importance.loc[feature_importance.feature.isin(cols)]
plt.figure(figsize=(16, 12));
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False));
plt.title('LGB Features (avg over folds)');
result_dict['feature_importance'] = feature_importance
return result_dict
def train_lgb_regression_group(X, X_test, y, params, folds, groups,
eval_metric='mae',
columns=None, plot_feature_importance=False, model=None,
verbose=10000, early_stopping_rounds=200, n_estimators=50000):
"""
A function to train a variety of regression models.
Returns dictionary with oof predictions, test predictions, scores and, if necessary, feature importances.
:params: X - training data, can be pd.DataFrame or np.ndarray (after normalizing)
:params: X_test - test data, can be pd.DataFrame or np.ndarray (after normalizing)
:params: y - target
:params: folds - Group Kfolds to split data
:params: model_type - type of model to use
:params: eval_metric - metric to use
:params: columns - columns to use. If None - use all columns
:params: plot_feature_importance - whether to plot feature importance of LGB
:params: model - sklearn model, works only for "sklearn" model type
"""
columns = X.columns if columns is None else columns
X_test = X_test[columns]
# to set up scoring parameters
metrics_dict = {'mae': {'lgb_metric_name': 'mae',
'sklearn_scoring_function': metrics.mean_absolute_error},
'group_mae': {'lgb_metric_name': 'mae',
'scoring_function': group_mean_log_mae},
'mse': {'lgb_metric_name': 'mse',
'sklearn_scoring_function': metrics.mean_squared_error}
}
result_dict = {}
# out-of-fold predictions on train data
oof = np.zeros(len(X))
# averaged predictions on train data
prediction = np.zeros(len(X_test))
# list of scores on folds
scores = []
feature_importance = pd.DataFrame()
if groups is not None:
folds_splits = folds.split(X,groups=groups)
else:
folds_splits = folds.split(X)
# split and train on folds
for fold_n, (train_index, valid_index) in enumerate(folds_splits):
print(f'\nFold {fold_n + 1} started at {time.ctime()}')
if type(X) == np.ndarray:
X_train, X_valid = X[columns][train_index], X[columns][valid_index]
y_train, y_valid = y[train_index], y[valid_index]
else:
X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
model = lgb.LGBMRegressor(**params, n_estimators = n_estimators, n_jobs = -1)
model.fit(X_train, y_train,
eval_set=[(X_train, y_train), (X_valid, y_valid)],
eval_metric=metrics_dict[eval_metric]['lgb_metric_name'],
verbose=verbose, early_stopping_rounds=early_stopping_rounds)
y_pred_valid = model.predict(X_valid)
y_pred = model.predict(X_test, num_iteration=model.best_iteration_)
oof[valid_index] = y_pred_valid.reshape(-1,)
if eval_metric != 'group_mae':
scores.append(metrics_dict[eval_metric]['sklearn_scoring_function'](y_valid, y_pred_valid))
else:
scores.append(metrics_dict[eval_metric]['scoring_function'](y_valid, y_pred_valid, X_valid['type']))
prediction += y_pred
if plot_feature_importance:
# feature importance
fold_importance = pd.DataFrame()
fold_importance["feature"] = columns
fold_importance["importance"] = model.feature_importances_
fold_importance["fold"] = fold_n + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
prediction /= folds.n_splits
print('CV mean score: {0:.6f}, std: {1:.6f}.\n'.format(np.mean(scores), np.std(scores)))
result_dict['oof'] = oof
result_dict['prediction'] = prediction
result_dict['scores'] = scores
if plot_feature_importance:
feature_importance["importance"] /= folds.n_splits
cols = feature_importance[["feature", "importance"]].groupby("feature").mean().sort_values(
by="importance", ascending=False)[:50].index
best_features = feature_importance.loc[feature_importance.feature.isin(cols)]
plt.figure(figsize=(16, 12));
sns.barplot(x="importance", y="feature",
data=best_features.sort_values(by="importance", ascending=False));
plt.title('LGB Features (avg over folds)');
result_dict['feature_importance'] = feature_importance
return result_dict
#############################
from hyperopt import hp, tpe, Trials, space_eval, STATUS_OK, STATUS_RUNNING
from hyperopt.fmin import fmin
from hyperopt.pyll.stochastic import sample
#optional but advised
#GLOBAL HYPEROPT PARAMETERS
NUM_EVALS = 1000 #number of hyperopt evaluation rounds
N_FOLDS = 5 #number of cross-validation folds on data in each evaluation round
#LIGHTGBM PARAMETERS
LGBM_MAX_LEAVES = 2**11 #maximum number of leaves per tree for LightGBM
LGBM_MAX_DEPTH = 25 #maximum tree depth for LightGBM
EVAL_METRIC_LGBM_REG = 'mae' #LightGBM regression metric. Note that 'rmse' is more commonly used
EVAL_METRIC_LGBM_CLASS = 'auc' #LightGBM classification metric
#XGBOOST PARAMETERS
XGB_MAX_LEAVES = 2**12 #maximum number of leaves when using histogram splitting
XGB_MAX_DEPTH = 25 #maximum tree depth for XGBoost
EVAL_METRIC_XGB_REG = 'mae' #XGBoost regression metric
EVAL_METRIC_XGB_CLASS = 'auc' #XGBoost classification metric
#CATBOOST PARAMETERS
CB_MAX_DEPTH = 8 #maximum tree depth in CatBoost
OBJECTIVE_CB_REG = 'MAE' #CatBoost regression metric
OBJECTIVE_CB_CLASS = 'Logloss' #CatBoost classification metric
#OPTIONAL OUTPUT
BEST_SCORE = 0
def quick_hyperopt(data, labels, package='lgbm',
num_evals=NUM_EVALS,
diagnostic=False, Class=False):
#==========
#LightGBM
#==========
if package=='lgbm':
print('Running {} rounds of LightGBM parameter optimisation:'.format(num_evals))
#clear space
gc.collect()
integer_params = ['max_depth',
'num_leaves',
'max_bin',
'min_data_in_leaf',
'min_data_in_bin']
def objective(space_params):
#cast integer params from float to int
for param in integer_params:
space_params[param] = int(space_params[param])
#extract nested conditional parameters
if space_params['boosting']['boosting'] == 'goss':
top_rate = space_params['boosting'].get('top_rate')
other_rate = space_params['boosting'].get('other_rate')
#0 <= top_rate + other_rate <= 1
top_rate = max(top_rate, 0)
top_rate = min(top_rate, 0.5)
other_rate = max(other_rate, 0)
other_rate = min(other_rate, 0.5)
space_params['top_rate'] = top_rate
space_params['other_rate'] = other_rate
subsample = space_params['boosting'].get('subsample', 1.0)
space_params['boosting'] = space_params['boosting']['boosting']
space_params['subsample'] = subsample
if Class:
cv_results = lgb.cv(space_params, train, nfold = N_FOLDS, stratified=True,
early_stopping_rounds=100, metrics=EVAL_METRIC_LGBM_CLASS, seed=42)
best_loss = 1 - cv_results['auc-mean'][-1]
else:
cv_results = lgb.cv(space_params, train, nfold = N_FOLDS, stratified=False,
early_stopping_rounds=100, metrics=EVAL_METRIC_LGBM_REG, seed=42)
best_loss = cv_results['l1-mean'][-1] #'l2-mean' for rmse
return{'loss':best_loss, 'status': STATUS_OK }
train = lgb.Dataset(data, labels)
#integer and string parameters, used with hp.choice()
boosting_list = [{'boosting': 'gbdt',
'subsample': hp.uniform('subsample', 0.5, 1)},
{'boosting': 'goss',
'subsample': 1.0,
'top_rate': hp.uniform('top_rate', 0, 0.5),
'other_rate': hp.uniform('other_rate', 0, 0.5)}] #if including 'dart', make sure to set 'n_estimators'
if Class:
metric_list = ['auc'] #modify as required for other classification metrics
objective_list = ['binary', 'cross_entropy']
else:
# metric_list = ['MAE', 'RMSE']
metric_list = ['MAE']
# objective_list = ['huber', 'gamma', 'fair', 'tweedie']
objective_list = ['huber', 'fair', 'regression']
space ={'boosting' : hp.choice('boosting', boosting_list),
'num_leaves' : hp.quniform('num_leaves', 2, LGBM_MAX_LEAVES, 1),
'max_depth': hp.quniform('max_depth', 2, LGBM_MAX_DEPTH, 1),
'max_bin': hp.quniform('max_bin', 32, 255, 1),
'min_data_in_leaf': hp.quniform('min_data_in_leaf', 10, 256, 1),
'min_data_in_bin': hp.quniform('min_data_in_bin', 10, 256, 1),
'min_gain_to_split' : hp.quniform('min_gain_to_split', 0.1, 5, 0.1),
'lambda_l1' : hp.uniform('lambda_l1', 0, 5),
'lambda_l2' : hp.uniform('lambda_l2', 0, 5),
'learning_rate' : hp.loguniform('learning_rate', np.log(0.005), np.log(0.2)),
'metric' : hp.choice('metric', metric_list),
'objective' : hp.choice('objective', objective_list),
'feature_fraction' : hp.quniform('feature_fraction', 0.5, 1, 0.02),
'bagging_fraction' : hp.quniform('bagging_fraction', 0.5, 1, 0.02),
# 'tweedie_variance_power' : hp.quniform('tweedie_variance_power', 1, 1.95, 0.05),
}
#optional: activate GPU for LightGBM
#follow compilation steps here:
#https://www.kaggle.com/vinhnguyen/gpu-acceleration-for-lightgbm/
#then uncomment lines below:
#space['device'] = 'gpu'
#space['gpu_platform_id'] = 0,
#space['gpu_device_id'] = 0
trials = Trials()
best = fmin(fn=objective,
space=space,
algo=tpe.suggest,
max_evals=num_evals,
trials=trials)
#fmin() will return the index of values chosen from the lists/arrays in 'space'
#to obtain actual values, index values are used to subset the original lists/arrays
best['boosting'] = boosting_list[best['boosting']]['boosting']#nested dict, index twice
best['metric'] = metric_list[best['metric']]
best['objective'] = objective_list[best['objective']]
#cast floats of integer params to int
for param in integer_params:
best[param] = int(best[param])
print('{' + '\n'.join('{}: {}'.format(k, v) for k, v in best.items()) + '}')
if diagnostic:
return(best, trials)
else:
return(best)
#==========
#XGBoost
#=========
if package=='xgb':
print('Running {} rounds of XGBoost parameter optimisation:'.format(num_evals))
#clear space
gc.collect()
integer_params = ['max_depth']
def objective(space_params):
for param in integer_params:
space_params[param] = int(space_params[param])
#extract multiple nested tree_method conditional parameters
#libera te tutemet ex inferis
if space_params['tree_method']['tree_method'] == 'hist':
max_bin = space_params['tree_method'].get('max_bin')
space_params['max_bin'] = int(max_bin)
if space_params['tree_method']['grow_policy']['grow_policy']['grow_policy'] == 'depthwise':
grow_policy = space_params['tree_method'].get('grow_policy').get('grow_policy').get('grow_policy')
space_params['grow_policy'] = grow_policy
space_params['tree_method'] = 'hist'
else:
max_leaves = space_params['tree_method']['grow_policy']['grow_policy'].get('max_leaves')
space_params['grow_policy'] = 'lossguide'
space_params['max_leaves'] = int(max_leaves)
space_params['tree_method'] = 'hist'
else:
space_params['tree_method'] = space_params['tree_method'].get('tree_method')
#for classification replace EVAL_METRIC_XGB_REG with EVAL_METRIC_XGB_CLASS
cv_results = xgb.cv(space_params, train, nfold=N_FOLDS, metrics=[EVAL_METRIC_XGB_REG],
early_stopping_rounds=100, stratified=False, seed=42)
best_loss = cv_results['test-mae-mean'].iloc[-1] #or 'test-rmse-mean' if using RMSE
#for classification, comment out the line above and uncomment the line below:
#best_loss = 1 - cv_results['test-auc-mean'].iloc[-1]
#if necessary, replace 'test-auc-mean' with 'test-[your-preferred-metric]-mean'
return{'loss':best_loss, 'status': STATUS_OK }
train = xgb.DMatrix(data, labels)
#integer and string parameters, used with hp.choice()
boosting_list = ['gbtree', 'gblinear'] #if including 'dart', make sure to set 'n_estimators'
metric_list = ['MAE', 'RMSE']
#for classification comment out the line above and uncomment the line below
#metric_list = ['auc']
#modify as required for other classification metrics classification
tree_method = [{'tree_method' : 'exact'},
{'tree_method' : 'approx'},
{'tree_method' : 'hist',
'max_bin': hp.quniform('max_bin', 2**3, 2**7, 1),
'grow_policy' : {'grow_policy': {'grow_policy':'depthwise'},
'grow_policy' : {'grow_policy':'lossguide',
'max_leaves': hp.quniform('max_leaves', 32, XGB_MAX_LEAVES, 1)}}}]
#if using GPU, replace 'exact' with 'gpu_exact' and 'hist' with
#'gpu_hist' in the nested dictionary above
objective_list_reg = ['reg:linear', 'reg:gamma', 'reg:tweedie']
objective_list_class = ['reg:logistic', 'binary:logistic']
#for classification change line below to 'objective_list = objective_list_class'
objective_list = objective_list_reg
space ={'boosting' : hp.choice('boosting', boosting_list),
'tree_method' : hp.choice('tree_method', tree_method),
'max_depth': hp.quniform('max_depth', 2, XGB_MAX_DEPTH, 1),
'reg_alpha' : hp.uniform('reg_alpha', 0, 5),
'reg_lambda' : hp.uniform('reg_lambda', 0, 5),
'min_child_weight' : hp.uniform('min_child_weight', 0, 5),
'gamma' : hp.uniform('gamma', 0, 5),
'learning_rate' : hp.loguniform('learning_rate', np.log(0.005), np.log(0.2)),
'eval_metric' : hp.choice('eval_metric', metric_list),
'objective' : hp.choice('objective', objective_list),
'colsample_bytree' : hp.quniform('colsample_bytree', 0.1, 1, 0.01),
'colsample_bynode' : hp.quniform('colsample_bynode', 0.1, 1, 0.01),
'colsample_bylevel' : hp.quniform('colsample_bylevel', 0.1, 1, 0.01),
'subsample' : hp.quniform('subsample', 0.5, 1, 0.05),
'nthread' : -1
}
trials = Trials()
best = fmin(fn=objective,
space=space,
algo=tpe.suggest,
max_evals=num_evals,
trials=trials)
best['tree_method'] = tree_method[best['tree_method']]['tree_method']
best['boosting'] = boosting_list[best['boosting']]
best['eval_metric'] = metric_list[best['eval_metric']]
best['objective'] = objective_list[best['objective']]
#cast floats of integer params to int
for param in integer_params:
best[param] = int(best[param])
if 'max_leaves' in best:
best['max_leaves'] = int(best['max_leaves'])
if 'max_bin' in best:
best['max_bin'] = int(best['max_bin'])
print('{' + '\n'.join('{}: {}'.format(k, v) for k, v in best.items()) + '}')
if diagnostic:
return(best, trials)
else:
return(best)
#==========
#CatBoost
#==========
if package=='cb':
print('Running {} rounds of CatBoost parameter optimisation:'.format(num_evals))
#clear memory
gc.collect()
integer_params = ['depth',
#'one_hot_max_size', #for categorical data
'min_data_in_leaf',
'max_bin']
def objective(space_params):
#cast integer params from float to int
for param in integer_params:
space_params[param] = int(space_params[param])
#extract nested conditional parameters
if space_params['bootstrap_type']['bootstrap_type'] == 'Bayesian':
bagging_temp = space_params['bootstrap_type'].get('bagging_temperature')
space_params['bagging_temperature'] = bagging_temp
if space_params['grow_policy']['grow_policy'] == 'LossGuide':
max_leaves = space_params['grow_policy'].get('max_leaves')
space_params['max_leaves'] = int(max_leaves)
space_params['bootstrap_type'] = space_params['bootstrap_type']['bootstrap_type']
space_params['grow_policy'] = space_params['grow_policy']['grow_policy']
#random_strength cannot be < 0
space_params['random_strength'] = max(space_params['random_strength'], 0)
#fold_len_multiplier cannot be < 1
space_params['fold_len_multiplier'] = max(space_params['fold_len_multiplier'], 1)
#for classification set stratified=True
cv_results = cb.cv(train, space_params, fold_count=N_FOLDS,
early_stopping_rounds=25, stratified=False, partition_random_seed=42)
best_loss = cv_results['test-MAE-mean'].iloc[-1] #'test-RMSE-mean' for RMSE
#for classification, comment out the line above and uncomment the line below:
#best_loss = cv_results['test-Logloss-mean'].iloc[-1]
#if necessary, replace 'test-Logloss-mean' with 'test-[your-preferred-metric]-mean'
return{'loss':best_loss, 'status': STATUS_OK}
train = cb.Pool(data, labels.astype('float32'))
#integer and string parameters, used with hp.choice()
bootstrap_type = [{'bootstrap_type':'Poisson'},
{'bootstrap_type':'Bayesian',
'bagging_temperature' : hp.loguniform('bagging_temperature', np.log(1), np.log(50))},
{'bootstrap_type':'Bernoulli'}]
LEB = ['No', 'AnyImprovement', 'Armijo'] #remove 'Armijo' if not using GPU
#score_function = ['Correlation', 'L2', 'NewtonCorrelation', 'NewtonL2']
grow_policy = [{'grow_policy':'SymmetricTree'},
{'grow_policy':'Depthwise'},
{'grow_policy':'Lossguide',
'max_leaves': hp.quniform('max_leaves', 2, 32, 1)}]
eval_metric_list_reg = ['MAE', 'RMSE', 'Poisson']
eval_metric_list_class = ['Logloss', 'AUC', 'F1']
#for classification change line below to 'eval_metric_list = eval_metric_list_class'
eval_metric_list = eval_metric_list_reg
space ={'depth': hp.quniform('depth', 2, CB_MAX_DEPTH, 1),
'max_bin' : hp.quniform('max_bin', 1, 32, 1), #if using CPU just set this to 254
'l2_leaf_reg' : hp.uniform('l2_leaf_reg', 0, 5),
'min_data_in_leaf' : hp.quniform('min_data_in_leaf', 1, 50, 1),
'random_strength' : hp.loguniform('random_strength', np.log(0.005), np.log(5)),
#'one_hot_max_size' : hp.quniform('one_hot_max_size', 2, 16, 1), #uncomment if using categorical features
'bootstrap_type' : hp.choice('bootstrap_type', bootstrap_type),
'learning_rate' : hp.uniform('learning_rate', 0.05, 0.25),
'eval_metric' : hp.choice('eval_metric', eval_metric_list),
'objective' : OBJECTIVE_CB_REG,
#'score_function' : hp.choice('score_function', score_function), #crashes kernel - reason unknown
'leaf_estimation_backtracking' : hp.choice('leaf_estimation_backtracking', LEB),
'grow_policy': hp.choice('grow_policy', grow_policy),
#'colsample_bylevel' : hp.quniform('colsample_bylevel', 0.1, 1, 0.01),# CPU only
'fold_len_multiplier' : hp.loguniform('fold_len_multiplier', np.log(1.01), np.log(2.5)),
'od_type' : 'Iter',
'od_wait' : 25,
'task_type' : 'GPU',
'verbose' : 0
}
#optional: run CatBoost without GPU
#uncomment line below
#space['task_type'] = 'CPU'
trials = Trials()
best = fmin(fn=objective,
space=space,
algo=tpe.suggest,
max_evals=num_evals,
trials=trials)
#unpack nested dicts first
best['bootstrap_type'] = bootstrap_type[best['bootstrap_type']]['bootstrap_type']
best['grow_policy'] = grow_policy[best['grow_policy']]['grow_policy']
best['eval_metric'] = eval_metric_list[best['eval_metric']]
#best['score_function'] = score_function[best['score_function']]
#best['leaf_estimation_method'] = LEM[best['leaf_estimation_method']] #CPU only
best['leaf_estimation_backtracking'] = LEB[best['leaf_estimation_backtracking']]
#cast floats of integer params to int
for param in integer_params:
best[param] = int(best[param])
if 'max_leaves' in best:
best['max_leaves'] = int(best['max_leaves'])
print('{' + '\n'.join('{}: {}'.format(k, v) for k, v in best.items()) + '}')
if diagnostic:
return(best, trials)
else:
return(best)
else:
print('Package not recognised. Please use "lgbm" for LightGBM, "xgb" for XGBoost or "cb" for CatBoost.')
################### Simple feature generation ###################
def map_atom_info(df_1, df_2, atom_idx):
df = pd.merge(df_1, df_2, how = 'left',
left_on = ['molecule_name', f'atom_index_{atom_idx}'],
right_on = ['molecule_name', 'atom_index'])
df = df.drop('atom_index', axis=1)
return df
def find_dist(df):
df_p_0 = df[['x_0', 'y_0', 'z_0']].values
df_p_1 = df[['x_1', 'y_1', 'z_1']].values
df['dist'] = np.linalg.norm(df_p_0 - df_p_1, axis=1)
df['dist_inv'] = 1/df['dist']
df['dist_inv2'] = 1/df['dist']**2
df['dist_inv3'] = 1/df['dist']**3
df['dist_x'] = (df['x_0'] - df['x_1']) ** 2
df['dist_y'] = (df['y_0'] - df['y_1']) ** 2
df['dist_z'] = (df['z_0'] - df['z_1']) ** 2
df['type_0'] = df['type'].apply(lambda x: x[0])
return df
def find_closest_atom(df):
'''
Find the closest and farthest atoms in a molecule to the two atoms of interest
'''
df_temp = df.loc[:,["molecule_name",
"atom_index_0","atom_index_1",
"dist","x_0","y_0","z_0","x_1","y_1","z_1"]].copy()
df_temp_ = df_temp.copy()
df_temp_ = df_temp_.rename(columns={'atom_index_0': 'atom_index_1',
'atom_index_1': 'atom_index_0',
'x_0': 'x_1',
'y_0': 'y_1',
'z_0': 'z_1',
'x_1': 'x_0',
'y_1': 'y_0',
'z_1': 'z_0'})
df_temp_all = pd.concat((df_temp,df_temp_),axis=0)
df_temp_all["min_distance"]=df_temp_all.groupby(['molecule_name',
'atom_index_0'])['dist'].transform('min')
df_temp_all["max_distance"]=df_temp_all.groupby(['molecule_name',
'atom_index_0'])['dist'].transform('max')
df_temp = df_temp_all[df_temp_all["min_distance"]==df_temp_all["dist"]].copy()
df_temp = df_temp.drop(['x_0','y_0','z_0','min_distance'], axis=1)
df_temp = df_temp.rename(columns={'atom_index_0': 'atom_index',
'atom_index_1': 'atom_index_closest',
'dist': 'distance_closest',
'x_1': 'x_closest',
'y_1': 'y_closest',
'z_1': 'z_closest'})
df_temp = df_temp.drop_duplicates(subset=['molecule_name', 'atom_index'])
for atom_idx in [0,1]:
df = map_atom_info(df,df_temp, atom_idx)
df = df.rename(columns={'atom_index_closest': f'atom_index_closest_{atom_idx}',
'distance_closest': f'distance_closest_{atom_idx}',
'x_closest': f'x_closest_{atom_idx}',
'y_closest': f'y_closest_{atom_idx}',
'z_closest': f'z_closest_{atom_idx}'})
df_temp= df_temp_all[df_temp_all["max_distance"]==df_temp_all["dist"]].copy()
df_temp = df_temp.drop(['x_0','y_0','z_0','max_distance'], axis=1)
df_temp= df_temp.rename(columns={'atom_index_0': 'atom_index',
'atom_index_1': 'atom_index_farthest',
'dist': 'distance_farthest',
'x_1': 'x_farthest',
'y_1': 'y_farthest',
'z_1': 'z_farthest'})
df_temp = df_temp.drop_duplicates(subset=['molecule_name', 'atom_index'])
for atom_idx in [0,1]:
df = map_atom_info(df,df_temp, atom_idx)
df = df.rename(columns={'atom_index_farthest': f'atom_index_farthest_{atom_idx}',
'distance_farthest': f'distance_farthest_{atom_idx}',
'x_farthest': f'x_farthest_{atom_idx}',
'y_farthest': f'y_farthest_{atom_idx}',
'z_farthest': f'z_farthest_{atom_idx}'})
return df
def add_cos_features(df):
df["distance_center0"] = np.sqrt((df['x_0']-df['c_x'])**2 \
+ (df['y_0']-df['c_y'])**2 \
+ (df['z_0']-df['c_z'])**2)
df["distance_center1"] = np.sqrt((df['x_1']-df['c_x'])**2 \
+ (df['y_1']-df['c_y'])**2 \
+ (df['z_1']-df['c_z'])**2)
df['distance_c0'] = np.sqrt((df['x_0']-df['x_closest_0'])**2 + \
(df['y_0']-df['y_closest_0'])**2 + \
(df['z_0']-df['z_closest_0'])**2)
df['distance_c1'] = np.sqrt((df['x_1']-df['x_closest_1'])**2 + \
(df['y_1']-df['y_closest_1'])**2 + \
(df['z_1']-df['z_closest_1'])**2)
df["distance_f0"] = np.sqrt((df['x_0']-df['x_farthest_0'])**2 + \
(df['y_0']-df['y_farthest_0'])**2 + \
(df['z_0']-df['z_farthest_0'])**2)
df["distance_f1"] = np.sqrt((df['x_1']-df['x_farthest_1'])**2 + \
(df['y_1']-df['y_farthest_1'])**2 + \
(df['z_1']-df['z_farthest_1'])**2)
vec_center0_x = (df['x_0']-df['c_x'])/(df["distance_center0"]+1e-10)
vec_center0_y = (df['y_0']-df['c_y'])/(df["distance_center0"]+1e-10)
vec_center0_z = (df['z_0']-df['c_z'])/(df["distance_center0"]+1e-10)
vec_center1_x = (df['x_1']-df['c_x'])/(df["distance_center1"]+1e-10)
vec_center1_y = (df['y_1']-df['c_y'])/(df["distance_center1"]+1e-10)
vec_center1_z = (df['z_1']-df['c_z'])/(df["distance_center1"]+1e-10)
vec_c0_x = (df['x_0']-df['x_closest_0'])/(df["distance_c0"])
vec_c0_y = (df['y_0']-df['y_closest_0'])/(df["distance_c0"])
vec_c0_z = (df['z_0']-df['z_closest_0'])/(df["distance_c0"])
vec_c1_x = (df['x_1']-df['x_closest_1'])/(df["distance_c1"])
vec_c1_y = (df['y_1']-df['y_closest_1'])/(df["distance_c1"])
vec_c1_z = (df['z_1']-df['z_closest_1'])/(df["distance_c1"])
vec_f0_x = (df['x_0']-df['x_farthest_0'])/(df["distance_f0"])
vec_f0_y = (df['y_0']-df['y_farthest_0'])/(df["distance_f0"])
vec_f0_z = (df['z_0']-df['z_farthest_0'])/(df["distance_f0"])
vec_f1_x = (df['x_1']-df['x_farthest_1'])/(df["distance_f1"])
vec_f1_y = (df['y_1']-df['y_farthest_1'])/(df["distance_f1"])
vec_f1_z = (df['z_1']-df['z_farthest_1'])/(df["distance_f1"])
vec_x = (df['x_1']-df['x_0'])/df['dist']
vec_y = (df['y_1']-df['y_0'])/df['dist']
vec_z = (df['z_1']-df['z_0'])/df['dist']
df["cos_c0_c1"] = vec_c0_x*vec_c1_x + vec_c0_y*vec_c1_y + vec_c0_z*vec_c1_z
df["cos_f0_f1"] = vec_f0_x*vec_f1_x + vec_f0_y*vec_f1_y + vec_f0_z*vec_f1_z
df["cos_c0_f0"] = vec_c0_x*vec_f0_x + vec_c0_y*vec_f0_y + vec_c0_z*vec_f0_z
df["cos_c1_f1"] = vec_c1_x*vec_f1_x + vec_c1_y*vec_f1_y + vec_c1_z*vec_f1_z
df["cos_center0_center1"] = vec_center0_x*vec_center1_x \
+ vec_center0_y*vec_center1_y \
+ vec_center0_z*vec_center1_z
df["cos_c0"] = vec_c0_x*vec_x + vec_c0_y*vec_y + vec_c0_z*vec_z
df["cos_c1"] = vec_c1_x*vec_x + vec_c1_y*vec_y + vec_c1_z*vec_z
df["cos_f0"] = vec_f0_x*vec_x + vec_f0_y*vec_y + vec_f0_z*vec_z
df["cos_f1"] = vec_f1_x*vec_x + vec_f1_y*vec_y + vec_f1_z*vec_z
df["cos_center0"] = vec_center0_x*vec_x + vec_center0_y*vec_y + vec_center0_z*vec_z
df["cos_center1"] = vec_center1_x*vec_x + vec_center1_y*vec_y + vec_center1_z*vec_z
return df
def add_dist_features(df):
# Andrew's features
df['molecule_couples'] = df.groupby('molecule_name')['id'].transform('count')
df['molecule_dist_mean'] = df.groupby('molecule_name')['dist'].transform('mean')
df['molecule_dist_min'] = df.groupby('molecule_name')['dist'].transform('min')
df['molecule_dist_max'] = df.groupby('molecule_name')['dist'].transform('max')
df['atom_0_couples_count'] = df.groupby(['molecule_name', 'atom_index_0'])['id'].transform('count')
df['atom_1_couples_count'] = df.groupby(['molecule_name', 'atom_index_1'])['id'].transform('count')
df[f'molecule_atom_index_0_x_1_std'] = df.groupby(['molecule_name', 'atom_index_0'])['x_1'].transform('std')
df[f'molecule_atom_index_0_y_1_mean'] = df.groupby(['molecule_name', 'atom_index_0'])['y_1'].transform('mean')
df[f'molecule_atom_index_0_y_1_mean_diff'] = df[f'molecule_atom_index_0_y_1_mean'] - df['y_1']
df[f'molecule_atom_index_0_y_1_mean_div'] = df[f'molecule_atom_index_0_y_1_mean'] / df['y_1']
df[f'molecule_atom_index_0_y_1_max'] = df.groupby(['molecule_name', 'atom_index_0'])['y_1'].transform('max')
df[f'molecule_atom_index_0_y_1_max_diff'] = df[f'molecule_atom_index_0_y_1_max'] - df['y_1']
df[f'molecule_atom_index_0_y_1_std'] = df.groupby(['molecule_name', 'atom_index_0'])['y_1'].transform('std')
df[f'molecule_atom_index_0_z_1_std'] = df.groupby(['molecule_name', 'atom_index_0'])['z_1'].transform('std')
# some of these are redundant by symmetry
df[f'molecule_atom_index_1_x_0_std'] = df.groupby(['molecule_name', 'atom_index_1'])['x_0'].transform('std')
df[f'molecule_atom_index_1_y_0_mean'] = df.groupby(['molecule_name', 'atom_index_1'])['y_0'].transform('mean')
df[f'molecule_atom_index_1_y_0_mean_diff'] = df[f'molecule_atom_index_1_y_0_mean'] - df['y_0']
df[f'molecule_atom_index_1_y_0_mean_div'] = df[f'molecule_atom_index_1_y_0_mean'] / df['y_0']
df[f'molecule_atom_index_1_y_0_max'] = df.groupby(['molecule_name', 'atom_index_1'])['y_0'].transform('max')
df[f'molecule_atom_index_1_y_0_max_diff'] = df[f'molecule_atom_index_1_y_0_max'] - df['y_0']
df[f'molecule_atom_index_1_y_0_std'] = df.groupby(['molecule_name', 'atom_index_1'])['y_0'].transform('std')
df[f'molecule_atom_index_1_z_0_std'] = df.groupby(['molecule_name', 'atom_index_1'])['z_0'].transform('std')
df[f'molecule_atom_index_0_dist_mean'] = df.groupby(['molecule_name', 'atom_index_0'])['dist'].transform('mean')
df[f'molecule_atom_index_0_dist_mean_diff'] = df[f'molecule_atom_index_0_dist_mean'] - df['dist']
df[f'molecule_atom_index_0_dist_mean_div'] = df[f'molecule_atom_index_0_dist_mean'] / df['dist']
df[f'molecule_atom_index_0_dist_max'] = df.groupby(['molecule_name', 'atom_index_0'])['dist'].transform('max')
df[f'molecule_atom_index_0_dist_max_diff'] = df[f'molecule_atom_index_0_dist_max'] - df['dist']
df[f'molecule_atom_index_0_dist_max_div'] = df[f'molecule_atom_index_0_dist_max'] / df['dist']
df[f'molecule_atom_index_0_dist_min'] = df.groupby(['molecule_name', 'atom_index_0'])['dist'].transform('min')
df[f'molecule_atom_index_0_dist_min_diff'] = df[f'molecule_atom_index_0_dist_min'] - df['dist']
df[f'molecule_atom_index_0_dist_min_div'] = df[f'molecule_atom_index_0_dist_min'] / df['dist']
df[f'molecule_atom_index_0_dist_std'] = df.groupby(['molecule_name', 'atom_index_0'])['dist'].transform('std')
df[f'molecule_atom_index_0_dist_std_diff'] = df[f'molecule_atom_index_0_dist_std'] - df['dist']
df[f'molecule_atom_index_0_dist_std_div'] = df[f'molecule_atom_index_0_dist_std'] / df['dist']
df[f'molecule_atom_index_1_dist_mean'] = df.groupby(['molecule_name', 'atom_index_1'])['dist'].transform('mean')
df[f'molecule_atom_index_1_dist_mean_diff'] = df[f'molecule_atom_index_1_dist_mean'] - df['dist']
df[f'molecule_atom_index_1_dist_mean_div'] = df[f'molecule_atom_index_1_dist_mean'] / df['dist']
df[f'molecule_atom_index_1_dist_max'] = df.groupby(['molecule_name', 'atom_index_1'])['dist'].transform('max')
df[f'molecule_atom_index_1_dist_max_diff'] = df[f'molecule_atom_index_1_dist_max'] - df['dist']
df[f'molecule_atom_index_1_dist_max_div'] = df[f'molecule_atom_index_1_dist_max'] / df['dist']
df[f'molecule_atom_index_1_dist_min'] = df.groupby(['molecule_name', 'atom_index_1'])['dist'].transform('min')
df[f'molecule_atom_index_1_dist_min_diff'] = df[f'molecule_atom_index_1_dist_min'] - df['dist']
df[f'molecule_atom_index_1_dist_min_div'] = df[f'molecule_atom_index_1_dist_min'] / df['dist']
df[f'molecule_atom_index_1_dist_std'] = df.groupby(['molecule_name', 'atom_index_1'])['dist'].transform('std')
df[f'molecule_atom_index_1_dist_std_diff'] = df[f'molecule_atom_index_1_dist_std'] - df['dist']
df[f'molecule_atom_index_1_dist_std_div'] = df[f'molecule_atom_index_1_dist_std'] / df['dist']
df[f'molecule_atom_1_dist_mean'] = df.groupby(['molecule_name', 'atom_1'])['dist'].transform('mean')
df[f'molecule_atom_1_dist_min'] = df.groupby(['molecule_name', 'atom_1'])['dist'].transform('min')
df[f'molecule_atom_1_dist_min_diff'] = df[f'molecule_atom_1_dist_min'] - df['dist']
df[f'molecule_atom_1_dist_min_div'] = df[f'molecule_atom_1_dist_min'] / df['dist']
df[f'molecule_atom_1_dist_std'] = df.groupby(['molecule_name', 'atom_1'])['dist'].transform('std')
df[f'molecule_atom_1_dist_std_diff'] = df[f'molecule_atom_1_dist_std'] - df['dist']
df[f'molecule_atom_0_dist_mean'] = df.groupby(['molecule_name', 'atom_0'])['dist'].transform('mean')
df[f'molecule_atom_0_dist_min'] = df.groupby(['molecule_name', 'atom_0'])['dist'].transform('min')
df[f'molecule_atom_0_dist_min_diff'] = df[f'molecule_atom_0_dist_min'] - df['dist']
df[f'molecule_atom_0_dist_min_div'] = df[f'molecule_atom_0_dist_min'] / df['dist']
df[f'molecule_atom_0_dist_std'] = df.groupby(['molecule_name', 'atom_0'])['dist'].transform('std')
df[f'molecule_atom_0_dist_std_diff'] = df[f'molecule_atom_0_dist_std'] - df['dist']
df[f'molecule_type_0_dist_std'] = df.groupby(['molecule_name', 'type_0'])['dist'].transform('std')
df[f'molecule_type_0_dist_std_diff'] = df[f'molecule_type_0_dist_std'] - df['dist']
df[f'molecule_type_dist_mean'] = df.groupby(['molecule_name', 'type'])['dist'].transform('mean')
df[f'molecule_type_dist_mean_diff'] = df[f'molecule_type_dist_mean'] - df['dist']
df[f'molecule_type_dist_mean_div'] = df[f'molecule_type_dist_mean'] / df['dist']
df[f'molecule_type_dist_max'] = df.groupby(['molecule_name', 'type'])['dist'].transform('max')
df[f'molecule_type_dist_min'] = df.groupby(['molecule_name', 'type'])['dist'].transform('min')
df[f'molecule_type_dist_std'] = df.groupby(['molecule_name', 'type'])['dist'].transform('std')
df[f'molecule_type_dist_std_diff'] = df[f'molecule_type_dist_std'] - df['dist']
return df
def dummies(df, list_cols):
for col in list_cols:
df_dummies = pd.get_dummies(df[col], drop_first=True,
prefix=(str(col)))
df = pd.concat([df, df_dummies], axis=1)
return df
def get_correlated_cols(df,threshold=0.98):
'''
threshold: threshold to remove correlated variables
'''
# Absolute value correlation matrix
corr_matrix = df.corr().abs()
# Getting the upper triangle of correlations
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1).astype(np.bool))
# Select columns with correlations above threshold
cols_to_drop = [column for column in upper.columns if any(upper[column] > threshold)]
print('There are {} columns to remove.'.format(len(cols_to_drop)))
return cols_to_drop
def add_qm9_features(df, cols=None):
data_qm9 = pd.read_pickle('../input/quantum-machine-9-qm9/data.covs.pickle')
to_drop = ['type',
'linear',
'atom_index_0',
'atom_index_1',
'scalar_coupling_constant',
'U', 'G', 'H',
'mulliken_mean', 'r2', 'U0']
data_qm9 = data_qm9.drop(columns = to_drop, axis=1)
if cols is not None:
data_qm9 = data_qm9[['molecule_name','id']+cols]
data_qm9 = reduce_mem_usage(data_qm9,verbose=False)
df = pd.merge(df, data_qm9, how='left', on=['molecule_name','id'])
del data_qm9
df = dummies(df, ['type', 'atom_1'])
return df
TOL = 1e-10
def get_chi2_distance(v1, v2):
'''
all columns must be non-negative
compute the weighted Chi-square distance
'''
diff = ((v1 - v2)**2)/(v1+v2+TOL)