|
| 1 | +from sklearn.ensemble import RandomForestClassifier |
| 2 | +from sklearn.model_selection import RandomizedSearchCV |
| 3 | +import xgboost |
| 4 | +import lightgbm |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | +import data_preprocess as dp |
| 8 | +import cudf |
| 9 | +from cuml.ensemble import RandomForestClassifier as cuRFC |
| 10 | +import time |
| 11 | + |
| 12 | +# List of hyperparameters to search for the Random Forest scikit-learn implementation |
| 13 | +rf_parameters = { |
| 14 | +'bootstrap': [True, False], |
| 15 | +'max_depth': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, None], |
| 16 | +'min_samples_leaf': [1, 2, 4], |
| 17 | +'min_samples_split': [2, 5, 10], |
| 18 | +'n_estimators': [100, 150, 200, 250, 500, 750, 1000]} |
| 19 | + |
| 20 | +# List of hyperparameters to search for the XGBoost gradient boosting implementation |
| 21 | +gdb_parameters = { |
| 22 | +'max_depth': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, None], |
| 23 | +'learning_rate': [0.001, 0.01, 0.1, 0.2, 0.3], |
| 24 | +'subsample': [0.5, 0.6, 0.7, 0.8, 0.9, 1.0], |
| 25 | +'colsample_bytree': [0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], |
| 26 | +'colsample_bylevel': [0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], |
| 27 | +'min_child_weight': [0.5, 1.0, 3.0, 5.0, 7.0, 10.0], |
| 28 | +'gamma': [0, 0.25, 0.5, 1.0], |
| 29 | +'reg_lambda': [0.1, 1.0, 5.0, 10.0, 50.0, 100.0], |
| 30 | +'n_estimators': [100, 150, 200, 250, 500, 750, 1000]} |
| 31 | + |
| 32 | +lgbm_parameters = { |
| 33 | +'max_depth':[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, None], |
| 34 | +'learning_rate': [0.001, 0.01, 0.1, 0.2, 0.3], |
| 35 | +'subsample': [0.5, 0.6, 0.7, 0.8, 0.9, 1.0], |
| 36 | +'colsample_bytree': [0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], |
| 37 | +'min_child_weight': [0.5, 1.0, 3.0, 5.0, 7.0, 10.0], |
| 38 | +'reg_lambda': [0.1, 1.0, 5.0, 10.0, 50.0, 100.0], |
| 39 | +'n_estimators': [100, 150, 200, 250, 500, 750, 1000]} |
| 40 | + |
| 41 | + |
| 42 | +# Classification wrapper used to select the correct classifier based on the configuration file selection |
| 43 | +def get_model(classifier, hyper_opt): |
| 44 | + |
| 45 | + # Classifier: "rf" |
| 46 | + # Random Forest, scikit-learn |
| 47 | + if classifier == 'rf': |
| 48 | + #model = RandomForestClassifier() |
| 49 | + model = cuRFC() |
| 50 | + # Random Search CV used for Hyperparameter optimization, sets up the operation for |
| 51 | + # going through the list of hyperparameters above and selects best performing model |
| 52 | + if hyper_opt == "random_search": |
| 53 | + model = RandomizedSearchCV(model, rf_parameters, n_iter=30, |
| 54 | + n_jobs=-1, verbose=0, cv=5, |
| 55 | + scoring='roc_auc', refit=True, random_state=42) |
| 56 | + # Classifier: "gdb" |
| 57 | + # Gradient Boosting, xgboost |
| 58 | + elif classifier == 'gdb': |
| 59 | + model = xgboost.XGBClassifier(eval_metric='logloss') |
| 60 | + # model = xgboost.XGBClassifier(eval_metric='logloss') |
| 61 | + # Random Search CV used for Hyperparameter optimization, sets up the operation for |
| 62 | + # going through the list of hyperparameters above and selects best performing model |
| 63 | + if hyper_opt == "random_search": |
| 64 | + model = RandomizedSearchCV(model, gdb_parameters, n_iter=30, |
| 65 | + n_jobs=-1, verbose=0, cv=5, |
| 66 | + scoring='roc_auc', refit=True, random_state=42) |
| 67 | + elif classifier == 'lgbm': |
| 68 | + model = lightgbm.LGBMClassifier() |
| 69 | + # Random Search CV used for Hyperparameter optimization, sets up the operation for |
| 70 | + # going through the list of hyperparameters above and selects best performing model |
| 71 | + if hyper_opt == "random_search": |
| 72 | + model = RandomizedSearchCV(model, lgbm_parameters, n_iter=30, |
| 73 | + n_jobs=-1, verbose=0, cv=5, |
| 74 | + scoring='roc_auc', refit=True, random_state=42) |
| 75 | + else: |
| 76 | + sys.exit("ERROR: Unrecognized classification technique in configuration file. Please pick one or more from these options: ['rf', 'gdb']") |
| 77 | + return model |
| 78 | + |
| 79 | +# Function that tranverse the data matrix so that it matches with the sickit-learn format and converts the labels to binary format |
| 80 | +def prepare_dataset(x, y): |
| 81 | + x = x.T |
| 82 | + y = y.apply(lambda x: dp.bool_to_binary(x)) |
| 83 | + start = time.time() |
| 84 | + x = cudf.from_pandas(x) |
| 85 | + y = cudf.from_pandas(y) |
| 86 | + end = time.time() |
| 87 | + print("COPY ARRAY: ", end - start) |
| 88 | + return x, y |
| 89 | + |
| 90 | +# Performs the classifier training using the training dataset |
| 91 | +def model_train(path, x, y, classifier, debug_mode, iteration, hyper_opt, best_parameters): |
| 92 | + # DEBUG MODE |
| 93 | + if debug_mode: |
| 94 | + # Saves input training dataset and labels |
| 95 | + debug_path = path + classifier + "/debug/" + str(iteration) + "/" |
| 96 | + dp.make_result_dir(debug_path) |
| 97 | + x.to_csv(debug_path + "/input_dataset.tsv", sep="\t") |
| 98 | + y.to_csv(debug_path + "/labels.tsv", sep="\t") |
| 99 | + |
| 100 | + # Selects correct model |
| 101 | + model = get_model(classifier, hyper_opt) |
| 102 | + x, y = prepare_dataset(x, y) |
| 103 | + if hyper_opt == "best": |
| 104 | + #print(best_parameters[1]) |
| 105 | + #print(best_parameters) |
| 106 | + model.set_params(**best_parameters[1]) |
| 107 | + # Transforms the dataset for correct scikit-learn format |
| 108 | + print("CLASSIFIER: " + classifier) |
| 109 | + # Trains the model |
| 110 | + start = time.time() |
| 111 | + model.fit(x, y) |
| 112 | + end = time.time() |
| 113 | + print("CLASSIFIER TRAINING TIME: ", end - start) |
| 114 | + |
| 115 | + if hyper_opt == "random_search": |
| 116 | + print(hyper_opt) |
| 117 | + best_parameters = model.best_params_ |
| 118 | + |
| 119 | + # DEBUG MODE |
| 120 | + if debug_mode: |
| 121 | + # Saves the trained model |
| 122 | + # Load function can be implemented to load the model back for debugging purposes |
| 123 | + from joblib import dump, load |
| 124 | + dump(model, debug_path + 'model.joblib') |
| 125 | + |
| 126 | + return model, best_parameters |
0 commit comments