Prabuddha21/encrypted_credit_scoring
0
1"""Train and compile the model."""2 3import shutil4import numpy5import pandas6import pickle7 8from settings import (9 DEPLOYMENT_PATH,10 DATA_PATH, 11 INPUT_SLICES, 12 PRE_PROCESSOR_APPLICANT_PATH, 13 PRE_PROCESSOR_BANK_PATH,14 PRE_PROCESSOR_CREDIT_BUREAU_PATH,15 APPLICANT_COLUMNS,16 BANK_COLUMNS,17 CREDIT_BUREAU_COLUMNS,18)19from utils.client_server_interface import MultiInputsFHEModelDev20from utils.model import MultiInputDecisionTreeClassifier21from utils.pre_processing import get_pre_processors22 23 24def get_multi_inputs(data):25 """Get inputs for all three parties from the input data, using fixed slices.26 27 Args:28 data (numpy.ndarray): The input data to consider.29 30 Returns:31 (Tuple[numpy.ndarray]): The inputs for all three parties.32 """33 return (34 data[:, INPUT_SLICES["applicant"]], 35 data[:, INPUT_SLICES["bank"]], 36 data[:, INPUT_SLICES["credit_bureau"]]37 )38 39 40print("Load and pre-process the data")41 42# Load the data43data = pandas.read_csv(DATA_PATH, encoding="utf-8")44 45# Define input and target data46data_x = data.copy()47data_y = data_x.pop("Target").copy().to_frame()48 49# Get data from all parties50data_applicant = data_x[APPLICANT_COLUMNS].copy()51data_bank = data_x[BANK_COLUMNS].copy()52data_credit_bureau = data_x[CREDIT_BUREAU_COLUMNS].copy()53 54# Feature engineer the data55pre_processor_applicant, pre_processor_bank, pre_processor_credit_bureau = get_pre_processors()56 57preprocessed_data_applicant = pre_processor_applicant.fit_transform(data_applicant)58preprocessed_data_bank = pre_processor_bank.fit_transform(data_bank)59preprocessed_data_credit_bureau = pre_processor_credit_bureau.fit_transform(data_credit_bureau)60 61preprocessed_data_x = numpy.concatenate((preprocessed_data_applicant, preprocessed_data_bank, preprocessed_data_credit_bureau), axis=1)62 63 64print("\nTrain and compile the model")65 66model = MultiInputDecisionTreeClassifier()67 68model, sklearn_model = model.fit_benchmark(preprocessed_data_x, data_y)69 70multi_inputs_train = get_multi_inputs(preprocessed_data_x)71 72model.compile(*multi_inputs_train, inputs_encryption_status=["encrypted", "encrypted", "encrypted"])73 74print("\nSave deployment files")75 76# Delete the deployment folder and its content if it already exists77if DEPLOYMENT_PATH.is_dir():78 shutil.rmtree(DEPLOYMENT_PATH)79 80# Save files needed for deployment (and enable cross-platform deployment)81fhe_model_dev = MultiInputsFHEModelDev(DEPLOYMENT_PATH, model)82fhe_model_dev.save(via_mlir=True)83 84# Save pre-processors85with (86 PRE_PROCESSOR_APPLICANT_PATH.open('wb') as file_applicant, 87 PRE_PROCESSOR_BANK_PATH.open('wb') as file_bank,88 PRE_PROCESSOR_CREDIT_BUREAU_PATH.open('wb') as file_credit_bureau,89):90 pickle.dump(pre_processor_applicant, file_applicant)91 pickle.dump(pre_processor_bank, file_bank)92 pickle.dump(pre_processor_credit_bureau, file_credit_bureau)93 94print("\nDone !")95 