DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Using an AutoML H2O Model to Predict Attrition and LIME to Explain the Predicted Class

Using an AutoML H2O Model to Predict Attrition and LIME to Explain the Predicted Class

This article will be explain how to build a model for an employee attrition use case.

Francis C. Fernandez-Reyes user avatar by
Francis C. Fernandez-Reyes
·
Jun. 14, 18 · Tutorial
Like (4)
Save
Tweet
Share
11.71K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction to H2O

H2O.ai is an open-source software for big data analysis. It allows users to fit thousands of potential models as part of discovering patterns in data. The software has an interface to the following programming languages: Java (6 or later), Python (2.7.x, 3.5.x), R (3.0.0 or later) and Scala (1.4-1.6). The H2O software can be run on conventional operating-systems and on big-data systems, particularly Apache Hadoop Distributed File System (HDFS) and also on cloud environments such as Amazon EC2, Google Compute Engine, and Microsoft Azure. Figure 1 shows the architecture of H2O and its integration with different tools.

Figure 1: H2O framework and integrations. (Taken from H2O documentation)

Once businesses identify a problem that can be resolved through machine learning, they ask data scientists to create a predictive analytics solution. In many cases, the turnaround time for delivering a solution is pretty long.

Even for qualified data scientists, developing machine learning models that can accurately predict the results is always challenging and time-consuming. The complex workflow involved in machine learning models have multiple stages. Some of the significant steps include data acquisition, data exploration, feature engineering, model selection, experimentation, and prediction.

One trend that will be changing the face of ML-based solutions is AutoML. It is going to enable business analysts and developers to develop machine learning models that can address complex scenarios.

AutoML focuses on data acquisition and prediction. All the steps that take place in between these two phases will be abstracted by the AutoML platform. Essentially, users bring their own dataset, identify the labels, and press a button to generate a trained and optimized model that is ready to predict. Figure 2 shows the main differences between traditional ML and AutoML.

Figure 2: Main differences between Traditional Machine Learning and AutoML

H2O provides a module to make AutoML models using different programming languages or their user interface.

In this article, I will be explaining how we built a model for an employee attrition use case. As we know, employee attrition is one of the principal issues for all companies in terms of retaining key talents in their organization. We developed a model using AutoML in order to predict if an employee is going to leave the company. We also provide an explanation of possible features that are affecting the model prediction using LIME.

Employee Attrition Model Combining LIME and H2O

is a novel explanation technique that explains the predictions of any classifier in an interpretable and faithful manner by learning an interpretable model locally around the prediction. We create a wrapper class to connect our H2O model to a tabular explainer in LIME. Figure 3 shows our architecture:

Figure 3: Employee Attrition Model

Firstly, we split the HR Dataset into: training and test set. We first initialize the H2O cluster and then train the model using the training set and testing set for validation (see code below).

import h2o
from h2o.automl import H2OAutoML
h2o.init()
aml = H2OAutoML()
aml.train(x = feature_names, y = 'class', training_frame = train_h2o_df, leaderboard_frame = test_h2o_df)

We next create the wrapper class to connect to LIME tabular (see code below).

class h2o_predict_proba_wrapper:
    # h2o object, the column_names is the labels of the X values
    def __init__(self,model,column_names):            
        self.model = model
        self.column_names = column_names

    def predict_proba(self,this_array):        
        # If we have just 1 row of data we need to reshape it
        shape_tuple = np.shape(this_array)        
        if len(shape_tuple) == 1:
            this_array = this_array.reshape(1, -1)

       # convert the pandas dataframe to an h2o frame
        self.pandas_df = pd.DataFrame(data = this_array,columns = self.column_names)
        self.h2o_df = h2o.H2OFrame(self.pandas_df)

        # Predict with the h2o model
        self.predictions = self.model.predict(self.h2o_df).as_data_frame()
        # the first column is the class labels, the rest are probabilities for each class
        self.predictions = self.predictions.iloc[:,1:].as_matrix()
        return self.predictions

Finally, we use the LIME explainer class to predict the top 5 most important features that influence in both predicted classes.  

import lime 
import lime.lime_tabular

h2o_drf_wrapper = h2o_predict_proba_wrapper(aml.leader,feature_names)
explainer = lime.lime_tabular.LimeTabularExplainer(train ,class_names=class_names, feature_names = feature_names, categorical_features=categorical_features, categorical_names=categorical_names,kernel_width=3, verbose=True)
i = 27
exp = explainer.explain_instance(test[i], h2o_drf_wrapper.predict_proba, num_features=5)
H2O (web server) Machine learning Lime (software) Attrition (website) Big data Data science

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Keep Your Application Secrets Secret
  • Fargate vs. Lambda: The Battle of the Future
  • Testing Level Dynamics: Achieving Confidence From Testing
  • The 5 Books You Absolutely Must Read as an Engineering Manager

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: