In [1]:
# Start and connect to a local H2O cluster
import h2o
h2o.init(nthreads = -1)
In [2]:
# Import Titanic data (local CSV)
titanic = h2o.import_file("kaggle_titanic.csv")
titanic.head(5)
Out[2]:
In [3]:
# Convert 'Survived' and 'Pclass' to categorical values
titanic['Survived'] = titanic['Survived'].asfactor()
titanic['Pclass'] = titanic['Pclass'].asfactor()
In [4]:
titanic['Survived'].table()
Out[4]:
In [5]:
titanic['Pclass'].table()
Out[5]:
In [6]:
titanic['Sex'].table()
Out[6]:
In [7]:
titanic['Age'].hist()
In [8]:
titanic['SibSp'].hist()
In [9]:
titanic['Parch'].hist()
In [10]:
titanic['Fare'].hist()
In [11]:
titanic['Embarked'].table()
Out[11]:
In [12]:
# Define features (or predictors) manually
features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
In [13]:
# Split the H2O data frame into training/test sets
# so we can evaluate out-of-bag performance
titanic_split = titanic.split_frame(ratios = [0.8], seed = 1234)
titanic_train = titanic_split[0] # using 80% for training
titanic_test = titanic_split[1] # using the rest 20% for out-of-bag evaluation
In [14]:
titanic_train.shape
Out[14]:
In [15]:
titanic_test.shape
Out[15]:
In [16]:
# Build a Generalized Linear Model (GLM) with default settings
# Import the function for GLM
from h2o.estimators.glm import H2OGeneralizedLinearEstimator
# Set up GLM for binary classification
glm_default = H2OGeneralizedLinearEstimator(family = 'binomial', model_id = 'glm_default')
# Use .train() to build the model
glm_default.train(x = features,
y = 'Survived',
training_frame = titanic_train)
In [17]:
# Check the model performance on training dataset
glm_default
Out[17]:
In [18]:
# Check the model performance on test dataset
glm_default.model_performance(titanic_test)
Out[18]:
In [19]:
# Build a Distributed Random Forest (DRF) model with default settings
# Import the function for DRF
from h2o.estimators.random_forest import H2ORandomForestEstimator
# Set up DRF for regression
# Add a seed for reproducibility
drf_default = H2ORandomForestEstimator(model_id = 'drf_default', seed = 1234)
# Use .train() to build the model
drf_default.train(x = features,
y = 'Survived',
training_frame = titanic_train)
In [20]:
# Check the DRF model summary
drf_default
Out[20]:
In [21]:
# Check the model performance on test dataset
drf_default.model_performance(titanic_test)
Out[21]:
In [22]:
# Build a Gradient Boosting Machines (GBM) model with default settings
# Import the function for GBM
from h2o.estimators.gbm import H2OGradientBoostingEstimator
# Set up GBM for regression
# Add a seed for reproducibility
gbm_default = H2OGradientBoostingEstimator(model_id = 'gbm_default', seed = 1234)
# Use .train() to build the model
gbm_default.train(x = features,
y = 'Survived',
training_frame = titanic_train)
In [23]:
# Check the GBM model summary
gbm_default
Out[23]:
In [24]:
# Check the model performance on test dataset
gbm_default.model_performance(titanic_test)
Out[24]:
In [25]:
# Build a Deep Learning (Deep Neural Networks, DNN) model with default settings
# Import the function for DNN
from h2o.estimators.deeplearning import H2ODeepLearningEstimator
# Set up DNN for regression
dnn_default = H2ODeepLearningEstimator(model_id = 'dnn_default')
# (not run) Change 'reproducible' to True if you want to reproduce the results
# The model will be built using a single thread (could be very slow)
# dnn_default = H2ODeepLearningEstimator(model_id = 'dnn_default', reproducible = True)
# Use .train() to build the model
dnn_default.train(x = features,
y = 'Survived',
training_frame = titanic_train)
In [26]:
# Check the DNN model summary
dnn_default
Out[26]:
In [27]:
# Check the model performance on test dataset
dnn_default.model_performance(titanic_test)
Out[27]:
In [28]:
# Use GLM model to make predictions
yhat_test_glm = glm_default.predict(titanic_test)
yhat_test_glm.head(5)
Out[28]:
In [29]:
# Use DRF model to make predictions
yhat_test_drf = drf_default.predict(titanic_test)
yhat_test_drf.head(5)
Out[29]:
In [30]:
# Use GBM model to make predictions
yhat_test_gbm = gbm_default.predict(titanic_test)
yhat_test_gbm.head(5)
Out[30]:
In [31]:
# Use DNN model to make predictions
yhat_test_dnn = dnn_default.predict(titanic_test)
yhat_test_dnn.head(5)
Out[31]: