@@ -343,7 +343,10 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
343
343
and (est [0 ] in self .estimators )
344
344
)
345
345
] + [
346
- ("GBoostClassifier(MultiTask(" + est [0 ] + "))" , partial (MultiTaskRegressor , regr = est [1 ]()))
346
+ (
347
+ "GBoostClassifier(MultiTask(" + est [0 ] + "))" ,
348
+ partial (MultiTaskRegressor , regr = est [1 ]()),
349
+ )
347
350
for est in all_estimators ()
348
351
if (
349
352
issubclass (est [1 ], RegressorMixin )
@@ -352,7 +355,7 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
352
355
]
353
356
354
357
if self .preprocess is True :
355
-
358
+
356
359
if self .n_jobs is None :
357
360
358
361
for name , model in tqdm (self .classifiers ): # do parallel exec
@@ -376,7 +379,9 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
376
379
fitted_clf = GenericBoostingClassifier (
377
380
{** other_args , ** kwargs },
378
381
verbose = self .verbose ,
379
- base_model = model (random_state = self .random_state ),
382
+ base_model = model (
383
+ random_state = self .random_state
384
+ ),
380
385
)
381
386
382
387
else :
@@ -401,15 +406,19 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
401
406
pipe .fit (X_train , y_train )
402
407
self .models_ [name ] = pipe
403
408
y_pred = pipe .predict (X_test )
404
- accuracy = accuracy_score (y_test , y_pred , normalize = True )
409
+ accuracy = accuracy_score (
410
+ y_test , y_pred , normalize = True
411
+ )
405
412
b_accuracy = balanced_accuracy_score (y_test , y_pred )
406
413
f1 = f1_score (y_test , y_pred , average = "weighted" )
407
414
try :
408
415
roc_auc = roc_auc_score (y_test , y_pred )
409
416
except Exception as exception :
410
417
roc_auc = None
411
418
if self .ignore_warnings is False :
412
- print ("ROC AUC couldn't be calculated for " + name )
419
+ print (
420
+ "ROC AUC couldn't be calculated for " + name
421
+ )
413
422
print (exception )
414
423
names .append (name )
415
424
Accuracy .append (accuracy )
@@ -452,15 +461,24 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
452
461
print (exception )
453
462
454
463
else :
455
-
456
- # train_model(self, name, model, X_train, y_train, X_test, y_test,
457
- #use_preprocessing=False, preprocessor=None,
464
+
465
+ # train_model(self, name, model, X_train, y_train, X_test, y_test,
466
+ # use_preprocessing=False, preprocessor=None,
458
467
# **kwargs):
459
- results = Parallel (n_jobs = self .n_jobs )(delayed (self .train_model )(
460
- name , model , X_train , y_train , X_test , y_test ,
461
- use_preprocessing = True , preprocessor = preprocessor , ** kwargs
462
- ) for name , model in tqdm (self .classifiers )
463
- )
468
+ results = Parallel (n_jobs = self .n_jobs )(
469
+ delayed (self .train_model )(
470
+ name ,
471
+ model ,
472
+ X_train ,
473
+ y_train ,
474
+ X_test ,
475
+ y_test ,
476
+ use_preprocessing = True ,
477
+ preprocessor = preprocessor ,
478
+ ** kwargs
479
+ )
480
+ for name , model in tqdm (self .classifiers )
481
+ )
464
482
Accuracy = [res ["accuracy" ] for res in results ]
465
483
B_Accuracy = [res ["balanced_accuracy" ] for res in results ]
466
484
ROC_AUC = [res ["roc_auc" ] for res in results ]
@@ -470,41 +488,50 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
470
488
if self .custom_metric is not None :
471
489
CUSTOM_METRIC = [res ["custom_metric" ] for res in results ]
472
490
if self .predictions :
473
- predictions = {res ["name" ]: res ["predictions" ] for res in results }
474
-
491
+ predictions = {
492
+ res ["name" ]: res ["predictions" ] for res in results
493
+ }
475
494
476
495
else : # no preprocessing
477
-
496
+
478
497
if self .n_jobs is None :
479
498
480
499
for name , model in tqdm (self .classifiers ): # do parallel exec
481
500
start = time .time ()
482
501
try :
483
502
if "random_state" in model ().get_params ().keys ():
484
503
fitted_clf = GenericBoostingClassifier (
485
- base_model = model (random_state = self .random_state ),
504
+ base_model = model (
505
+ random_state = self .random_state
506
+ ),
486
507
verbose = self .verbose ,
487
508
** kwargs
488
509
)
489
510
490
511
else :
491
512
fitted_clf = GenericBoostingClassifier (
492
- base_model = model (), verbose = self .verbose , ** kwargs
513
+ base_model = model (),
514
+ verbose = self .verbose ,
515
+ ** kwargs
493
516
)
494
517
495
518
fitted_clf .fit (X_train , y_train )
496
519
497
520
self .models_ [name ] = fitted_clf
498
521
y_pred = fitted_clf .predict (X_test )
499
- accuracy = accuracy_score (y_test , y_pred , normalize = True )
522
+ accuracy = accuracy_score (
523
+ y_test , y_pred , normalize = True
524
+ )
500
525
b_accuracy = balanced_accuracy_score (y_test , y_pred )
501
526
f1 = f1_score (y_test , y_pred , average = "weighted" )
502
527
try :
503
528
roc_auc = roc_auc_score (y_test , y_pred )
504
529
except Exception as exception :
505
530
roc_auc = None
506
531
if self .ignore_warnings is False :
507
- print ("ROC AUC couldn't be calculated for " + name )
532
+ print (
533
+ "ROC AUC couldn't be calculated for " + name
534
+ )
508
535
print (exception )
509
536
names .append (name )
510
537
Accuracy .append (accuracy )
@@ -546,13 +573,21 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
546
573
print (name + " model failed to execute" )
547
574
print (exception )
548
575
549
- else :
576
+ else :
550
577
551
- results = Parallel (n_jobs = self .n_jobs )(delayed (self .train_model )(
552
- name , model , X_train , y_train , X_test , y_test ,
553
- use_preprocessing = False , ** kwargs
554
- ) for name , model in tqdm (self .classifiers )
555
- )
578
+ results = Parallel (n_jobs = self .n_jobs )(
579
+ delayed (self .train_model )(
580
+ name ,
581
+ model ,
582
+ X_train ,
583
+ y_train ,
584
+ X_test ,
585
+ y_test ,
586
+ use_preprocessing = False ,
587
+ ** kwargs
588
+ )
589
+ for name , model in tqdm (self .classifiers )
590
+ )
556
591
Accuracy = [res ["accuracy" ] for res in results ]
557
592
B_Accuracy = [res ["balanced_accuracy" ] for res in results ]
558
593
ROC_AUC = [res ["roc_auc" ] for res in results ]
@@ -562,8 +597,9 @@ def fit(self, X_train, X_test, y_train, y_test, **kwargs):
562
597
if self .custom_metric is not None :
563
598
CUSTOM_METRIC = [res ["custom_metric" ] for res in results ]
564
599
if self .predictions :
565
- predictions = {res ["name" ]: res ["predictions" ] for res in results }
566
-
600
+ predictions = {
601
+ res ["name" ]: res ["predictions" ] for res in results
602
+ }
567
603
568
604
if self .custom_metric is None :
569
605
scores = pd .DataFrame (
@@ -643,18 +679,29 @@ def provide_models(self, X_train, X_test, y_train, y_test):
643
679
644
680
return self .models_
645
681
646
-
647
- def train_model (self , name , model , X_train , y_train , X_test , y_test ,
648
- use_preprocessing = False , preprocessor = None ,
649
- ** kwargs ):
682
+ def train_model (
683
+ self ,
684
+ name ,
685
+ model ,
686
+ X_train ,
687
+ y_train ,
688
+ X_test ,
689
+ y_test ,
690
+ use_preprocessing = False ,
691
+ preprocessor = None ,
692
+ ** kwargs
693
+ ):
650
694
"""
651
695
Function to train a single model and return its results.
652
696
"""
653
697
other_args = {}
654
698
655
699
# Handle n_jobs parameter
656
700
try :
657
- if "n_jobs" in model ().get_params ().keys () and "LogisticRegression" not in name :
701
+ if (
702
+ "n_jobs" in model ().get_params ().keys ()
703
+ and "LogisticRegression" not in name
704
+ ):
658
705
other_args ["n_jobs" ] = self .n_jobs
659
706
except Exception :
660
707
pass
@@ -688,13 +735,21 @@ def train_model(self, name, model, X_train, y_train, X_test, y_test,
688
735
]
689
736
)
690
737
if self .verbose > 0 :
691
- print ("\n Fitting pipeline with preprocessing for " + name + " model..." )
738
+ print (
739
+ "\n Fitting pipeline with preprocessing for "
740
+ + name
741
+ + " model..."
742
+ )
692
743
pipe .fit (X_train , y_train )
693
744
y_pred = pipe .predict (X_test )
694
745
else :
695
746
# Case with no preprocessing
696
747
if self .verbose > 0 :
697
- print ("\n Fitting model without preprocessing for " + name + " model..." )
748
+ print (
749
+ "\n Fitting model without preprocessing for "
750
+ + name
751
+ + " model..."
752
+ )
698
753
y_pred = fitted_clf .predict (X_test )
699
754
700
755
accuracy = accuracy_score (y_test , y_pred , normalize = True )
@@ -728,4 +783,4 @@ def train_model(self, name, model, X_train, y_train, X_test, y_test,
728
783
if self .ignore_warnings is False :
729
784
print (name + " model failed to execute" )
730
785
print (exception )
731
- return None
786
+ return None
0 commit comments