encrypted_credit_scoring / development.py
romanbredehoft-zama's picture
Update to synthetic data-set
18ba8c1
raw history blame
No virus
2.59 kB
"""Train and compile the model."""
import shutil
import numpy
import pandas
import pickle
from settings import (
DEPLOYMENT_PATH,
DATA_PATH,
INPUT_SLICES,
PRE_PROCESSOR_USER_PATH,
PRE_PROCESSOR_BANK_PATH,
PRE_PROCESSOR_THIRD_PARTY_PATH,
USER_COLUMNS,
BANK_COLUMNS,
THIRD_PARTY_COLUMNS,
)
from utils.client_server_interface import MultiInputsFHEModelDev
from utils.model import MultiInputXGBClassifier
from utils.pre_processing import get_pre_processors
def get_processed_multi_inputs(data):
return (
data[:, INPUT_SLICES["user"]],
data[:, INPUT_SLICES["bank"]],
data[:, INPUT_SLICES["third_party"]]
)
print("Load and pre-process the data")
# Load the data
data = pandas.read_csv(DATA_PATH, encoding="utf-8")
# Define input and target data
data_x = data.copy()
data_y = data_x.pop("Target").copy().to_frame()
# Get data from all parties
data_user = data_x[USER_COLUMNS].copy()
data_bank = data_x[BANK_COLUMNS].copy()
data_third_party = data_x[THIRD_PARTY_COLUMNS].copy()
# Feature engineer the data
pre_processor_user, pre_processor_bank, pre_processor_third_party = get_pre_processors()
preprocessed_data_user = pre_processor_user.fit_transform(data_user)
preprocessed_data_bank = pre_processor_bank.fit_transform(data_bank)
preprocessed_data_third_party = pre_processor_third_party.fit_transform(data_third_party)
preprocessed_data_x = numpy.concatenate((preprocessed_data_user, preprocessed_data_bank, preprocessed_data_third_party), axis=1)
print("\nTrain and compile the model")
model = MultiInputXGBClassifier(max_depth=3, n_estimators=20)
model, sklearn_model = model.fit_benchmark(preprocessed_data_x, data_y)
multi_inputs_train = get_processed_multi_inputs(preprocessed_data_x)
model.compile(*multi_inputs_train, inputs_encryption_status=["encrypted", "encrypted", "encrypted"])
# Delete the deployment folder and its content if it already exists
if DEPLOYMENT_PATH.is_dir():
shutil.rmtree(DEPLOYMENT_PATH)
print("\nSave deployment files")
# Save files needed for deployment (and enable cross-platform deployment)
fhe_dev = MultiInputsFHEModelDev(DEPLOYMENT_PATH, model)
fhe_dev.save(via_mlir=True)
# Save pre-processors
with (
PRE_PROCESSOR_USER_PATH.open('wb') as file_user,
PRE_PROCESSOR_BANK_PATH.open('wb') as file_bank,
PRE_PROCESSOR_THIRD_PARTY_PATH.open('wb') as file_third_party,
):
pickle.dump(pre_processor_user, file_user)
pickle.dump(pre_processor_bank, file_bank)
pickle.dump(pre_processor_third_party, file_third_party)
print("\nDone !")