All Courses

#### Master's In Artificial Intelligence Job Guarantee Program

4.5 (1,292 Ratings)

559 Learners

#### How To Land a Job in Data Science?

Feb 9th (7:00 PM) 187 Registered
More webinars

# H2O - AutoML

Pallavi Parekh

a year ago

• Importing AutoML
• Initialize H2O
• Preparing Dataset
• Applying AutoML
• Predicting on Test Data
• Printing Result
• Printing the Ranking for All
• Conclusion

To use AutoML, start a new Jupyter notebook and follow the steps shown below.

### Importing AutoML

First import H2O and AutoML package into the project using the following two statements −
import h2o

from h2o.automl import H2OAutoML

### Initialize H2O

Initialize h2o using the following statement −
h2o.init()
You should see the cluster information on the screen as shown in the screenshot below −

We will use the same iris.csv dataset that you used earlier in this tutorial. Load the data using the following statement −
data = h2o.import_file('iris.csv')

### Preparing Dataset

We need to decide on the features and the prediction columns. We use the same features and the predication column as in our earlier case. Set the features and the output column using the following two statements −
features = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']

output = 'class'
Split the data in 80:20 ratio for training and testing −
train, test = data.split_frame(ratios=[0.8])

### Applying AutoML

Now, we are all set for applying AutoML on our dataset. The AutoML will run for a fixed amount of time set by us and give us the optimized model. We set up the AutoML using the following statement −
aml = H2OAutoML(max_models = 30, max_runtime_secs=300, seed = 1)
The first parameter specifies the number of models that we want to evaluate and compare.
The second parameter specifies the time for which the algorithm runs.
We now call the training method on the AutoML object as shown here −
aml.train(x = features, y = output, training_frame = train)
We specify the x as the features array that we created earlier, the y as the output variable to indicate the predicted value and the dataframe as train dataset.
Run the code, you will have to wait for 5 minutes (we set the max_runtime_secs to 300) until you get the following output −

When the AutoML processing completes, it creates a leaderboard ranking of all the 30 algorithms that it has evaluated. To see the first 10 records of the leaderboard, use the following code −

Upon execution, the above code will generate the following output −
Clearly, the DeepLearning algorithm has got the maximum score.

### Predicting on Test Data

Now, you have the models ranked, you can see the performance of the top-rated model on your test data. To do so, run the following code statement −
preds = aml.predict(test)
The processing continues for a while and you will see the following output when it completes.

### Printing Result

Print the predicted result using the following statement −
print (preds)
Upon execution of the above statement, you will see the following result −

### Printing the Ranking for All

If you want to see the ranks of all the tested algorithms, run the following code statement −