Machine Learning Library (MLlib) Guide

MLlib is Spark’s machine learning (ML) library. Its goal is to make practical machine learning scalable and easy. At a high level, it provides tools such as:

Announcement: DataFrame-based API is primary API

The MLlib RDD-based API is now in maintenance mode.

As of Spark 2.0, the RDD-based APIs in the spark.mllib package have entered maintenance mode. The primary Machine Learning API for Spark is now the DataFrame-based API in the package.

What are the implications?

Why is MLlib switching to the DataFrame-based API?


MLlib uses the linear algebra package Breeze, which depends on netlib-java for optimised numerical processing. If native libraries1 are not available at runtime, you will see a warning message and a pure JVM implementation will be used instead.

Due to licensing issues with runtime proprietary binaries, we do not include netlib-java’s native proxies by default. To configure netlib-java / Breeze to use system optimised binaries, include com.github.fommil.netlib:all:1.1.2 (or build Spark with -Pnetlib-lgpl) as a dependency of your project and read the netlib-java documentation for your platform’s additional installation instructions.

To use MLlib in Python, you will need NumPy version 1.4 or newer.

Migration guide

MLlib is under active development. The APIs marked Experimental/DeveloperApi may change in future releases, and the migration guide below will explain all changes between releases.

From 1.6 to 2.0

Breaking changes

There were several breaking changes in Spark 2.0, which are outlined below.

Linear algebra classes for DataFrame-based APIs

Spark’s linear algebra dependencies were moved to a new project, mllib-local (see SPARK-13944). As part of this change, the linear algebra classes were copied to a new package, The DataFrame-based APIs in now depend on the classes, leading to a few breaking changes, predominantly in various model classes (see SPARK-14810 for a full list).

Note: the RDD-based APIs in spark.mllib continue to depend on the previous package spark.mllib.linalg.

Converting vectors and matrices

While most pipeline components support backward compatibility for loading, some existing DataFrames and pipelines in Spark versions prior to 2.0, that contain vector or matrix columns, may need to be migrated to the new vector and matrix types. Utilities for converting DataFrame columns from spark.mllib.linalg to types (and vice versa) can be found in spark.mllib.util.MLUtils.

There are also utility methods available for converting single instances of vectors and matrices. Use the asML method on a mllib.linalg.Vector / mllib.linalg.Matrix for converting to ml.linalg types, and mllib.linalg.Vectors.fromML / mllib.linalg.Matrices.fromML for converting to mllib.linalg types.

import org.apache.spark.mllib.util.MLUtils

// convert DataFrame columns
val convertedVecDF = MLUtils.convertVectorColumnsToML(vecDF)
val convertedMatrixDF = MLUtils.convertMatrixColumnsToML(matrixDF)
// convert a single vector or matrix
val mlVec: = mllibVec.asML
val mlMat: = mllibMat.asML

Refer to the MLUtils Scala docs for further detail.

import org.apache.spark.mllib.util.MLUtils;
import org.apache.spark.sql.Dataset;

// convert DataFrame columns
Dataset<Row> convertedVecDF = MLUtils.convertVectorColumnsToML(vecDF);
Dataset<Row> convertedMatrixDF = MLUtils.convertMatrixColumnsToML(matrixDF);
// convert a single vector or matrix mlVec = mllibVec.asML(); mlMat = mllibMat.asML();

Refer to the MLUtils Java docs for further detail.

from pyspark.mllib.util import MLUtils

# convert DataFrame columns
convertedVecDF = MLUtils.convertVectorColumnsToML(vecDF)
convertedMatrixDF = MLUtils.convertMatrixColumnsToML(matrixDF)
# convert a single vector or matrix
mlVec = mllibVec.asML()
mlMat = mllibMat.asML()

Refer to the MLUtils Python docs for further detail.

Deprecated methods removed

Several deprecated methods were removed in the spark.mllib and packages:

A full list of breaking changes can be found at SPARK-14810.

Deprecations and changes of behavior


Deprecations in the spark.mllib and packages include:

Changes of behavior

Changes of behavior in the spark.mllib and packages include:

Previous Spark versions

Earlier migration guides are archived on this page.

  1. To learn more about the benefits and background of system optimised natives, you may wish to watch Sam Halliday’s ScalaX talk on High Performance Linear Algebra in Scala.