1 Load data and model

load("data/customer_churn.RData")
model_keras = load_model_hdf5("model/customer_churn.hdf5", compile = FALSE)

2 Customer scorecard inputs

main_vars = c("tenure", "Contract", "InternetService", "MonthlyCharges", "OnlineBackup",
    "OnlineSecurity", "DeviceProtection", "TechSupport", "StreamingMovies", "PhoneService")

commercial_vars = c("InternetService", "OnlineBackup", "OnlineSecurity", "DeviceProtection",
    "TechSupport", "StreamingMovies", "PhoneService")

financial_vars = c("PaymentMethod")

customer_feature_vars = c(main_vars, commercial_vars, financial_vars) %>%
    unique

3 Transform original datasset

churn_data_raw = read_csv("data/WA_Fn-UseC_-Telco-Customer-Churn.csv") %>%
    mutate(tenure_range = case_when(tenure < 12 ~ "< 1 Yr", tenure < 24 ~ "1-2 Yrs",
        tenure < 36 ~ "2-3 Yrs", tenure >= 36 ~ "Over 3 Yrs", TRUE ~ "NA"), monthly_charge_range = case_when(MonthlyCharges <
        20 ~ "< 20 per Month", MonthlyCharges < 50 ~ "20-50 per Month", MonthlyCharges <
        100 ~ "50-100 per Month", MonthlyCharges >= 100 ~ "Over 100 per Month", TRUE ~
        "NA"))

churn_data_tbl = churn_data_raw %>%
    drop_na() %>%
    select(Churn, everything())

4 Set-up model with Keras + LIME

assign("model_type.keras.engine.sequential.Sequential", envir = globalenv(), function(x,
    ...) {
    "classification"
})

5 Predict model with Keras + LIME

assign("predict_model.keras.engine.sequential.Sequential", envir = globalenv(), function(x,
    newdata, type, ...) {
    pred = predict_proba(object = x, x = as.matrix(newdata))
    data.frame(Yes = pred, No = 1 - pred)
})

6 Customer Churn Risk Analytics