Project: Apache Spark (App Definition and Development / Streaming & Messaging)
File: ml-classification-regression.md

layout: global title: Classification and regression displayTitle: Classification and regression license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. `\[ \newcommand{\R}{\mathbb{R}} \newcommand{\E}{\mathbb{E}} \newcommand{\x}{\mathbf{x}} \newcommand{\y}{\mathbf{y}} \newcommand{\wv}{\mathbf{w}} \newcommand{\av}{\mathbf{\alpha}} \newcommand{\bv}{\mathbf{b}} \newcommand{\N}{\mathbb{N}} \newcommand{\id}{\mathbf{I}} \newcommand{\ind}{\mathbf{1}} \newcommand{\0}{\mathbf{0}} \newcommand{\unit}{\mathbf{e}} \newcommand{\one}{\mathbf{1}} \newcommand{\zero}{\mathbf{0}} \]` This page covers algorithms for Classification and Regression. It also includes sections discussing specific classes of algorithms, such as linear methods, trees, and ensembles. Table of Contents This will become a table of contents (this text will be scraped). {:toc} Logistic regression is a popular method to predict a categorical response. It is a special case of that predicts the probability of the outcomes. In `spark.ml` logistic regression can be used to predict a binary outcome by using binomial logistic regression, or it can be used to predict a multiclass outcome by using multinomial logistic regression. Use the `family` parameter to select between these two algorithms, or leave it unset and Spark will infer the correct variant. > Multinomial logistic regression can be used for binary classification by setting the `family` param to "multinomial". It will produce two sets of coefficients and two intercepts. > When fitting LogisticRegressionModel without intercept on dataset with constant nonzero column, Spark MLlib outputs zero coefficients for constant nonzero columns. This behavior is the same as R glmnet but different from LIBSVM. For more background and more details about the implementation of binomial logistic regression, refer to the documentation of . Examples The following example shows how to train binomial and multinomial logistic regression models for binary classification with elastic net regularization. `elasticNetParam` corresponds to $\alpha$ and `regParam` corresponds to $\lambda$. <div class="codetabs"> <div data-lang="python" markdown="1"> More details on parameters can be found in the . {% includeexample python/ml/logisticregressionwithelastic_net.py %} </div> <div data-lang="scala" markdown="1"> More details on parameters can be found in the . {% include_example scala/org/apache/spark/examples/ml/LogisticRegressionWithElasticNetExample.scala %} </div> <div data-lang="java" markdown="1"> More details on parameters can be found in the . {% include_example java/org/apache/spark/examples/ml/JavaLogisticRegressionWithElasticNetExample.java %} </div> <div data-lang="r" markdown="1"> More details on parameters can be found in the . {% include_example binomial r/ml/logit.R %} </div> </div> The `spark.ml` implementation of logistic regression also supports extracting a summary of the model over the training set. Note that the predictions and metrics which are stored as `DataFrame` in `LogisticRegressionSummary` are annotated `@transient` and hence only available on the driver. <div class="codetabs"> <div data-lang="python" markdown="1"> provides a summary for a . In the case of binary classification, certain additional metrics are available, e.g. ROC curve. See . Continuing the earlier example: {% includeexample python/ml/logisticregressionsummaryexample.py %} </div> <div data-lang="scala" markdown="1"> provides a summary for a . In the case of binary classification, certain additional metrics are available, e.g. ROC curve. The binary summary can be accessed via the `binarySummary` method. See . Continuing the earlier example: {% include_example scala/org/apache/spark/examples/ml/LogisticRegressionSummaryExample.scala %} </div> <div data-lang="java" markdown="1"> provides a summary for a . In the case of binary classification, certain additional metrics are available, e.g. ROC

