7
7
from sklearn import metrics
8
8
from sklearn .model_selection import ParameterGrid
9
9
from sklearn .preprocessing import StandardScaler
10
- from sklearn .metrics import precision_score
11
- from sklearn .metrics import recall_score
12
- from sklearn .metrics import f1_score
13
10
14
11
from sklearn .linear_model import LogisticRegression , SGDClassifier
15
12
from sklearn .svm import SVC , SVR
@@ -41,6 +38,9 @@ def train_gb(df_X, df_y, model_config: dict):
41
38
"""
42
39
Train model with the specified hyper-parameters and return this model (and scaler if any).
43
40
"""
41
+ is_scale = model_config .get ("train" , {}).get ("is_scale" , False )
42
+ is_regression = model_config .get ("train" , {}).get ("is_regression" , False )
43
+
44
44
#
45
45
# Double column set if required
46
46
#
@@ -54,7 +54,6 @@ def train_gb(df_X, df_y, model_config: dict):
54
54
#
55
55
# Scale
56
56
#
57
- is_scale = model_config .get ("train" , {}).get ("is_scale" , False )
58
57
if is_scale :
59
58
scaler = StandardScaler ()
60
59
scaler .fit (df_X )
@@ -176,6 +175,9 @@ def train_nn(df_X, df_y, model_config: dict):
176
175
"""
177
176
Train model with the specified hyper-parameters and return this model (and scaler if any).
178
177
"""
178
+ is_scale = model_config .get ("train" , {}).get ("is_scale" , True )
179
+ is_regression = model_config .get ("train" , {}).get ("is_regression" , False )
180
+
179
181
#
180
182
# Double column set if required
181
183
#
@@ -189,7 +191,6 @@ def train_nn(df_X, df_y, model_config: dict):
189
191
#
190
192
# Scale
191
193
#
192
- is_scale = model_config .get ("train" , {}).get ("is_scale" , True )
193
194
if is_scale :
194
195
scaler = StandardScaler ()
195
196
scaler .fit (df_X )
@@ -227,19 +228,31 @@ def train_nn(df_X, df_y, model_config: dict):
227
228
model .add (Dense (out_features , activation = 'sigmoid' , input_dim = in_features )) # , kernel_regularizer=l2(reg_l2)
228
229
#model.add(Dropout(rate=0.5))
229
230
230
- model .add (Dense (1 , activation = 'sigmoid' ))
231
-
232
- # Compile model
233
- optimizer = Adam (learning_rate = learning_rate )
234
- model .compile (
235
- loss = 'binary_crossentropy' ,
236
- optimizer = optimizer ,
237
- metrics = [
238
- tf .keras .metrics .AUC (name = "auc" ),
239
- tf .keras .metrics .Precision (name = "precision" ),
240
- tf .keras .metrics .Recall (name = "recall" ),
241
- ],
242
- )
231
+ if is_regression :
232
+ model .add (Dense (units = 1 ))
233
+
234
+ model .compile (
235
+ loss = 'mse' ,
236
+ optimizer = Adam (learning_rate = learning_rate ),
237
+ metrics = [
238
+ tf .keras .metrics .MeanAbsoluteError (name = "mean_absolute_error" ),
239
+ tf .keras .metrics .MeanAbsolutePercentageError (name = "mean_absolute_percentage_error" ),
240
+ tf .keras .metrics .R2Score (name = "r2_score" ),
241
+ ],
242
+ )
243
+ else :
244
+ model .add (Dense (units = 1 , activation = 'sigmoid' ))
245
+
246
+ model .compile (
247
+ loss = 'binary_crossentropy' ,
248
+ optimizer = Adam (learning_rate = learning_rate ),
249
+ metrics = [
250
+ tf .keras .metrics .AUC (name = "auc" ),
251
+ tf .keras .metrics .Precision (name = "precision" ),
252
+ tf .keras .metrics .Recall (name = "recall" ),
253
+ ],
254
+ )
255
+
243
256
#model.summary()
244
257
245
258
es = EarlyStopping (
@@ -328,6 +341,9 @@ def train_lc(df_X, df_y, model_config: dict):
328
341
"""
329
342
Train model with the specified hyper-parameters and return this model (and scaler if any).
330
343
"""
344
+ is_scale = model_config .get ("train" , {}).get ("is_scale" , True )
345
+ is_regression = model_config .get ("train" , {}).get ("is_regression" , False )
346
+
331
347
#
332
348
# Double column set if required
333
349
#
@@ -341,7 +357,6 @@ def train_lc(df_X, df_y, model_config: dict):
341
357
#
342
358
# Scale
343
359
#
344
- is_scale = model_config .get ("train" , {}).get ("is_scale" , True )
345
360
if is_scale :
346
361
scaler = StandardScaler ()
347
362
scaler .fit (df_X )
@@ -526,11 +541,65 @@ def compute_scores(y_true, y_hat):
526
541
recall = metrics .recall_score (y_true , y_hat_class )
527
542
528
543
scores = dict (
529
- auc = auc ,
530
- ap = ap , # it summarizes precision-recall curve, should be equivalent to auc
531
- f1 = f1 ,
532
- precision = precision ,
533
- recall = recall ,
544
+ auc = round (auc , 3 ),
545
+ ap = round (ap , 3 ),
546
+ f1 = round (f1 , 3 ),
547
+ precision = round (precision , 3 ),
548
+ recall = round (recall , 3 ),
549
+ )
550
+
551
+ return scores
552
+
553
+
554
+ def compute_scores_regression (y_true , y_hat ):
555
+ """Compute regression scores. Input columns must have numeric data type."""
556
+
557
+ try :
558
+ mae = metrics .mean_absolute_error (y_true , y_hat )
559
+ except ValueError :
560
+ mae = np .nan
561
+
562
+ try :
563
+ mape = metrics .mean_absolute_percentage_error (y_true , y_hat )
564
+ except ValueError :
565
+ mape = np .nan
566
+
567
+ try :
568
+ r2 = metrics .r2_score (y_true , y_hat )
569
+ except ValueError :
570
+ r2 = np .nan
571
+
572
+ #
573
+ # How good it is in predicting the sign (increase of decrease)
574
+ #
575
+
576
+ y_true_class = np .where (y_true .values > 0.0 , + 1 , - 1 )
577
+ y_hat_class = np .where (y_hat .values > 0.0 , + 1 , - 1 )
578
+
579
+ try :
580
+ auc = metrics .roc_auc_score (y_true_class , y_hat_class )
581
+ except ValueError :
582
+ auc = 0.0 # Only one class is present (if dataset is too small, e.g,. when debugging) or Nulls in predictions
583
+
584
+ try :
585
+ ap = metrics .average_precision_score (y_true_class , y_hat_class )
586
+ except ValueError :
587
+ ap = 0.0 # Only one class is present (if dataset is too small, e.g,. when debugging) or Nulls in predictions
588
+
589
+ f1 = metrics .f1_score (y_true_class , y_hat_class )
590
+ precision = metrics .precision_score (y_true_class , y_hat_class )
591
+ recall = metrics .recall_score (y_true_class , y_hat_class )
592
+
593
+ scores = dict (
594
+ mae = round (mae , 3 ),
595
+ mape = round (mape , 3 ),
596
+ r2 = round (r2 , 3 ),
597
+
598
+ auc = round (auc , 3 ),
599
+ ap = round (ap , 3 ),
600
+ f1 = round (f1 , 3 ),
601
+ precision = round (precision , 3 ),
602
+ recall = round (recall , 3 ),
534
603
)
535
604
536
605
return scores
0 commit comments