Over a million developers have joined DZone.

Logistic Regression With H2O Deep Learning in Scala [Code Snippet]

DZone's Guide to

Logistic Regression With H2O Deep Learning in Scala [Code Snippet]

In this heavily code-based tutorial, learn how to perform logistic regression using feed-forward network-based deep learning algorithms and H2O.

· AI Zone
Free Resource

Find out how AI-Fueled APIs from Neura can make interesting products more exciting and engaging. 

Here is some sample code that shows an example of using feed-forward network-based deep learning algorithms with H2O to perform logistic regression.

First, let's import key classes specific to H2O:

import org.apache.spark.h2o._
import water.Key
import java.io.File

Now, we will create H2O context so that we can call key H2O functions specific to data ingest and deep learning algorithms:

val h2oContext = H2OContext.getOrCreate(sc)
import h2oContext._
import h2oContext.implicits._

Let's import data from a local file system as an H2O DataFrame:

val prostateData = new H2OFrame(new File("/Users/avkashchauhan/src/github.com/h2oai/sparkling-water/examples/smalldata/prostate.csv"))

Next, let's import deep learning classes:

import root.hex.deeplearning.DeepLearning
import root.hex.deeplearning.DeepLearningModel.DeepLearningParameters

Now, we will define all key parameters specific to the H2O deep learning algorithm:

val dlParams = new DeepLearningParameters()
dlParams._epochs = 100
dlParams._train = prostateData
dlParams._response_column = 'CAPSULE
dlParams._variable_importances = true
dlParams._nfolds = 5
dlParams._seed = 1111
dlParams._keep_cross_validation_predictions = true;

Now we will create the Deep Learning Algorithm key first and then start the deep learning algorithm in blocking mode:

val dl = new DeepLearning(dlParams, Key.make("dlProstateModel.hex"))
val dlModel = dl.trainModel.get()

Let's learn more about our model with dlModel.

Now, we can perform the prediction by passing an H2O DataFrame. Here, I am simply passing the original DataFrame. However, you can load your test DataFrame and pass it as an H2O frame to perform prediction:

val predictionH2OFrame = dlModel.score(prostateData)('predict)
val predictionsFromModel = asRDD[DoubleHolder](predictionH2OFrame).collect.map(_.result.getOrElse(Double.NaN))

That's it — enjoy!

To find out how AI-Fueled APIs can increase engagement and retention, download Six Ways to Boost Engagement for Your IoT Device or App with AI today.

h2o ,machine learning ,regression model ,deep learning ,predictive analytics ,ai

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}