The binary summary can be accessed via the `binarySummary` method. See . Continuing the earlier example: {% include_example java/org/apache/spark/examples/ml/JavaLogisticRegressionSummaryExample.java %} </div> </div> Multiclass classification is supported via multinomial logistic (softmax) regression. In multinomial logistic regression, the algorithm produces $K$ sets of coefficients, or a matrix of dimension $K \times J$ where $K$ is the number of outcome classes and $J$ is the number of features. If the algorithm is fit with an intercept term then a length $K$ vector of intercepts is available. > Multinomial coefficients are available as `coefficientMatrix` and intercepts are available as `interceptVector`. > `coefficients` and `intercept` methods on a logistic regression model trained with multinomial family are not supported. Use `coefficientMatrix` and `interceptVector` instead. The conditional probabilities of the outcome classes $k \in \{1, 2, ..., K\}$ are modeled using the softmax function. `\[ P(Y=k|\mathbf{X}, \boldsymbol{\beta}k, \beta{0k}) = \frac{e^{\boldsymbol{\beta}k \cdot \mathbf{X} + \beta{0k}}}{\sum{k'=0}^{K-1} e^{\boldsymbol{\beta}{k'} \cdot \mathbf{X} + \beta_{0k'}}} \]` We minimize the weighted negative log-likelihood, using a multinomial response model, with elastic-net penalty to control for overfitting. `\[ \min{\beta, \beta0} -\left[\sum{i=1}^L wi \cdot \log P(Y = yi|\mathbf{x}i)\right] + \lambda \left[\frac{1}{2}\left(1 - \alpha\right)||\boldsymbol{\beta}||2^2 + \alpha ||\boldsymbol{\beta}||1\right] \]` For a detailed derivation please see . Examples The following example shows how to train a multiclass logistic regression model with elastic net regularization, as well as extract the multiclass training summary for evaluating the model. <div class="codetabs"> <div data-lang="python" markdown="1"> {% includeexample python/ml/multiclasslogisticregressionwithelasticnet.py %} </div> <div data-lang="scala" markdown="1"> {% include_example scala/org/apache/spark/examples/ml/MulticlassLogisticRegressionWithElasticNetExample.scala %} </div> <div data-lang="java" markdown="1"> {% include_example java/org/apache/spark/examples/ml/JavaMulticlassLogisticRegressionWithElasticNetExample.java %} </div> <div data-lang="r" markdown="1"> More details on parameters can be found in the . {% include_example multinomial r/ml/logit.R %} </div> </div> Decision trees are a popular family of classification and regression methods. More information about the `spark.ml` implementation can be found further in the . Examples 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 Decision Tree algorithm can recognize. <div class="codetabs"> <div data-lang="python" markdown="1"> More details on parameters can be found in the . {% includeexample python/ml/decisiontreeclassificationexample.py %} </div> <div data-lang="scala" markdown="1"> More details on parameters can be found in the . {% include_example scala/org/apache/spark/examples/ml/DecisionTreeClassificationExample.scala %} </div> <div data-lang="java" markdown="1"> More details on parameters can be found in the . {% include_example java/org/apache/spark/examples/ml/JavaDecisionTreeClassificationExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example classification r/ml/decisionTree.R %} </div> </div> Random forests are a popular family of classification and regression methods. More information about the `spark.ml` implementation can be found further in the . Examples 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. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/randomforestclassifierexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/RandomForestClassifierExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaRandomForestClassifierExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example classification r/ml/randomForest.R %} </div> </div> Gradient-boosted trees (GBTs) are a popular classification and regression method using ensembles of decision trees. More information about the `spark.ml` implementation can be found further in the

