Skip to content

royles/tko_telco_churn

Repository files navigation

TKO Lab

Telco churn with refactor code

This is the CML port of the Refractor prototype which is part of the Interpretability report from Cloudera Fast Forward Labs.

Setup

Start a Python 3 Session with at least 8GB of memory and run the utils/setup.py code. This will create the minimum setup to use existing, pretrained models.

1 Ingest Data

Open 1_data_ingest.py in a workbench: python3, 1 CPU, 2 GB.

Change the hadoop principle to your user name. i.e. replace jfletcher in the file with your trail21xx user.

.config("spark.hadoop.yarn.resourcemanager.principal","jfletcher")\

Run the file.

2 Explore Data

Open a jupyter notebook at open the 2_data_exploration.ipynb file

3 Train Models

A model has been pre-trained and placed in the models directory.

If you want to retrain the model run the 3_train_models.py code to train a new model.

The model artifact will be saved in the models directory named after the datestamp, dataset and algorithm (ie. 20191120T161757_ibm_linear). The default settings will create a linear regression model against the IBM telco dataset. However, the code is vary modular and can train multiple model types against essentially any tabular dataset (see below for details).

4 Serve Models

Go to the Models section and create a new predictor model with the following:

  • Name: Predictor
  • Description: Predict customer churn
  • File: 4_model_serve_predictor.py
  • Function: predict
  • Input: {"StreamingTV":"No","MonthlyCharges":70.35,"PhoneService":"No","PaperlessBilling":"No","Partner":"No","OnlineBackup":"No","gender":"Female","Contract":"Month-to-month","TotalCharges":1397.475,"StreamingMovies":"No","DeviceProtection":"No","PaymentMethod":"Bank transfer (automatic)","tenure":29,"Dependents":"No","OnlineSecurity":"No","MultipleLines":"No","InternetService":"DSL","SeniorCitizen":"No","TechSupport":"No"}
  • Kernel: Python 3

If you created your own model (see above)

  • Click on "Set Environment Variables" and add:
    • Name: CHURN_MODEL_NAME
    • Value: 20191120T161757_telco_linear your model name from above Click "Add" and "Deploy Model"

Create a new Explainer model with the following:

  • Name: Explainer
  • Description: Explain customer churn prediction
  • File: 4_model_serve_explainer.py
  • Function: explain
  • Input: {"StreamingTV":"No","MonthlyCharges":70.35,"PhoneService":"No","PaperlessBilling":"No","Partner":"No","OnlineBackup":"No","gender":"Female","Contract":"Month-to-month","TotalCharges":1397.475,"StreamingMovies":"No","DeviceProtection":"No","PaymentMethod":"Bank transfer (automatic)","tenure":29,"Dependents":"No","OnlineSecurity":"No","MultipleLines":"No","InternetService":"DSL","SeniorCitizen":"No","TechSupport":"No"}
  • Kernel: Python 3

If you created your own model (see above)

  • Click on "Set Environment Variables" and add:
    • Name: CHURN_MODEL_NAME
    • Value: 20191120T161757_telco_linear your model name from above Click "Add" and "Deploy Model"

In the deployed Explainer model -> Settings note (copy) the "Access Key" (ie. mukd9sit7tacnfq2phhn3whc4unq1f38)

5 Deploy Application

From the Project level click on "Open Workbench" (note you don't actually have to Launch a session) in order to edit a file. Select the flask/single_view.html file and paste the Access Key in at line 19. Save and go back to the Project.

Go to the Applications section and select "New Application" with the following:

  • Name: Visual Churn Analysis
  • Subdomain: telco-churn
  • Script: 5_application.py
  • Kernel: Python 3
  • Engine Profile: 1vCPU / 2 GiB Memory

If you created your own model (see above)

  • Add Environment Variables:
    • Name: CHURN_MODEL_NAME
    • Value: 20191120T161757_tekci_linear your model name from above
      Click "Add" and "Deploy Model"

After the Application deploys, click on the blue-arrow next to the name. The initial view is a table of rows selected at random from the dataset. This shows a global view of which features are most important for the predictor model.

Clicking on any single row will show a "local" interpretabilty of a particular instance. Here you can see how adjusting any one of the features will change the instance's churn prediction.

OLD README

Refractor (or churnexplainer)

CML Applications: Train and inspect a new model locally

This project uses the Applications feature of CML (>=1.2) and CDSW (>=1.7) to instantiate a UI frontend for visual interpretability and decision management.

Deploy a Predictor and Explainer models

Go to the Models section and create a new predictor model with the following:

  • Name: Predictor
  • Description: Predict customer churn
  • File: predictor.py
  • Function: predict
  • Input: {"StreamingTV":"No","MonthlyCharges":70.35,"PhoneService":"No","PaperlessBilling":"No","Partner":"No","OnlineBackup":"No","gender":"Female","Contract":"Month-to-month","TotalCharges":1397.475,"StreamingMovies":"No","DeviceProtection":"No","PaymentMethod":"Bank transfer (automatic)","tenure":29,"Dependents":"No","OnlineSecurity":"No","MultipleLines":"No","InternetService":"DSL","SeniorCitizen":"No","TechSupport":"No"}
  • Kernel: Python 3

If you created your own model (see above)

  • Click on "Set Environment Variables" and add:
    • Name: CHURN_MODEL_NAME
    • Value: 20191120T161757_ibm_linear your model name from above Click "Add" and "Deploy Model"

Create a new Explainer model with the following:

  • Name: Explainer
  • Description: Explain customer churn prediction
  • File: explainer.py
  • Function: explain
  • Input: {"StreamingTV":"No","MonthlyCharges":70.35,"PhoneService":"No","PaperlessBilling":"No","Partner":"No","OnlineBackup":"No","gender":"Female","Contract":"Month-to-month","TotalCharges":1397.475,"StreamingMovies":"No","DeviceProtection":"No","PaymentMethod":"Bank transfer (automatic)","tenure":29,"Dependents":"No","OnlineSecurity":"No","MultipleLines":"No","InternetService":"DSL","SeniorCitizen":"No","TechSupport":"No"}
  • Kernel: Python 3

If you created your own model (see above)

  • Click on "Set Environment Variables" and add:
    • Name: CHURN_MODEL_NAME
    • Value: 20191120T161757_ibm_linear your model name from above Click "Add" and "Deploy Model"

In the deployed Explainer model -> Settings note (copy) the "Access Key" (ie. mukd9sit7tacnfq2phhn3whc4unq1f38)

Instatiate the flask UI application

** Don't forget** to stop your Models and Experiments once you are done to save resources for your colleagues.

Additional options

By default this code trains a linear regression model against the IBM dataset.
There are other datasets and other model types as well. Look at train_multiple.py for examples or set the Project environment variables to try other datasets and models:
Name Value
CHURN_DATASET ibm (default) | breastcancer | iris
CHURN_MODEL_TYPE linear (default) | gb | nonlinear | voting

NOTE that not all of these options have been fully tested so your mileage may vary.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published