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.
Join the DZone community and get the full member experience.
Join For FreeIntroduction 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.
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.
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:
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)
Opinions expressed by DZone contributors are their own.
Comments