Examples 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. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/gradientboostedtreeclassifier_example.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/GradientBoostedTreeClassifierExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaGradientBoostedTreeClassifierExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example classification r/ml/gbt.R %} </div> </div> Multilayer perceptron classifier (MLPC) is a classifier based on the . MLPC consists of multiple layers of nodes. Each layer is fully connected to the next layer in the network. Nodes in the input layer represent the input data. All other nodes map inputs to outputs by a linear combination of the inputs with the node's weights `$\wv$` and bias `$\bv$` and applying an activation function. This can be written in matrix form for MLPC with `$K+1$` layers as follows: `\[ \mathrm{y}(\x) = \mathrm{fK}(...\mathrm{f2}(\wv2^T\mathrm{f1}(\wv1^T \x+b1)+b2)...+bK) \]` Nodes in intermediate layers use sigmoid (logistic) function: `\[ \mathrm{f}(zi) = \frac{1}{1 + e^{-zi}} \]` Nodes in the output layer use softmax function: `\[ \mathrm{f}(zi) = \frac{e^{zi}}{\sum{k=1}^N e^{zk}} \]` The number of nodes `$N$` in the output layer corresponds to the number of classes. MLPC employs backpropagation for learning the model. We use the logistic loss function for optimization and L-BFGS as an optimization routine. Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/multilayerperceptron_classification.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/MultilayerPerceptronClassifierExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaMultilayerPerceptronClassifierExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example r/ml/mlp.R %} </div> </div> A constructs a hyperplane or set of hyperplanes in a high- or infinite-dimensional space, which can be used for classification, regression, or other tasks. Intuitively, a good separation is achieved by the hyperplane that has the largest distance to the nearest training-data points of any class (so-called functional margin), since in general the larger the margin the lower the generalization error of the classifier. LinearSVC in Spark ML supports binary classification with linear SVM. Internally, it optimizes the using OWLQN optimizer. Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% include_example python/ml/linearsvc.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/LinearSVCExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaLinearSVCExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example r/ml/svmLinear.R %} </div> </div> 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. Examples The example below demonstrates how to load the , parse it as a DataFrame and perform multiclass classification using

The test error is calculated to measure the algorithm accuracy. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/onevsrestexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/OneVsRestExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaOneVsRestExample.java %} </div> </div> are a family of simple probabilistic, multiclass classifiers based on applying Bayes' theorem with strong (naive) independence assumptions between every pair of features. Naive Bayes can be trained very efficiently. With a single pass over the training data, it computes the conditional probability distribution of each feature given each label. For prediction, it applies Bayes' theorem to compute the conditional probability distribution of each label given an observation. MLlib supports , , and . Input data: These Multinomial, Complement and Bernoulli models are typically used for . Within that context, each observation is a document and each feature represents a term. A feature's value is the frequency of the term (in Multinomial or Complement Naive Bayes) or a zero or one indicating whether the term was found in the document (in Bernoulli Naive Bayes). Feature values for Multinomial and Bernoulli models must be non-negative. The model type is selected with an optional parameter "multinomial", "complement", "bernoulli" or "gaussian", with "multinomial" as the default. For document classification, the input feature vectors should usually be sparse vectors. Since the training data is only used once, it is not necessary to cache it. can be used by setting the parameter $\lambda$ (default to $1.0$). Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/naivebayes_example.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/NaiveBayesExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaNaiveBayesExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example r/ml/naiveBayes.R %} </div> </div> For more background and more details about the implementation of factorization machines, refer to the . Examples 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 scale features to be between 0 and 1 to prevent the exploding gradient problem. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/fmclassifier_example.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/FMClassifierExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaFMClassifierExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. Note: At the moment SparkR doesn't support feature scaling. {% include_example r/ml/fmClassifier.R %} </div> </div> The interface for working with linear regression models and model summaries is similar to the logistic regression case. > When fitting LinearRegressionModel without intercept on dataset with constant nonzero column by "l-bfgs" solver, Spark MLlib outputs zero coefficients for constant nonzero columns. This behavior is the same as R glmnet but different from LIBSVM. Examples The following example demonstrates training an elastic net regularized linear regression model and extracting model summary statistics. <div class="codetabs"> <div data-lang="python" markdown="1"> <! TODO: Add python model summaries once implemented --> More details on parameters can be found in the . {% includeexample python/ml/linearregressionwithelastic_net.py %} </div> <div data-lang="scala" markdown="1"> More details on parameters can be found in the . {% include_example scala/org/apache/spark/examples/ml/LinearRegressionWithElasticNetExample.scala %} </div> <div data-lang="java" markdown="1"> More details on parameters can be found in the . {% include_example java/org/apache/spark/examples/ml/JavaLinearRegressionWithElasticNetExample.java %} </div> <div data-lang="r" markdown="1"> More details on parameters can be found in the

{% includeexample r/ml/lmwithelasticnet.R %} </div> </div> Contrasted with linear regression where the output is assumed to follow a Gaussian distribution, (GLMs) are specifications of linear models where the response variable $Y_i$ follows some distribution from the . Spark's `GeneralizedLinearRegression` interface allows for flexible specification of GLMs which can be used for various types of prediction problems including linear regression, Poisson regression, logistic regression, and others. Currently in `spark.ml`, only a subset of the exponential family distributions are supported and they are listed . NOTE: Spark currently only supports up to 4096 features through its `GeneralizedLinearRegression` interface, and will throw an exception if this constraint is exceeded. See the for more details. Still, for linear and logistic regression, models with an increased number of features can be trained using the `LinearRegression` and `LogisticRegression` estimators. GLMs require exponential family distributions that can be written in their "canonical" or "natural" form, aka . The form of a natural exponential family distribution is given as: $$ f_Y(y|\theta, \tau) = h(y, \tau)\exp{\left( \frac{\theta \cdot y - A(\theta)}{d(\tau)} \right)} $$ where $\theta$ is the parameter of interest and $\tau$ is a dispersion parameter. In a GLM the response variable $Y_i$ is assumed to be drawn from a natural exponential family distribution: $$ Yi \sim f\left(\cdot|\thetai, \tau \right) $$ where the parameter of interest $\thetai$ is related to the expected value of the response variable $\mui$ by $$ \mui = A'(\thetai) $$ Here, $A'(\theta_i)$ is defined by the form of the distribution selected. GLMs also allow specification of a link function, which defines the relationship between the expected value of the response variable $\mu_i$ and the so called linear predictor $\eta_i$: $$ g(\mui) = \etai = \vec{x_i}^T \cdot \vec{\beta} $$ Often, the link function is chosen such that $A' = g^{-1}$, which yields a simplified relationship between the parameter of interest $\theta$ and the linear predictor $\eta$. In this case, the link function $g(\mu)$ is said to be the "canonical" link function. $$ \thetai = A'^{-1}(\mui) = g(g^{-1}(\etai)) = \etai $$ A GLM finds the regression coefficients $\vec{\beta}$ which maximize the likelihood function. $$ \max_{\vec{\beta}} \mathcal{L}(\vec{\theta}|\vec{y},X) = \prod{i=1}^{N} h(yi, \tau) \exp{\left(\frac{yi\thetai - A(\theta_i)}{d(\tau)}\right)} $$ where the parameter of interest $\theta_i$ is related to the regression coefficients $\vec{\beta}$ by $$ \thetai = A'^{-1}(g^{-1}(\vec{xi} \cdot \vec{\beta})) $$ Spark's generalized linear regression interface also provides summary statistics for diagnosing the fit of GLM models, including residuals, p-values, deviances, the Akaike information criterion, and others. for a more comprehensive review of GLMs and their applications. <table> <thead> <tr> <th>Family</th> <th>Response Type</th> <th>Supported Links</th></tr> </thead> <tbody> <tr> <td>Gaussian</td> <td>Continuous</td> <td>Identity*, Log, Inverse</td> </tr> <tr> <td>Binomial</td> <td>Binary</td> <td>Logit*, Probit, CLogLog</td> </tr> <tr> <td>Poisson</td> <td>Count</td> <td>Log*, Identity, Sqrt</td> </tr> <tr> <td>Gamma</td> <td>Continuous</td> <td>Inverse*, Identity, Log</td> </tr> <tr> <td>Tweedie</td> <td>Zero-inflated continuous</td> <td>Power link function</td> </tr> <tfoot><tr><td colspan="4">* Canonical Link</td></tr></tfoot> </tbody> </table> Examples The following example demonstrates training a GLM with a Gaussian response and identity link function and extracting model summary statistics. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/generalizedlinearregressionexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/GeneralizedLinearRegressionExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaGeneralizedLinearRegressionExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example r/ml/glm.R %} </div> </div> Decision trees are a popular family of classification and regression methods. More information about the `spark.ml` implementation can be found further in the

