DZone
Big Data Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Big Data Zone > Exploring and Transforming H2O DataFrame in R and Python

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.

Avkash Chauhan user avatar by
Avkash Chauhan
·
Oct. 08, 17 · Big Data Zone · Tutorial
Like (5)
Save
Tweet
11.14K Views

Join the DZone community and get the full member experience.

Join For Free

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')
df.types
{    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
df.as_data_frame().hist(figsize=(20,20))
pl.show()

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):
        try:
            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:

N=100
set.seed(999)
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)
table(fdata)
dd = data.frame(color,sex,num,fdata)
data = as.h2o(dd)
str(data)
data$sex = h2o.setLevels(x = data$sex ,levels = c("F","M"))
data

That's it — enjoy!

R (programming language) Python (language) H2O (web server)

Published at DZone with permission of Avkash Chauhan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Integrate Event Streaming Into Your Applications
  • OPC-UA, MQTT, and Apache Kafka: The Trinity of Data Streaming in IoT
  • What Is ERP Testing? - A Brief Guide
  • OLAs vs. SLAs vs. UCs: What They Mean and How They're Different

Comments

Big Data Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo