spark-instrumented-optimizer/docs/ml-ensembles.md
Xusen Yin 9154f89bef [SPARK-11728] Replace example code in ml-ensembles.md using include_example
JIRA issue https://issues.apache.org/jira/browse/SPARK-11728.

The ml-ensembles.md file contains `OneVsRestExample`. Instead of writing new code files of two `OneVsRestExample`s, I use two existing files in the examples directory, they are `OneVsRestExample.scala` and `JavaOneVsRestExample.scala`.

Author: Xusen Yin <yinxusen@gmail.com>

Closes #9716 from yinxusen/SPARK-11728.
2015-11-17 23:44:06 -08:00

11 KiB

layout title displayTitle
global Ensembles <a href="ml-guide.html">ML</a> - Ensembles

Table of Contents

  • This will become a table of contents (this text will be scraped). {:toc}

An ensemble method is a learning algorithm which creates a model composed of a set of other base models.

Tree Ensembles

The Pipelines API supports two major tree ensemble algorithms: Random Forests and Gradient-Boosted Trees (GBTs). Both use MLlib decision trees as their base models.

Users can find more information about ensemble algorithms in the MLlib Ensemble guide. In this section, we demonstrate the Pipelines API for ensembles.

The main differences between this API and the original MLlib ensembles API are:

  • support for ML Pipelines
  • separation of classification vs. regression
  • use of DataFrame metadata to distinguish continuous and categorical features
  • a bit more functionality for random forests: estimates of feature importance, as well as the predicted probability of each class (a.k.a. class conditional probabilities) for classification.

Random Forests

Random forests are ensembles of decision trees. Random forests combine many decision trees in order to reduce the risk of overfitting. MLlib supports random forests for binary and multiclass classification and for regression, using both continuous and categorical features.

This section gives examples of using random forests with the Pipelines API. For more information on the algorithm, please see the main MLlib docs on random forests.

Inputs and Outputs

We list the input and output (prediction) column types here. All output columns are optional; to exclude an output column, set its corresponding Param to an empty string.

Input Columns
Param name Type(s) Default Description
labelCol Double "label" Label to predict
featuresCol Vector "features" Feature vector
Output Columns (Predictions)
Param name Type(s) Default Description Notes
predictionCol Double "prediction" Predicted label
rawPredictionCol Vector "rawPrediction" Vector of length # classes, with the counts of training instance labels at the tree node which makes the prediction Classification only
probabilityCol Vector "probability" Vector of length # classes equal to rawPrediction normalized to a multinomial distribution Classification only

Example: Classification

The following examples load a dataset in LibSVM format, split it into training and test sets, train on the first dataset, and then evaluate on the held-out test set. We use two feature transformers to prepare the data; these help index categories for the label and categorical features, adding metadata to the DataFrame which the tree-based algorithms can recognize.

Refer to the Scala API docs for more details.

{% include_example scala/org/apache/spark/examples/ml/RandomForestClassifierExample.scala %}

Refer to the Java API docs for more details.

{% include_example java/org/apache/spark/examples/ml/JavaRandomForestClassifierExample.java %}

Refer to the Python API docs for more details.

{% include_example python/ml/random_forest_classifier_example.py %}

Example: Regression

The following examples load a dataset in LibSVM format, split it into training and test sets, train on the first dataset, and then evaluate on the held-out test set. We use a feature transformer to index categorical features, adding metadata to the DataFrame which the tree-based algorithms can recognize.

Refer to the Scala API docs for more details.

{% include_example scala/org/apache/spark/examples/ml/RandomForestRegressorExample.scala %}

Refer to the Java API docs for more details.

{% include_example java/org/apache/spark/examples/ml/JavaRandomForestRegressorExample.java %}

Refer to the Python API docs for more details.

{% include_example python/ml/random_forest_regressor_example.py %}

Gradient-Boosted Trees (GBTs)

Gradient-Boosted Trees (GBTs) are ensembles of decision trees. GBTs iteratively train decision trees in order to minimize a loss function. MLlib supports GBTs for binary classification and for regression, using both continuous and categorical features.

This section gives examples of using GBTs with the Pipelines API. For more information on the algorithm, please see the main MLlib docs on GBTs.

Inputs and Outputs

We list the input and output (prediction) column types here. All output columns are optional; to exclude an output column, set its corresponding Param to an empty string.

Input Columns
Param name Type(s) Default Description
labelCol Double "label" Label to predict
featuresCol Vector "features" Feature vector

Note that GBTClassifier currently only supports binary labels.

Output Columns (Predictions)
Param name Type(s) Default Description Notes
predictionCol Double "prediction" Predicted label

In the future, GBTClassifier will also output columns for rawPrediction and probability, just as RandomForestClassifier does.

Example: Classification

The following examples load a dataset in LibSVM format, split it into training and test sets, train on the first dataset, and then evaluate on the held-out test set. We use two feature transformers to prepare the data; these help index categories for the label and categorical features, adding metadata to the DataFrame which the tree-based algorithms can recognize.

Refer to the Scala API docs for more details.

{% include_example scala/org/apache/spark/examples/ml/GradientBoostedTreeClassifierExample.scala %}

Refer to the Java API docs for more details.

{% include_example java/org/apache/spark/examples/ml/JavaGradientBoostedTreeClassifierExample.java %}

Refer to the Python API docs for more details.

{% include_example python/ml/gradient_boosted_tree_classifier_example.py %}

Example: Regression

Note: For this example dataset, GBTRegressor actually only needs 1 iteration, but that will not be true in general.

Refer to the Scala API docs for more details.

{% include_example scala/org/apache/spark/examples/ml/GradientBoostedTreeRegressorExample.scala %}

Refer to the Java API docs for more details.

{% include_example java/org/apache/spark/examples/ml/JavaGradientBoostedTreeRegressorExample.java %}

Refer to the Python API docs for more details.

{% include_example python/ml/gradient_boosted_tree_regressor_example.py %}

One-vs-Rest (a.k.a. One-vs-All)

OneVsRest is an example of a machine learning reduction for performing multiclass classification given a base classifier that can perform binary classification efficiently. It is also known as "One-vs-All."

OneVsRest is implemented as an Estimator. For the base classifier it takes instances of Classifier and creates a binary classification problem for each of the k classes. The classifier for class i is trained to predict whether the label is i or not, distinguishing class i from all other classes.

Predictions are done by evaluating each binary classifier and the index of the most confident classifier is output as label.

Example

The example below demonstrates how to load the Iris dataset, parse it as a DataFrame and perform multiclass classification using OneVsRest. The test error is calculated to measure the algorithm accuracy.

Refer to the Scala API docs for more details.

{% include_example scala/org/apache/spark/examples/ml/OneVsRestExample.scala %}

Refer to the Java API docs for more details.

{% include_example java/org/apache/spark/examples/ml/JavaOneVsRestExample.java %}