Automating Machine Learning
By: Himanshu Sharma
Tags: Machine Learning, Python, Automation, Automl, Data Science

Automating Machine Learning

Using H2O AutoML to automate machine learning

Photo by Franck V. on Unsplash

Machine Learning and Artificial Intelligence are the most used terms nowadays. Everyone nowadays wants to learn and experiment with Machine Learning but it doesn’t seem easy, because you need to learn a lot about different concepts of machine learning and how to implement them.

There are many learning interfaces in the market that makes machine learning an easy task for non-experts but still, you need knowledge of data science and different machine learning models.

H2O AutoML is an easy to use machine learning interface which automates the whole process of Machine Learning model selection by automatically training and tuning the model in a stipulated time limit.

A user just needs the dataset, should know the target variable and set a time limit, with all this he can test different machine learning model and select the best model with results.

Let us now explore how we can use H2O AutoML and automate the Machine Learning journey.

Installing H2O AutoML

Like any other python library, we can install H2O AutoML using pip install command given below.

pip install h2o

Importing necessary Libraries and loading dataset

The dataset we will be using here is an advertising dataset which contains the sales of a company as the dependant variable with features like ‘Radio’, ‘TV’ and ‘Newspaper’.

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

df = pd.read_csv('Advertising.csv')

df.head()

Advertising Dataset

Importing H2O and Initializing it

Here we will H2O library and initialize the H2O instance. It checks whether an instance is already running or not. Also displays several other details which you will see below in the image of the output.

import h2o

h2o.init()

Initializing the H2O Library

Pass dataset to H2OFRAME

This will create an H2OFrame similar to that of a data frame but with some more properties.

adver_df = h2o.H2OFrame(df)

adver_df.describe()

Statistical Properties of the H2OFrame

Splitting the Training and Test Data

Here we will split data into 50% training and 50% test dataset. Also, we will define our target value from the dataset and features o which it depends.

train, test = adver_df.split_frame(ratios=[.50])

x = train.columns

y = "Sales"

x.remove(y)

Run the H2O AutoML Model

We will import the H2O AutoML and run it.

from h2o.automl import H2OAutoML

aml = H2OAutoML(max_runtime_secs=600,

seed=1,

balance_classes=False,

project_name='Advertising'

)

%time aml.train(x=x, y=y, training_frame=train)

This code will render the data through different machine learning models in the stipulated time limit which we have taken as 600 Seconds, in this time it will store the performance of all the models through which data is passed.

Now we will check out the leaderboard of the model performance and find out which model performance is the best i.e. which model has the lowest error and highest accuracy.

lb = aml.leaderboard

lb.head()

Model Performance Leaderboard

Next, we will take the best performing model and find the variable which is important for the dependent variable.

se = aml.leader

#Loading Stack Ensambled Metelearner model metalearner = h2o.get_model(se.metalearner()['name']) metalearner.varimp()

Best Performing Model

Now we will analyze the best model we have got.

model = h2o.get_model('DeepLearning_grid__1_AutoML_20200731_222821_model_1') model.model_performance(test)

Model Performance

Visualizing the importance of features.

model.varimp_plot(num_of_features=3)

Variable Importance

Here we can clearly see that ‘TV’ is the most important feature in ‘Sales’ prediction. Let us visualize its dependence on Sales.

model.partial_plot(train, cols=["TV"], figsize=(5,5));

‘TV’ Dependence

H2O AutoML also allows you to save your model and use it later when required.

model_path = h2o.save_model(model=model, path='C:/Users/advert',

force=True)

Conclusion

In this article, we saw how we can use H2O AutoML to automate the whole process of Machine Learning and save a lot of time and effort.

Before You Go

Thanks for reading! If you want to get in touch with me, feel free to reach me on hmix13@gmail.com or my LinkedIn Profile. You can also view my Github profile for different projects on data science.