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.
Join the DZone community and get the full member experience.
Join For FreeSometimes, 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:
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!
Published at DZone with permission of Avkash Chauhan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments