/
Chefboost.py
636 lines (488 loc) · 19.6 KB
/
Chefboost.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
import time
import pickle
import os
import json
from typing import Optional, Dict, Any, Union
import numpy as np
import pandas as pd
from chefboost.commons import functions, evaluate as cb_eval
from chefboost.training import Training
from chefboost.tuning import gbm, adaboost as adaboost_clf, randomforest
from chefboost.commons.logger import Logger
# pylint: disable=too-many-nested-blocks, no-else-return, inconsistent-return-statements
logger = Logger(module="chefboost/Chefboost.py")
# ------------------------
def fit(
df: pd.DataFrame,
config: Optional[dict] = None,
target_label: str = "Decision",
validation_df: Optional[pd.DataFrame] = None,
silent: bool = False,
) -> Dict[str, Any]:
"""
Build (a) decision tree model(s)
Args:
df (pandas data frame): Training data frame.
config (dictionary): training configuration. e.g.
config = {
'algorithm' (string): ID3, 'C4.5, CART, CHAID or Regression
'enableParallelism' (boolean): False
'enableGBM' (boolean): True,
'epochs' (int): 7,
'learning_rate' (int): 1,
'enableRandomForest' (boolean): True,
'num_of_trees' (int): 5,
'enableAdaboost' (boolean): True,
'num_of_weak_classifier' (int): 4
}
target_label (str): target label for supervised learning.
Default is Decision at the end of dataframe.
validation_df (pandas data frame): validation data frame
if nothing is passed to validation data frame, then the function validates
built trees for training data frame
silent (bool): set this to True if you do not want to see
any informative logs
Returns:
chefboost model
"""
# ------------------------
process_id = os.getpid()
# ------------------------
# rename target column name
if target_label != "Decision":
# TODO: what if another column name is Decision?
df = df.rename(columns={target_label: "Decision"})
# if target is not the last column
if df.columns[-1] != "Decision":
if "Decision" in df.columns:
new_column_order = df.columns.drop("Decision").tolist() + ["Decision"]
logger.debug(new_column_order)
df = df[new_column_order]
else:
raise ValueError("Please set the target_label")
# ------------------------
base_df = df.copy()
# ------------------------
target_label = df.columns[len(df.columns) - 1]
# ------------------------
# handle NaN values
nan_values = []
for column in df.columns:
if df[column].dtypes != "object":
min_value = df[column].min()
idx = df[df[column].isna()].index
nan_value = []
nan_value.append(column)
if idx.shape[0] > 0:
df.loc[idx, column] = min_value - 1
nan_value.append(min_value - 1)
logger.debug("NaN values are replaced to {min_value - 1} in column {column}")
else:
nan_value.append(None)
nan_values.append(nan_value)
# ------------------------
# initialize params and folders
config = functions.initializeParams(config)
functions.initializeFolders()
# ------------------------
algorithm = config["algorithm"]
valid_algorithms = ["ID3", "C4.5", "CART", "CHAID", "Regression"]
if algorithm not in valid_algorithms:
raise ValueError(
"Invalid algorithm passed. You passed ",
algorithm,
" but valid algorithms are ",
valid_algorithms,
)
# ------------------------
enableRandomForest = config["enableRandomForest"]
enableGBM = config["enableGBM"]
enableAdaboost = config["enableAdaboost"]
enableParallelism = config["enableParallelism"]
# ------------------------
if enableParallelism == True:
num_cores = config["num_cores"]
if silent is False:
logger.info(f"[INFO]: {num_cores} CPU cores will be allocated in parallel running")
from multiprocessing import set_start_method, freeze_support
set_start_method("spawn", force=True)
freeze_support()
# ------------------------
num_of_columns = df.shape[1]
if algorithm == "Regression":
if df["Decision"].dtypes == "object":
raise ValueError(
"Regression trees cannot be applied for nominal target values!"
"You can either change the algorithm or data set."
)
if (
df["Decision"].dtypes != "object"
): # this must be regression tree even if it is not mentioned in algorithm
if algorithm != "Regression":
logger.warn(
f"You set the algorithm to {algorithm} but the Decision column of your"
" data set has non-object type."
"That's why, the algorithm is set to Regression to handle the data set."
)
algorithm = "Regression"
config["algorithm"] = "Regression"
if enableGBM == True:
if silent is False:
logger.info("Gradient Boosting Machines...")
algorithm = "Regression"
config["algorithm"] = "Regression"
if enableAdaboost == True:
# enableParallelism = False
for j in range(0, num_of_columns):
column_name = df.columns[j]
if df[column_name].dtypes == "object":
raise ValueError(
"Adaboost must be run on numeric data set for both features and target"
)
# -------------------------
if silent is False:
logger.info(f"{algorithm} tree is going to be built...")
# initialize a dictionary. this is going to be used to check features numeric or nominal.
# numeric features should be transformed to nominal values based on scales.
dataset_features = {}
header = "def findDecision(obj): #"
num_of_columns = df.shape[1] - 1
for i in range(0, num_of_columns):
column_name = df.columns[i]
dataset_features[column_name] = df[column_name].dtypes
header += f"obj[{str(i)}]: {column_name}"
if i != num_of_columns - 1:
header = header + ", "
header = header + "\n"
# ------------------------
begin = time.time()
trees = []
alphas = []
if enableAdaboost == True:
trees, alphas = adaboost_clf.apply(
df,
config,
header,
dataset_features,
validation_df=validation_df,
process_id=process_id,
silent=silent,
)
elif enableGBM == True:
if df["Decision"].dtypes == "object": # transform classification problem to regression
trees, alphas = gbm.classifier(
df,
config,
header,
dataset_features,
validation_df=validation_df,
process_id=process_id,
silent=silent,
)
# classification = True
else: # regression
trees = gbm.regressor(
df,
config,
header,
dataset_features,
validation_df=validation_df,
process_id=process_id,
silent=silent,
)
# classification = False
elif enableRandomForest == True:
trees = randomforest.apply(
df,
config,
header,
dataset_features,
validation_df=validation_df,
process_id=process_id,
silent=silent,
)
else: # regular decision tree building
root = 1
file = "outputs/rules/rules.py"
functions.createFile(file, header)
if enableParallelism == True:
json_file = "outputs/rules/rules.json"
functions.createFile(json_file, "[\n")
trees = Training.buildDecisionTree(
df,
root=root,
file=file,
config=config,
dataset_features=dataset_features,
parent_level=0,
leaf_id=0,
parents="root",
validation_df=validation_df,
main_process_id=process_id,
)
if silent is False:
logger.info("-------------------------")
logger.info(f"finished in {time.time() - begin} seconds")
obj = {"trees": trees, "alphas": alphas, "config": config, "nan_values": nan_values}
# -----------------------------------------
# train set accuracy
df = base_df.copy()
trainset_evaluation = evaluate(obj, df, task="train", silent=silent)
obj["evaluation"] = {"train": trainset_evaluation}
# validation set accuracy
if isinstance(validation_df, pd.DataFrame):
validationset_evaluation = evaluate(obj, validation_df, task="validation", silent=silent)
obj["evaluation"]["validation"] = validationset_evaluation
return obj
# -----------------------------------------
def predict(model: dict, param: list) -> Union[str, int, float]:
"""
Predict the target label of given features from a pre-trained model
Args:
model (built chefboost model): pre-trained model which is the output
of fit function
param (list): pass input features as python list
e.g. chef.predict(model, param = ['Sunny', 'Hot', 'High', 'Weak'])
Returns:
prediction
"""
trees = model["trees"]
config = model["config"]
alphas = []
if "alphas" in model:
alphas = model["alphas"]
nan_values = []
if "nan_values" in model:
nan_values = model["nan_values"]
# -----------------------
# handle missing values
column_index = 0
for column in nan_values:
column_name = column[0]
missing_value = column[1]
if pd.isna(missing_value) != True:
logger.debug(
f"missing values will be replaced with {missing_value} in {column_name} column"
)
if pd.isna(param[column_index]):
param[column_index] = missing_value
column_index = column_index + 1
logger.debug(f"instance: {param}")
# -----------------------
enableGBM = config["enableGBM"]
adaboost = config["enableAdaboost"]
enableRandomForest = config["enableRandomForest"]
# -----------------------
classification = False
prediction = 0
prediction_classes = []
# -----------------------
if enableGBM == True:
if len(trees) == config["epochs"]:
classification = False
else:
classification = True
prediction_classes = [0 for i in alphas]
# -----------------------
if len(trees) > 1: # bagging or boosting
index = 0
for tree in trees:
if adaboost != True:
custom_prediction = tree.findDecision(param)
if custom_prediction != None:
if not isinstance(custom_prediction, str): # regression
if enableGBM == True and classification == True:
prediction_classes[index % len(alphas)] += custom_prediction
else:
prediction += custom_prediction
else:
classification = True
prediction_classes.append(custom_prediction)
else: # adaboost
prediction += alphas[index] * tree.findDecision(param)
index = index + 1
if enableRandomForest == True:
# notice that gbm requires cumilative sum but random forest requires mean of each tree
prediction = prediction / len(trees)
if adaboost == True:
prediction = functions.sign(prediction)
else: # regular decision tree
tree = trees[0]
prediction = tree.findDecision(param)
if classification == False:
return prediction
else:
if enableGBM == True and classification == True:
return alphas[np.argmax(prediction_classes)]
else: # classification
# e.g. random forest
# get predictions made by different trees
predictions = np.array(prediction_classes)
# find the most frequent prediction
(values, counts) = np.unique(predictions, return_counts=True)
idx = np.argmax(counts)
prediction = values[idx]
return prediction
def save_model(base_model: dict, file_name: str = "model.pkl") -> None:
"""
Save pre-trained model on file system
Args:
base_model (dict): pre-trained model which is the output
of the fit function
file_name (string): target file name as exact path.
"""
model = base_model.copy()
# modules cannot be saved. Save its reference instead.
module_names = []
for tree in model["trees"]:
module_names.append(tree.__name__)
model["trees"] = module_names
with open(f"outputs/rules/{file_name}", "wb") as f:
pickle.dump(model, f)
def load_model(file_name: str = "model.pkl") -> dict:
"""
Load the save pre-trained model from file system
Args:
file_name (str): exact path of the target saved model
Returns:
built model (dict)
"""
with open("outputs/rules/" + file_name, "rb") as f:
model = pickle.load(f)
# restore modules from its references
modules = []
for model_name in model["trees"]:
module = functions.restoreTree(model_name)
modules.append(module)
model["trees"] = modules
return model
def restoreTree(module_name) -> Any:
"""
Load built model from set of decision rules
Args:
module_name (str): e.g. outputs/rules/rules to restore outputs/rules/rules.py
Returns:
built model (dict)
"""
return functions.restoreTree(module_name)
def feature_importance(rules: Union[str, list], silent: bool = False) -> pd.DataFrame:
"""
Show the feature importance values of a built model
Args:
rules (str or list): e.g. decision_rules = "outputs/rules/rules.py"
or this could be retrieved from built model as shown below.
```python
decision_rules = []
for tree in model["trees"]:
rule = .__dict__["__spec__"].origin
decision_rules.append(rule)
```
silent (bool): set this to True if you do want to see
any informative logs.
Returns:
feature importance (pd.DataFrame)
"""
if not isinstance(rules, list):
rules = [rules]
if silent is False:
logger.info(f"rules: {rules}")
# -----------------------------
dfs = []
for rule in rules:
if silent is False:
logger.info(f"Decision rule: {rule}")
with open(rule, "r", encoding="UTF-8") as file:
lines = file.readlines()
pivot = {}
rules = []
# initialize feature importances
line_idx = 0
for line in lines:
if line_idx == 0:
feature_explainer_list = line.split("#")[1].split(", ")
for feature_explainer in feature_explainer_list:
feature = feature_explainer.split(": ")[1].replace("\n", "")
pivot[feature] = 0
else:
if "# " in line:
rule = line.strip().split("# ")[1]
rules.append(json.loads(rule))
line_idx = line_idx + 1
feature_names = list(pivot.keys())
for feature in feature_names:
for rule in rules:
if rule["feature"] == feature:
score = rule["metric_value"] * rule["instances"]
current_depth = rule["depth"]
child_scores = 0
# find child node importances
for child_rule in rules:
if child_rule["depth"] == current_depth + 1:
child_score = child_rule["metric_value"] * child_rule["instances"]
child_scores = child_scores + child_score
score = score - child_scores
pivot[feature] = pivot[feature] + score
# normalize feature importance
total_score = 0
for feature, score in pivot.items():
total_score = total_score + score
for feature, score in pivot.items():
pivot[feature] = round(pivot[feature] / total_score, 4)
instances = []
for feature, score in pivot.items():
instance = []
instance.append(feature)
instance.append(score)
instances.append(instance)
df = pd.DataFrame(instances, columns=["feature", "final_importance"])
df = df.sort_values(by=["final_importance"], ascending=False)
if len(rules) == 1:
return df
else:
dfs.append(df)
if len(rules) > 1:
hf = pd.DataFrame(feature_names, columns=["feature"])
hf["importance"] = 0
for df in dfs:
hf = hf.merge(df, on=["feature"], how="left")
hf["importance"] = hf["importance"] + hf["final_importance"]
hf = hf.drop(columns=["final_importance"])
# ------------------------
# normalize
hf["importance"] = hf["importance"] / hf["importance"].sum()
hf = hf.sort_values(by=["importance"], ascending=False)
return hf
def evaluate(
model: dict,
df: pd.DataFrame,
target_label: str = "Decision",
task: str = "test",
silent: bool = False,
) -> dict:
"""
Evaluate the performance of a built model on a data set
Args:
model (dict): built model which is the output of fit function
df (pandas data frame): data frame you would like to evaluate
target_label (str): target label
task (string): set this to train, validation or test
silent (bool): set this to True if you do not want to see
any informative logs
Returns:
evaluation results (dict)
"""
# --------------------------
if target_label != "Decision":
df = df.rename(columns={target_label: "Decision"})
# if target is not the last column
if df.columns[-1] != "Decision":
new_column_order = df.columns.drop("Decision").tolist() + ["Decision"]
logger.debug(new_column_order)
df = df[new_column_order]
# --------------------------
functions.bulk_prediction(df, model)
enableAdaboost = model["config"]["enableAdaboost"]
if enableAdaboost == True:
df["Decision"] = df["Decision"].astype(str)
df["Prediction"] = df["Prediction"].astype(str)
return cb_eval.evaluate(df, task=task, silent=silent)