Examples 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 Decision Tree algorithm can recognize. <div class="codetabs"> <div data-lang="python" markdown="1"> More details on parameters can be found in the . {% includeexample python/ml/decisiontreeregressionexample.py %} </div> <div data-lang="scala" markdown="1"> More details on parameters can be found in the . {% include_example scala/org/apache/spark/examples/ml/DecisionTreeRegressionExample.scala %} </div> <div data-lang="java" markdown="1"> More details on parameters can be found in the . {% include_example java/org/apache/spark/examples/ml/JavaDecisionTreeRegressionExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example regression r/ml/decisionTree.R %} </div> </div> Random forests are a popular family of classification and regression methods. More information about the `spark.ml` implementation can be found further in the . Examples 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. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/randomforestregressorexample.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/RandomForestRegressorExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaRandomForestRegressorExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example regression r/ml/randomForest.R %} </div> </div> Gradient-boosted trees (GBTs) are a popular regression method using ensembles of decision trees. More information about the `spark.ml` implementation can be found further in the . Examples Note: For this example dataset, `GBTRegressor` actually only needs 1 iteration, but that will not be true in general. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/gradientboostedtreeregressor_example.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/GradientBoostedTreeRegressorExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaGradientBoostedTreeRegressorExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example regression r/ml/gbt.R %} </div> </div> In `spark.ml`, we implement the model which is a parametric survival regression model for censored data. It describes a model for the log of survival time, so it's often called a log-linear model for survival analysis. Different from a model designed for the same purpose, the AFT model is easier to parallelize because each instance contributes to the objective function independently. Given the values of the covariates $x^{'}$, for random lifetime $t_{i}$ of subjects i = 1, ..., n, with possible right-censoring, the likelihood function under the AFT model is given as: `\[ L(\beta,\sigma)=\prod{i=1}^n[\frac{1}{\sigma}f{0}(\frac{\log{t{i}}-x^{'}\beta}{\sigma})]^{\delta{i}}S{0}(\frac{\log{t{i}}-x^{'}\beta}{\sigma})^{1-\delta_{i}} \]` Where $\delta_{i}$ is the indicator of the event has occurred i.e. uncensored or not. Using $\epsilon{i}=\frac{\log{t{i}}-x^{'}\beta}{\sigma}$, the log-likelihood function assumes the form: `\[ \iota(\beta,\sigma)=\sum{i=1}^{n}[-\delta{i}\log\sigma+\delta{i}\log{f{0}}(\epsilon{i})+(1-\delta{i})\log{S{0}(\epsilon{i})}] \]` Where $S{0}(\epsilon{i})$ is the baseline survivor function, and $f{0}(\epsilon{i})$ is the corresponding density function. The most commonly used AFT model is based on the Weibull distribution of the survival time. The Weibull distribution for lifetime corresponds to the extreme value distribution for the log of the lifetime, and the $S_{0}(\epsilon)$ function is: `\[ S{0}(\epsilon{i})=\exp(-e^{\epsilon_{i}}) \]` the $f{0}(\epsilon{i})$ function is: `\[ f{0}(\epsilon{i})=e^{\epsilon{i}}\exp(-e^{\epsilon{i}}) \]` The log-likelihood function for AFT model with a Weibull distribution of lifetime is: `\[ \iota(\beta,\sigma)= -\sum{i=1}^n[\delta{i}\log\sigma-\delta{i}\epsilon{i}+e^{\epsilon_{i}}] \]` Due to minimizing the negative log-likelihood equivalent to maximum a posteriori probability, the loss function we use to optimize is $-\iota(\beta,\sigma)$. The gradient functions for $\beta$ and $\log\sigma$ respectively are: `\[ \frac{\partial (-\iota)}{\partial \beta}=\sum{1=1}^{n}[\delta{i}-e^{\epsilon{i}}]\frac{x{i}}{\sigma} \]` `\[ \frac{\partial (-\iota)}{\partial (\log\sigma)}=\sum{i=1}^{n}[\delta{i}+(\delta{i}-e^{\epsilon{i}})\epsilon_{i}] \]` The AFT model can be formulated as a convex optimization problem,

the task of finding a minimizer of a convex function $-\iota(\beta,\sigma)$ that depends on the coefficients vector $\beta$ and the log of scale parameter $\log\sigma$. The optimization algorithm underlying the implementation is L-BFGS. The implementation matches the result from R's survival function > When fitting AFTSurvivalRegressionModel without intercept on dataset with constant nonzero column, Spark MLlib outputs zero coefficients for constant nonzero columns. This behavior is different from R survival::survreg. Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample python/ml/aftsurvival_regression.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/AFTSurvivalRegressionExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaAFTSurvivalRegressionExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. {% include_example r/ml/survreg.R %} </div> </div> belongs to the family of regression algorithms. Formally isotonic regression is a problem where given a finite set of real numbers `$Y = {y1, y2, ..., y_n}$` representing observed responses and `$X = {x1, x2, ..., x_n}$` the unknown response values to be fitted finding a function that minimizes `\begin{equation} f(x) = \sum{i=1}^n wi (yi - xi)^2 \end{equation}` with respect to complete order subject to `$x1\le x2\le ...\le xn$` where `$wi$` are positive weights. The resulting function is called isotonic regression and it is unique. It can be viewed as least squares problem under order restriction. Essentially isotonic regression is a best fitting the original data points. We implement a which uses an approach to . The training input is a DataFrame which contains three columns label, features and weight. Additionally, IsotonicRegression algorithm has one optional parameter called $isotonic$ defaulting to true. This argument specifies if the isotonic regression is isotonic (monotonically increasing) or antitonic (monotonically decreasing). Training returns an IsotonicRegressionModel that can be used to predict labels for both known and unknown features. The result of isotonic regression is treated as piecewise linear function. The rules for prediction therefore are: If the prediction input exactly matches a training feature then associated prediction is returned. In case there are multiple predictions with the same feature then one of them is returned. Which one is undefined (same as java.util.Arrays.binarySearch). If the prediction input is lower or higher than all training features then prediction with lowest or highest feature is returned respectively. In case there are multiple predictions with the same feature then the lowest or highest is returned respectively. If the prediction input falls between two training features then prediction is treated as piecewise linear function and interpolated value is calculated from the predictions of the two closest features. In case there are multiple values with the same feature then the same rules as in previous point are used. Examples <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details on the API. {% includeexample python/ml/isotonicregression_example.py %} </div> <div data-lang="scala" markdown="1"> Refer to the for details on the API. {% include_example scala/org/apache/spark/examples/ml/IsotonicRegressionExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for details on the API. {% include_example java/org/apache/spark/examples/ml/JavaIsotonicRegressionExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details on the API. {% include_example r/ml/isoreg.R %} </div> </div> For more background and more details about the implementation of factorization machines, refer to the . Examples 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 scale features to be between 0 and 1 to prevent the exploding gradient problem. <div class="codetabs"> <div data-lang="python" markdown="1"> Refer to the for more details. {% includeexample

%} </div> <div data-lang="scala" markdown="1"> Refer to the for more details. {% include_example scala/org/apache/spark/examples/ml/FMRegressorExample.scala %} </div> <div data-lang="java" markdown="1"> Refer to the for more details. {% include_example java/org/apache/spark/examples/ml/JavaFMRegressorExample.java %} </div> <div data-lang="r" markdown="1"> Refer to the for more details. Note: At the moment SparkR doesn't support feature scaling. {% include_example r/ml/fmRegressor.R %} </div> </div> We implement popular linear methods such as logistic regression and linear least squares with $L1$ or $L2$ regularization. Refer to for details about implementation and tuning; this information is still relevant. We also include a DataFrame API for [Elastic net](http://en.wikipedia.org/wiki/Elasticnetregularization), a hybrid of $L1$ and $L2$ regularization proposed in [Zou et al, Regularization and variable selection via the elastic net](http://users.stat.umn.edu/~zouxx019/Papers/elasticnet.pdf). Mathematically, it is defined as a convex combination of the $L_1$ and the $L_2$ regularization terms: `\[ \alpha \left( \lambda \|\wv\|1 \right) + (1-\alpha) \left( \frac{\lambda}{2}\|\wv\|2^2 \right) , \alpha \in [0, 1], \lambda \geq 0 \]` By setting $\alpha$ properly, elastic net contains both $L1$ and $L2$ regularization as special cases. For example, if a [linear regression](https://en.wikipedia.org/wiki/Linear_regression) model is trained with the elastic net parameter $\alpha$ set to $1$, it is equivalent to a model. On the other hand, if $\alpha$ is set to $0$, the trained model reduces to a [ridge regression](http://en.wikipedia.org/wiki/Tikhonov_regularization) model. We implement Pipelines API for both linear regression and logistic regression with elastic net regularization. are able to estimate interactions between features even in problems with huge sparsity (like advertising and recommendation system). The `spark.ml` implementation supports factorization machines for binary classification and for regression. Factorization machines formula is: $$ \hat{y} = w0 + \sum\limits^n{i-1} wi xi + \sum\limits^n{i=1} \sum\limits^n{j=i+1} \langle vi, vj \rangle xi xj $$ The first two terms denote intercept and linear term (same as in linear regression), and the last term denotes pairwise interactions term. $$v_i$$ describes the i-th variable with k factors. FM can be used for regression and optimization criterion is mean square error. FM also can be used for binary classification through sigmoid function. The optimization criterion is logistic loss. The pairwise interactions can be reformulated: $$ \sum\limits^n{i=1} \sum\limits^n{j=i+1} \langle vi, vj \rangle xi xj = \frac{1}{2}\sum\limits^k_{f=1} \left(\left( \sum\limits^n{i=1}v{i,f}x_i \right)^2 - \sum\limits^n{i=1}v{i,f}^2x_i^2 \right) $$ This equation has only linear complexity in both k and n - i.e. its computation is in $$O(kn)$$. In general, in order to prevent the exploding gradient problem, it is best to scale continuous features to be between 0 and 1, or bin the continuous features and one-hot encode them. and their ensembles are popular methods for the machine learning tasks of classification and regression. Decision trees are widely used since they are easy to interpret, handle categorical features, extend to the multiclass classification setting, do not require feature scaling, and are able to capture non-linearities and feature interactions. Tree ensemble algorithms such as random forests and boosting are among the top performers for classification and regression tasks. The `spark.ml` implementation supports decision trees for binary and multiclass classification and for regression, using both continuous and categorical features. The implementation partitions data by rows, allowing distributed training with millions or even billions of instances. Users can find more information about the decision tree algorithm in the . The main differences between this API and the are: support for ML Pipelines separation of Decision Trees for classification vs. regression use of DataFrame metadata to distinguish continuous and categorical features The Pipelines API for Decision Trees offers a bit more functionality than the original API. In particular, for classification, users can get the predicted probability of each class

class conditional probabilities); for regression, users can get the biased sample variance of prediction. Ensembles of trees (Random Forests and Gradient-Boosted Trees) are described below in the . 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. <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> </tr> </thead> <tbody> <tr> <td>labelCol</td> <td>Double</td> <td>"label"</td> <td>Label to predict</td> </tr> <tr> <td>featuresCol</td> <td>Vector</td> <td>"features"</td> <td>Feature vector</td> </tr> </tbody> </table> <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> <th align="left">Notes</th> </tr> </thead> <tbody> <tr> <td>predictionCol</td> <td>Double</td> <td>"prediction"</td> <td>Predicted label</td> <td></td> </tr> <tr> <td>rawPredictionCol</td> <td>Vector</td> <td>"rawPrediction"</td> <td>Vector of length # classes, with the counts of training instance labels at the tree node which makes the prediction</td> <td>Classification only</td> </tr> <tr> <td>probabilityCol</td> <td>Vector</td> <td>"probability"</td> <td>Vector of length # classes equal to rawPrediction normalized to a multinomial distribution</td> <td>Classification only</td> </tr> <tr> <td>varianceCol</td> <td>Double</td> <td></td> <td>The biased sample variance of prediction</td> <td>Regression only</td> </tr> </tbody> </table> The DataFrame API supports two major tree ensemble algorithms: and . Both use as their base models. Users can find more information about ensemble algorithms in the . In this section, we demonstrate the DataFrame API for ensembles. The main differences between this API and the are: support for DataFrames and ML Pipelines separation of classification vs. regression use of DataFrame metadata to distinguish continuous and categorical features 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. are ensembles of . Random forests combine many decision trees in order to reduce the risk of overfitting. The `spark.ml` implementation supports random forests for binary and multiclass classification and for regression, using both continuous and categorical features. For more information on the algorithm itself, please see the . 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. <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> </tr> </thead> <tbody> <tr> <td>labelCol</td> <td>Double</td> <td>"label"</td> <td>Label to predict</td> </tr> <tr> <td>featuresCol</td> <td>Vector</td> <td>"features"</td> <td>Feature vector</td> </tr> </tbody> </table> <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> <th align="left">Notes</th> </tr> </thead> <tbody> <tr> <td>predictionCol</td> <td>Double</td> <td>"prediction"</td> <td>Predicted label</td> <td></td> </tr> <tr> <td>rawPredictionCol</td> <td>Vector</td> <td>"rawPrediction"</td> <td>Vector of length # classes, with the counts of training instance labels at the tree node which makes the prediction</td> <td>Classification only</td> </tr> <tr> <td>probabilityCol</td> <td>Vector</td> <td>"probability"</td> <td>Vector of length # classes equal to rawPrediction normalized to a multinomial distribution</td> <td>Classification only</td> </tr> </tbody> </table> are ensembles of . GBTs iteratively train decision trees in order to minimize a loss function. The `spark.ml` implementation supports GBTs for binary classification and for regression, using both continuous and categorical features. For more information on the algorithm itself, please see the . 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. <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> </tr> </thead> <tbody> <tr> <td>labelCol</td> <td>Double</td> <td>"label"</td> <td>Label to predict</td> </tr> <tr> <td>featuresCol</td> <td>Vector</td> <td>"features"</td> <td>Feature vector</td> </tr> </tbody> </table> Note that `GBTClassifier` currently only supports binary labels. <table> <thead> <tr> <th align="left">Param name</th> <th align="left">Type(s)</th> <th align="left">Default</th> <th align="left">Description</th> <th align="left">Notes</th> </tr> </thead> <tbody> <tr> <td>predictionCol</td> <td>Double</td> <td>"prediction"</td> <td>Predicted label</td> <td></td> </tr> </tbody> </table> In the future, `GBTClassifier` will also output columns for `rawPrediction` and