Over a million developers have joined DZone.

Exploring and Transforming H2O DataFrame in R and Python

DZone's Guide to

Exploring and Transforming H2O DataFrame in R and Python

In this code-heavy tutorial, learn how to ingest datasets for building models using H2O DataFrames as well as R and Python code.

· Big Data Zone ·
Free Resource

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

Sometimes, you may need to ingest a dataset for building models. Your first task will be to explore all the features and their type. Once that is done, you may want to change the feature types to be the way you want them to be.

Here is the code snippet in Python:

df = h2o.import_file('https://raw.githubusercontent.com/h2oai/sparkling-water/master/examples/smalldata/prostate.csv')
{    u'AGE': u'int', u'CAPSULE': u'int', u'DCAPS': u'int', 
     u'DPROS': u'int', u'GLEASON': u'int', u'ID': u'int',
     u'PSA': u'real', u'RACE': u'int', u'VOL': u'real'

If you would like to visualize all the features in a graphical format, you can do the following:

import pylab as pl

The result looks like this on Jupyter notebook:

Screen Shot 2017-10-05 at 5.20.03 PM

Note: If you have above 50 features, you might have to trim your DataFrame to fewer features in order to have an effective visualization.

You can also use the following function to convert a list of columns as factor/categorical by passing the H2O DataFrame and a list of columns:

def convert_columns_as_factor(hdf, column_list):
    list_count = len(column_list)
    if list_count is 0:
        return "Error: You don't have a list of binary columns."
    if (len(pdf.columns)) is 0:
        return "Error: You don't have any columns in your data frame."
    local_column_list = pdf.columns
    for i in range(list_count):
            target_index = local_column_list.index(column_list[i])
            pdf[column_list[i]] = pdf[column_list[i]].asfactor()
            print('Column ' + column_list[i] + " is converted into factor/catagorical.")
        except ValueError:
            print('Error: ' + str(column_list[i]) + " not found in the data frame.")

The following script is in R to perform the same above tasks:

color = sample(c("D","E","I","F","M"),size=N,replace=TRUE)
num = rnorm(N,mean = 12,sd = 21212)
sex = sample(c("male","female"),size=N,replace=TRUE)
sex = as.factor(sex)
color = as.factor(color)
data = sample(c(0,1),size = N,replace = T)
fdata = factor(data)
dd = data.frame(color,sex,num,fdata)
data = as.h2o(dd)
data$sex = h2o.setLevels(x = data$sex ,levels = c("F","M"))

That's it — enjoy!

Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub.  Join the discussion.

h2o ,python ,r ,visualization ,tutorial ,big data ,dataframe

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}