<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Data Types - MLlib - Spark 1.4.1 Documentation</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="css/main.css">
<script src="js/vendor/modernizr-2.6.1-respond-1.1.0.min.js"></script>
<link rel="stylesheet" href="css/pygments-default.css">
<!-- Google analytics script -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-32518208-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an outdated browser. <a href="http://browsehappy.com/">Upgrade your browser today</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to better experience this site.</p>
<![endif]-->
<!-- This code is taken from http://twitter.github.com/bootstrap/examples/hero.html -->
<div class="navbar navbar-fixed-top" id="topbar">
<div class="navbar-inner">
<div class="container">
<div class="brand"><a href="index.html">
<img src="img/spark-logo-hd.png" style="height:50px;"/></a><span class="version">1.4.1</span>
</div>
<ul class="nav">
<!--TODO(andyk): Add class="active" attribute to li some how.-->
<li><a href="index.html">Overview</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Programming Guides<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="quick-start.html">Quick Start</a></li>
<li><a href="programming-guide.html">Spark Programming Guide</a></li>
<li class="divider"></li>
<li><a href="streaming-programming-guide.html">Spark Streaming</a></li>
<li><a href="sql-programming-guide.html">DataFrames and SQL</a></li>
<li><a href="mllib-guide.html">MLlib (Machine Learning)</a></li>
<li><a href="graphx-programming-guide.html">GraphX (Graph Processing)</a></li>
<li><a href="bagel-programming-guide.html">Bagel (Pregel on Spark)</a></li>
<li><a href="sparkr.html">SparkR (R on Spark)</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">API Docs<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="api/scala/index.html#org.apache.spark.package">Scala</a></li>
<li><a href="api/java/index.html">Java</a></li>
<li><a href="api/python/index.html">Python</a></li>
<li><a href="api/R/index.html">R</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Deploying<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="cluster-overview.html">Overview</a></li>
<li><a href="submitting-applications.html">Submitting Applications</a></li>
<li class="divider"></li>
<li><a href="spark-standalone.html">Spark Standalone</a></li>
<li><a href="running-on-mesos.html">Mesos</a></li>
<li><a href="running-on-yarn.html">YARN</a></li>
<li class="divider"></li>
<li><a href="ec2-scripts.html">Amazon EC2</a></li>
</ul>
</li>
<li class="dropdown">
<a href="api.html" class="dropdown-toggle" data-toggle="dropdown">More<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="configuration.html">Configuration</a></li>
<li><a href="monitoring.html">Monitoring</a></li>
<li><a href="tuning.html">Tuning Guide</a></li>
<li><a href="job-scheduling.html">Job Scheduling</a></li>
<li><a href="security.html">Security</a></li>
<li><a href="hardware-provisioning.html">Hardware Provisioning</a></li>
<li><a href="hadoop-third-party-distributions.html">3<sup>rd</sup>-Party Hadoop Distros</a></li>
<li class="divider"></li>
<li><a href="building-spark.html">Building Spark</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/SPARK/Contributing+to+Spark">Contributing to Spark</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/SPARK/Supplemental+Spark+Projects">Supplemental Projects</a></li>
</ul>
</li>
</ul>
<!--<p class="navbar-text pull-right"><span class="version-text">v1.4.1</span></p>-->
</div>
</div>
</div>
<div class="container" id="content">
<h1 class="title"><a href="mllib-guide.html">MLlib</a> - Data Types</h1>
<ul id="markdown-toc">
<li><a href="#local-vector" id="markdown-toc-local-vector">Local vector</a></li>
<li><a href="#labeled-point" id="markdown-toc-labeled-point">Labeled point</a></li>
<li><a href="#local-matrix" id="markdown-toc-local-matrix">Local matrix</a></li>
<li><a href="#distributed-matrix" id="markdown-toc-distributed-matrix">Distributed matrix</a> <ul>
<li><a href="#rowmatrix" id="markdown-toc-rowmatrix">RowMatrix</a></li>
<li><a href="#indexedrowmatrix" id="markdown-toc-indexedrowmatrix">IndexedRowMatrix</a></li>
<li><a href="#coordinatematrix" id="markdown-toc-coordinatematrix">CoordinateMatrix</a></li>
<li><a href="#blockmatrix" id="markdown-toc-blockmatrix">BlockMatrix</a></li>
</ul>
</li>
</ul>
<p>MLlib supports local vectors and matrices stored on a single machine,
as well as distributed matrices backed by one or more RDDs.
Local vectors and local matrices are simple data models
that serve as public interfaces. The underlying linear algebra operations are provided by
<a href="http://www.scalanlp.org/">Breeze</a> and <a href="http://jblas.org/">jblas</a>.
A training example used in supervised learning is called a “labeled point” in MLlib.</p>
<h2 id="local-vector">Local vector</h2>
<p>A local vector has integer-typed and 0-based indices and double-typed values, stored on a single
machine. MLlib supports two types of local vectors: dense and sparse. A dense vector is backed by
a double array representing its entry values, while a sparse vector is backed by two parallel
arrays: indices and values. For example, a vector <code>(1.0, 0.0, 3.0)</code> can be represented in dense
format as <code>[1.0, 0.0, 3.0]</code> or in sparse format as <code>(3, [0, 2], [1.0, 3.0])</code>, where <code>3</code> is the size
of the vector.</p>
<div class="codetabs">
<div data-lang="scala">
<p>The base class of local vectors is
<a href="api/scala/index.html#org.apache.spark.mllib.linalg.Vector"><code>Vector</code></a>, and we provide two
implementations: <a href="api/scala/index.html#org.apache.spark.mllib.linalg.DenseVector"><code>DenseVector</code></a> and
<a href="api/scala/index.html#org.apache.spark.mllib.linalg.SparseVector"><code>SparseVector</code></a>. We recommend
using the factory methods implemented in
<a href="api/scala/index.html#org.apache.spark.mllib.linalg.Vectors$"><code>Vectors</code></a> to create local vectors.</p>
<div class="highlight"><pre><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">org.apache.spark.mllib.linalg.</span><span class="o">{</span><span class="nc">Vector</span><span class="o">,</span> <span class="nc">Vectors</span><span class="o">}</span>
<span class="c1">// Create a dense vector (1.0, 0.0, 3.0).</span>
<span class="k">val</span> <span class="n">dv</span><span class="k">:</span> <span class="kt">Vector</span> <span class="o">=</span> <span class="nc">Vectors</span><span class="o">.</span><span class="n">dense</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">0.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">)</span>
<span class="c1">// Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries.</span>
<span class="k">val</span> <span class="n">sv1</span><span class="k">:</span> <span class="kt">Vector</span> <span class="o">=</span> <span class="nc">Vectors</span><span class="o">.</span><span class="n">sparse</span><span class="o">(</span><span class="mi">3</span><span class="o">,</span> <span class="nc">Array</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span> <span class="mi">2</span><span class="o">),</span> <span class="nc">Array</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">))</span>
<span class="c1">// Create a sparse vector (1.0, 0.0, 3.0) by specifying its nonzero entries.</span>
<span class="k">val</span> <span class="n">sv2</span><span class="k">:</span> <span class="kt">Vector</span> <span class="o">=</span> <span class="nc">Vectors</span><span class="o">.</span><span class="n">sparse</span><span class="o">(</span><span class="mi">3</span><span class="o">,</span> <span class="nc">Seq</span><span class="o">((</span><span class="mi">0</span><span class="o">,</span> <span class="mf">1.0</span><span class="o">),</span> <span class="o">(</span><span class="mi">2</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">)))</span></code></pre></div>
<p><strong><em>Note:</em></strong>
Scala imports <code>scala.collection.immutable.Vector</code> by default, so you have to import
<code>org.apache.spark.mllib.linalg.Vector</code> explicitly to use MLlib’s <code>Vector</code>.</p>
</div>
<div data-lang="java">
<p>The base class of local vectors is
<a href="api/java/org/apache/spark/mllib/linalg/Vector.html"><code>Vector</code></a>, and we provide two
implementations: <a href="api/java/org/apache/spark/mllib/linalg/DenseVector.html"><code>DenseVector</code></a> and
<a href="api/java/org/apache/spark/mllib/linalg/SparseVector.html"><code>SparseVector</code></a>. We recommend
using the factory methods implemented in
<a href="api/java/org/apache/spark/mllib/linalg/Vectors.html"><code>Vectors</code></a> to create local vectors.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.Vector</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.Vectors</span><span class="o">;</span>
<span class="c1">// Create a dense vector (1.0, 0.0, 3.0).</span>
<span class="n">Vector</span> <span class="n">dv</span> <span class="o">=</span> <span class="n">Vectors</span><span class="o">.</span><span class="na">dense</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">0.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">);</span>
<span class="c1">// Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries.</span>
<span class="n">Vector</span> <span class="n">sv</span> <span class="o">=</span> <span class="n">Vectors</span><span class="o">.</span><span class="na">sparse</span><span class="o">(</span><span class="mi">3</span><span class="o">,</span> <span class="k">new</span> <span class="kt">int</span><span class="o">[]</span> <span class="o">{</span><span class="mi">0</span><span class="o">,</span> <span class="mi">2</span><span class="o">},</span> <span class="k">new</span> <span class="kt">double</span><span class="o">[]</span> <span class="o">{</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">});</span></code></pre></div>
</div>
<div data-lang="python">
<p>MLlib recognizes the following types as dense vectors:</p>
<ul>
<li>NumPy’s <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html"><code>array</code></a></li>
<li>Python’s list, e.g., <code>[1, 2, 3]</code></li>
</ul>
<p>and the following as sparse vectors:</p>
<ul>
<li>MLlib’s <a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.SparseVector"><code>SparseVector</code></a>.</li>
<li>SciPy’s
<a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csc_matrix.html#scipy.sparse.csc_matrix"><code>csc_matrix</code></a>
with a single column</li>
</ul>
<p>We recommend using NumPy arrays over lists for efficiency, and using the factory methods implemented
in <a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.Vectors"><code>Vectors</code></a> to create sparse vectors.</p>
<div class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">scipy.sparse</span> <span class="kn">as</span> <span class="nn">sps</span>
<span class="kn">from</span> <span class="nn">pyspark.mllib.linalg</span> <span class="kn">import</span> <span class="n">Vectors</span>
<span class="c"># Use a NumPy array as a dense vector.</span>
<span class="n">dv1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">])</span>
<span class="c"># Use a Python list as a dense vector.</span>
<span class="n">dv2</span> <span class="o">=</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">]</span>
<span class="c"># Create a SparseVector.</span>
<span class="n">sv1</span> <span class="o">=</span> <span class="n">Vectors</span><span class="o">.</span><span class="n">sparse</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">])</span>
<span class="c"># Use a single-column SciPy csc_matrix as a sparse vector.</span>
<span class="n">sv2</span> <span class="o">=</span> <span class="n">sps</span><span class="o">.</span><span class="n">csc_matrix</span><span class="p">((</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">]),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">]),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">])),</span> <span class="n">shape</span> <span class="o">=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span></code></pre></div>
</div>
</div>
<h2 id="labeled-point">Labeled point</h2>
<p>A labeled point is a local vector, either dense or sparse, associated with a label/response.
In MLlib, labeled points are used in supervised learning algorithms.
We use a double to store a label, so we can use labeled points in both regression and classification.
For binary classification, a label should be either <code>0</code> (negative) or <code>1</code> (positive).
For multiclass classification, labels should be class indices starting from zero: <code>0, 1, 2, ...</code>.</p>
<div class="codetabs">
<div data-lang="scala">
<p>A labeled point is represented by the case class
<a href="api/scala/index.html#org.apache.spark.mllib.regression.LabeledPoint"><code>LabeledPoint</code></a>.</p>
<div class="highlight"><pre><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">org.apache.spark.mllib.linalg.Vectors</span>
<span class="k">import</span> <span class="nn">org.apache.spark.mllib.regression.LabeledPoint</span>
<span class="c1">// Create a labeled point with a positive label and a dense feature vector.</span>
<span class="k">val</span> <span class="n">pos</span> <span class="k">=</span> <span class="nc">LabeledPoint</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="nc">Vectors</span><span class="o">.</span><span class="n">dense</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">0.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">))</span>
<span class="c1">// Create a labeled point with a negative label and a sparse feature vector.</span>
<span class="k">val</span> <span class="n">neg</span> <span class="k">=</span> <span class="nc">LabeledPoint</span><span class="o">(</span><span class="mf">0.0</span><span class="o">,</span> <span class="nc">Vectors</span><span class="o">.</span><span class="n">sparse</span><span class="o">(</span><span class="mi">3</span><span class="o">,</span> <span class="nc">Array</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span> <span class="mi">2</span><span class="o">),</span> <span class="nc">Array</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">)))</span></code></pre></div>
</div>
<div data-lang="java">
<p>A labeled point is represented by
<a href="api/java/org/apache/spark/mllib/regression/LabeledPoint.html"><code>LabeledPoint</code></a>.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.Vectors</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.regression.LabeledPoint</span><span class="o">;</span>
<span class="c1">// Create a labeled point with a positive label and a dense feature vector.</span>
<span class="n">LabeledPoint</span> <span class="n">pos</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">LabeledPoint</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="n">Vectors</span><span class="o">.</span><span class="na">dense</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">0.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">));</span>
<span class="c1">// Create a labeled point with a negative label and a sparse feature vector.</span>
<span class="n">LabeledPoint</span> <span class="n">neg</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">LabeledPoint</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="n">Vectors</span><span class="o">.</span><span class="na">sparse</span><span class="o">(</span><span class="mi">3</span><span class="o">,</span> <span class="k">new</span> <span class="kt">int</span><span class="o">[]</span> <span class="o">{</span><span class="mi">0</span><span class="o">,</span> <span class="mi">2</span><span class="o">},</span> <span class="k">new</span> <span class="kt">double</span><span class="o">[]</span> <span class="o">{</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">}));</span></code></pre></div>
</div>
<div data-lang="python">
<p>A labeled point is represented by
<a href="api/python/pyspark.mllib.html#pyspark.mllib.regression.LabeledPoint"><code>LabeledPoint</code></a>.</p>
<div class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">pyspark.mllib.linalg</span> <span class="kn">import</span> <span class="n">SparseVector</span>
<span class="kn">from</span> <span class="nn">pyspark.mllib.regression</span> <span class="kn">import</span> <span class="n">LabeledPoint</span>
<span class="c"># Create a labeled point with a positive label and a dense feature vector.</span>
<span class="n">pos</span> <span class="o">=</span> <span class="n">LabeledPoint</span><span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">])</span>
<span class="c"># Create a labeled point with a negative label and a sparse feature vector.</span>
<span class="n">neg</span> <span class="o">=</span> <span class="n">LabeledPoint</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">SparseVector</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">]))</span></code></pre></div>
</div>
</div>
<p><strong><em>Sparse data</em></strong></p>
<p>It is very common in practice to have sparse training data. MLlib supports reading training
examples stored in <code>LIBSVM</code> format, which is the default format used by
<a href="http://www.csie.ntu.edu.tw/~cjlin/libsvm/"><code>LIBSVM</code></a> and
<a href="http://www.csie.ntu.edu.tw/~cjlin/liblinear/"><code>LIBLINEAR</code></a>. It is a text format in which each line
represents a labeled sparse feature vector using the following format:</p>
<pre><code>label index1:value1 index2:value2 ...
</code></pre>
<p>where the indices are one-based and in ascending order.
After loading, the feature indices are converted to zero-based.</p>
<div class="codetabs">
<div data-lang="scala">
<p><a href="api/scala/index.html#org.apache.spark.mllib.util.MLUtils$"><code>MLUtils.loadLibSVMFile</code></a> reads training
examples stored in LIBSVM format.</p>
<div class="highlight"><pre><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">org.apache.spark.mllib.regression.LabeledPoint</span>
<span class="k">import</span> <span class="nn">org.apache.spark.mllib.util.MLUtils</span>
<span class="k">import</span> <span class="nn">org.apache.spark.rdd.RDD</span>
<span class="k">val</span> <span class="n">examples</span><span class="k">:</span> <span class="kt">RDD</span><span class="o">[</span><span class="kt">LabeledPoint</span><span class="o">]</span> <span class="k">=</span> <span class="nc">MLUtils</span><span class="o">.</span><span class="n">loadLibSVMFile</span><span class="o">(</span><span class="n">sc</span><span class="o">,</span> <span class="s">"data/mllib/sample_libsvm_data.txt"</span><span class="o">)</span></code></pre></div>
</div>
<div data-lang="java">
<p><a href="api/java/org/apache/spark/mllib/util/MLUtils.html"><code>MLUtils.loadLibSVMFile</code></a> reads training
examples stored in LIBSVM format.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.spark.mllib.regression.LabeledPoint</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.util.MLUtils</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.api.java.JavaRDD</span><span class="o">;</span>
<span class="n">JavaRDD</span><span class="o"><</span><span class="n">LabeledPoint</span><span class="o">></span> <span class="n">examples</span> <span class="o">=</span>
<span class="n">MLUtils</span><span class="o">.</span><span class="na">loadLibSVMFile</span><span class="o">(</span><span class="n">jsc</span><span class="o">.</span><span class="na">sc</span><span class="o">(),</span> <span class="s">"data/mllib/sample_libsvm_data.txt"</span><span class="o">).</span><span class="na">toJavaRDD</span><span class="o">();</span></code></pre></div>
</div>
<div data-lang="python">
<p><a href="api/python/pyspark.mllib.html#pyspark.mllib.util.MLUtils"><code>MLUtils.loadLibSVMFile</code></a> reads training
examples stored in LIBSVM format.</p>
<div class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">pyspark.mllib.util</span> <span class="kn">import</span> <span class="n">MLUtils</span>
<span class="n">examples</span> <span class="o">=</span> <span class="n">MLUtils</span><span class="o">.</span><span class="n">loadLibSVMFile</span><span class="p">(</span><span class="n">sc</span><span class="p">,</span> <span class="s">"data/mllib/sample_libsvm_data.txt"</span><span class="p">)</span></code></pre></div>
</div>
</div>
<h2 id="local-matrix">Local matrix</h2>
<p>A local matrix has integer-typed row and column indices and double-typed values, stored on a single
machine. MLlib supports dense matrices, whose entry values are stored in a single double array in
column major. For example, the following matrix <code>\[ \begin{pmatrix}
1.0 & 2.0 \\
3.0 & 4.0 \\
5.0 & 6.0
\end{pmatrix}
\]</code>
is stored in a one-dimensional array <code>[1.0, 3.0, 5.0, 2.0, 4.0, 6.0]</code> with the matrix size <code>(3, 2)</code>.</p>
<div class="codetabs">
<div data-lang="scala">
<p>The base class of local matrices is
<a href="api/scala/index.html#org.apache.spark.mllib.linalg.Matrix"><code>Matrix</code></a>, and we provide one
implementation: <a href="api/scala/index.html#org.apache.spark.mllib.linalg.DenseMatrix"><code>DenseMatrix</code></a>.
We recommend using the factory methods implemented
in <a href="api/scala/index.html#org.apache.spark.mllib.linalg.Matrices$"><code>Matrices</code></a> to create local
matrices.</p>
<div class="highlight"><pre><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">org.apache.spark.mllib.linalg.</span><span class="o">{</span><span class="nc">Matrix</span><span class="o">,</span> <span class="nc">Matrices</span><span class="o">}</span>
<span class="c1">// Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))</span>
<span class="k">val</span> <span class="n">dm</span><span class="k">:</span> <span class="kt">Matrix</span> <span class="o">=</span> <span class="nc">Matrices</span><span class="o">.</span><span class="n">dense</span><span class="o">(</span><span class="mi">3</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="nc">Array</span><span class="o">(</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">,</span> <span class="mf">5.0</span><span class="o">,</span> <span class="mf">2.0</span><span class="o">,</span> <span class="mf">4.0</span><span class="o">,</span> <span class="mf">6.0</span><span class="o">))</span></code></pre></div>
</div>
<div data-lang="java">
<p>The base class of local matrices is
<a href="api/java/org/apache/spark/mllib/linalg/Matrix.html"><code>Matrix</code></a>, and we provide one
implementation: <a href="api/java/org/apache/spark/mllib/linalg/DenseMatrix.html"><code>DenseMatrix</code></a>.
We recommend using the factory methods implemented
in <a href="api/java/org/apache/spark/mllib/linalg/Matrices.html"><code>Matrices</code></a> to create local
matrices.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.Matrix</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.Matrices</span><span class="o">;</span>
<span class="c1">// Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))</span>
<span class="n">Matrix</span> <span class="n">dm</span> <span class="o">=</span> <span class="n">Matrices</span><span class="o">.</span><span class="na">dense</span><span class="o">(</span><span class="mi">3</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="k">new</span> <span class="kt">double</span><span class="o">[]</span> <span class="o">{</span><span class="mf">1.0</span><span class="o">,</span> <span class="mf">3.0</span><span class="o">,</span> <span class="mf">5.0</span><span class="o">,</span> <span class="mf">2.0</span><span class="o">,</span> <span class="mf">4.0</span><span class="o">,</span> <span class="mf">6.0</span><span class="o">});</span></code></pre></div>
</div>
</div>
<h2 id="distributed-matrix">Distributed matrix</h2>
<p>A distributed matrix has long-typed row and column indices and double-typed values, stored
distributively in one or more RDDs. It is very important to choose the right format to store large
and distributed matrices. Converting a distributed matrix to a different format may require a
global shuffle, which is quite expensive. Three types of distributed matrices have been implemented
so far.</p>
<p>The basic type is called <code>RowMatrix</code>. A <code>RowMatrix</code> is a row-oriented distributed
matrix without meaningful row indices, e.g., a collection of feature vectors.
It is backed by an RDD of its rows, where each row is a local vector.
We assume that the number of columns is not huge for a <code>RowMatrix</code> so that a single
local vector can be reasonably communicated to the driver and can also be stored /
operated on using a single node.
An <code>IndexedRowMatrix</code> is similar to a <code>RowMatrix</code> but with row indices,
which can be used for identifying rows and executing joins.
A <code>CoordinateMatrix</code> is a distributed matrix stored in <a href="https://en.wikipedia.org/wiki/Sparse_matrix#Coordinate_list_.28COO.29">coordinate list (COO)</a> format,
backed by an RDD of its entries.</p>
<p><strong><em>Note</em></strong></p>
<p>The underlying RDDs of a distributed matrix must be deterministic, because we cache the matrix size.
In general the use of non-deterministic RDDs can lead to errors.</p>
<h3 id="rowmatrix">RowMatrix</h3>
<p>A <code>RowMatrix</code> is a row-oriented distributed matrix without meaningful row indices, backed by an RDD
of its rows, where each row is a local vector.
Since each row is represented by a local vector, the number of columns is
limited by the integer range but it should be much smaller in practice.</p>
<div class="codetabs">
<div data-lang="scala">
<p>A <a href="api/scala/index.html#org.apache.spark.mllib.linalg.distributed.RowMatrix"><code>RowMatrix</code></a> can be
created from an <code>RDD[Vector]</code> instance. Then we can compute its column summary statistics.</p>
<div class="highlight"><pre><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">org.apache.spark.mllib.linalg.Vector</span>
<span class="k">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.RowMatrix</span>
<span class="k">val</span> <span class="n">rows</span><span class="k">:</span> <span class="kt">RDD</span><span class="o">[</span><span class="kt">Vector</span><span class="o">]</span> <span class="k">=</span> <span class="o">...</span> <span class="c1">// an RDD of local vectors</span>
<span class="c1">// Create a RowMatrix from an RDD[Vector].</span>
<span class="k">val</span> <span class="n">mat</span><span class="k">:</span> <span class="kt">RowMatrix</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">RowMatrix</span><span class="o">(</span><span class="n">rows</span><span class="o">)</span>
<span class="c1">// Get its size.</span>
<span class="k">val</span> <span class="n">m</span> <span class="k">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numRows</span><span class="o">()</span>
<span class="k">val</span> <span class="n">n</span> <span class="k">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numCols</span><span class="o">()</span></code></pre></div>
</div>
<div data-lang="java">
<p>A <a href="api/java/org/apache/spark/mllib/linalg/distributed/RowMatrix.html"><code>RowMatrix</code></a> can be
created from a <code>JavaRDD<Vector></code> instance. Then we can compute its column summary statistics.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.spark.api.java.JavaRDD</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.Vector</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.RowMatrix</span><span class="o">;</span>
<span class="n">JavaRDD</span><span class="o"><</span><span class="n">Vector</span><span class="o">></span> <span class="n">rows</span> <span class="o">=</span> <span class="o">...</span> <span class="c1">// a JavaRDD of local vectors</span>
<span class="c1">// Create a RowMatrix from an JavaRDD<Vector>.</span>
<span class="n">RowMatrix</span> <span class="n">mat</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">RowMatrix</span><span class="o">(</span><span class="n">rows</span><span class="o">.</span><span class="na">rdd</span><span class="o">());</span>
<span class="c1">// Get its size.</span>
<span class="kt">long</span> <span class="n">m</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="na">numRows</span><span class="o">();</span>
<span class="kt">long</span> <span class="n">n</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="na">numCols</span><span class="o">();</span></code></pre></div>
</div>
</div>
<h3 id="indexedrowmatrix">IndexedRowMatrix</h3>
<p>An <code>IndexedRowMatrix</code> is similar to a <code>RowMatrix</code> but with meaningful row indices. It is backed by
an RDD of indexed rows, so that each row is represented by its index (long-typed) and a local vector.</p>
<div class="codetabs">
<div data-lang="scala">
<p>An
<a href="api/scala/index.html#org.apache.spark.mllib.linalg.distributed.IndexedRowMatrix"><code>IndexedRowMatrix</code></a>
can be created from an <code>RDD[IndexedRow]</code> instance, where
<a href="api/scala/index.html#org.apache.spark.mllib.linalg.distributed.IndexedRow"><code>IndexedRow</code></a> is a
wrapper over <code>(Long, Vector)</code>. An <code>IndexedRowMatrix</code> can be converted to a <code>RowMatrix</code> by dropping
its row indices.</p>
<div class="highlight"><pre><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.</span><span class="o">{</span><span class="nc">IndexedRow</span><span class="o">,</span> <span class="nc">IndexedRowMatrix</span><span class="o">,</span> <span class="nc">RowMatrix</span><span class="o">}</span>
<span class="k">val</span> <span class="n">rows</span><span class="k">:</span> <span class="kt">RDD</span><span class="o">[</span><span class="kt">IndexedRow</span><span class="o">]</span> <span class="k">=</span> <span class="o">...</span> <span class="c1">// an RDD of indexed rows</span>
<span class="c1">// Create an IndexedRowMatrix from an RDD[IndexedRow].</span>
<span class="k">val</span> <span class="n">mat</span><span class="k">:</span> <span class="kt">IndexedRowMatrix</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">IndexedRowMatrix</span><span class="o">(</span><span class="n">rows</span><span class="o">)</span>
<span class="c1">// Get its size.</span>
<span class="k">val</span> <span class="n">m</span> <span class="k">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numRows</span><span class="o">()</span>
<span class="k">val</span> <span class="n">n</span> <span class="k">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numCols</span><span class="o">()</span>
<span class="c1">// Drop its row indices.</span>
<span class="k">val</span> <span class="n">rowMat</span><span class="k">:</span> <span class="kt">RowMatrix</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toRowMatrix</span><span class="o">()</span></code></pre></div>
</div>
<div data-lang="java">
<p>An
<a href="api/java/org/apache/spark/mllib/linalg/distributed/IndexedRowMatrix.html"><code>IndexedRowMatrix</code></a>
can be created from an <code>JavaRDD<IndexedRow></code> instance, where
<a href="api/java/org/apache/spark/mllib/linalg/distributed/IndexedRow.html"><code>IndexedRow</code></a> is a
wrapper over <code>(long, Vector)</code>. An <code>IndexedRowMatrix</code> can be converted to a <code>RowMatrix</code> by dropping
its row indices.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.spark.api.java.JavaRDD</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.IndexedRow</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.IndexedRowMatrix</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.RowMatrix</span><span class="o">;</span>
<span class="n">JavaRDD</span><span class="o"><</span><span class="n">IndexedRow</span><span class="o">></span> <span class="n">rows</span> <span class="o">=</span> <span class="o">...</span> <span class="c1">// a JavaRDD of indexed rows</span>
<span class="c1">// Create an IndexedRowMatrix from a JavaRDD<IndexedRow>.</span>
<span class="n">IndexedRowMatrix</span> <span class="n">mat</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">IndexedRowMatrix</span><span class="o">(</span><span class="n">rows</span><span class="o">.</span><span class="na">rdd</span><span class="o">());</span>
<span class="c1">// Get its size.</span>
<span class="kt">long</span> <span class="n">m</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="na">numRows</span><span class="o">();</span>
<span class="kt">long</span> <span class="n">n</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="na">numCols</span><span class="o">();</span>
<span class="c1">// Drop its row indices.</span>
<span class="n">RowMatrix</span> <span class="n">rowMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="na">toRowMatrix</span><span class="o">();</span></code></pre></div>
</div></div>
<h3 id="coordinatematrix">CoordinateMatrix</h3>
<p>A <code>CoordinateMatrix</code> is a distributed matrix backed by an RDD of its entries. Each entry is a tuple
of <code>(i: Long, j: Long, value: Double)</code>, where <code>i</code> is the row index, <code>j</code> is the column index, and
<code>value</code> is the entry value. A <code>CoordinateMatrix</code> should be used only when both
dimensions of the matrix are huge and the matrix is very sparse.</p>
<div class="codetabs">
<div data-lang="scala">
<p>A
<a href="api/scala/index.html#org.apache.spark.mllib.linalg.distributed.CoordinateMatrix"><code>CoordinateMatrix</code></a>
can be created from an <code>RDD[MatrixEntry]</code> instance, where
<a href="api/scala/index.html#org.apache.spark.mllib.linalg.distributed.MatrixEntry"><code>MatrixEntry</code></a> is a
wrapper over <code>(Long, Long, Double)</code>. A <code>CoordinateMatrix</code> can be converted to an <code>IndexedRowMatrix</code>
with sparse rows by calling <code>toIndexedRowMatrix</code>. Other computations for
<code>CoordinateMatrix</code> are not currently supported.</p>
<div class="highlight"><pre><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.</span><span class="o">{</span><span class="nc">CoordinateMatrix</span><span class="o">,</span> <span class="nc">MatrixEntry</span><span class="o">}</span>
<span class="k">val</span> <span class="n">entries</span><span class="k">:</span> <span class="kt">RDD</span><span class="o">[</span><span class="kt">MatrixEntry</span><span class="o">]</span> <span class="k">=</span> <span class="o">...</span> <span class="c1">// an RDD of matrix entries</span>
<span class="c1">// Create a CoordinateMatrix from an RDD[MatrixEntry].</span>
<span class="k">val</span> <span class="n">mat</span><span class="k">:</span> <span class="kt">CoordinateMatrix</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">CoordinateMatrix</span><span class="o">(</span><span class="n">entries</span><span class="o">)</span>
<span class="c1">// Get its size.</span>
<span class="k">val</span> <span class="n">m</span> <span class="k">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numRows</span><span class="o">()</span>
<span class="k">val</span> <span class="n">n</span> <span class="k">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numCols</span><span class="o">()</span>
<span class="c1">// Convert it to an IndexRowMatrix whose rows are sparse vectors.</span>
<span class="k">val</span> <span class="n">indexedRowMatrix</span> <span class="k">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toIndexedRowMatrix</span><span class="o">()</span></code></pre></div>
</div>
<div data-lang="java">
<p>A
<a href="api/java/org/apache/spark/mllib/linalg/distributed/CoordinateMatrix.html"><code>CoordinateMatrix</code></a>
can be created from a <code>JavaRDD<MatrixEntry></code> instance, where
<a href="api/java/org/apache/spark/mllib/linalg/distributed/MatrixEntry.html"><code>MatrixEntry</code></a> is a
wrapper over <code>(long, long, double)</code>. A <code>CoordinateMatrix</code> can be converted to an <code>IndexedRowMatrix</code>
with sparse rows by calling <code>toIndexedRowMatrix</code>. Other computations for
<code>CoordinateMatrix</code> are not currently supported.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.spark.api.java.JavaRDD</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.CoordinateMatrix</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.IndexedRowMatrix</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.MatrixEntry</span><span class="o">;</span>
<span class="n">JavaRDD</span><span class="o"><</span><span class="n">MatrixEntry</span><span class="o">></span> <span class="n">entries</span> <span class="o">=</span> <span class="o">...</span> <span class="c1">// a JavaRDD of matrix entries</span>
<span class="c1">// Create a CoordinateMatrix from a JavaRDD<MatrixEntry>.</span>
<span class="n">CoordinateMatrix</span> <span class="n">mat</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">CoordinateMatrix</span><span class="o">(</span><span class="n">entries</span><span class="o">.</span><span class="na">rdd</span><span class="o">());</span>
<span class="c1">// Get its size.</span>
<span class="kt">long</span> <span class="n">m</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="na">numRows</span><span class="o">();</span>
<span class="kt">long</span> <span class="n">n</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="na">numCols</span><span class="o">();</span>
<span class="c1">// Convert it to an IndexRowMatrix whose rows are sparse vectors.</span>
<span class="n">IndexedRowMatrix</span> <span class="n">indexedRowMatrix</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="na">toIndexedRowMatrix</span><span class="o">();</span></code></pre></div>
</div>
</div>
<h3 id="blockmatrix">BlockMatrix</h3>
<p>A <code>BlockMatrix</code> is a distributed matrix backed by an RDD of <code>MatrixBlock</code>s, where a <code>MatrixBlock</code> is
a tuple of <code>((Int, Int), Matrix)</code>, where the <code>(Int, Int)</code> is the index of the block, and <code>Matrix</code> is
the sub-matrix at the given index with size <code>rowsPerBlock</code> x <code>colsPerBlock</code>.
<code>BlockMatrix</code> supports methods such as <code>add</code> and <code>multiply</code> with another <code>BlockMatrix</code>.
<code>BlockMatrix</code> also has a helper function <code>validate</code> which can be used to check whether the
<code>BlockMatrix</code> is set up properly.</p>
<div class="codetabs">
<div data-lang="scala">
<p>A <a href="api/scala/index.html#org.apache.spark.mllib.linalg.distributed.BlockMatrix"><code>BlockMatrix</code></a> can be
most easily created from an <code>IndexedRowMatrix</code> or <code>CoordinateMatrix</code> by calling <code>toBlockMatrix</code>.
<code>toBlockMatrix</code> creates blocks of size 1024 x 1024 by default.
Users may change the block size by supplying the values through <code>toBlockMatrix(rowsPerBlock, colsPerBlock)</code>.</p>
<div class="highlight"><pre><code class="language-scala" data-lang="scala"><span class="k">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.</span><span class="o">{</span><span class="nc">BlockMatrix</span><span class="o">,</span> <span class="nc">CoordinateMatrix</span><span class="o">,</span> <span class="nc">MatrixEntry</span><span class="o">}</span>
<span class="k">val</span> <span class="n">entries</span><span class="k">:</span> <span class="kt">RDD</span><span class="o">[</span><span class="kt">MatrixEntry</span><span class="o">]</span> <span class="k">=</span> <span class="o">...</span> <span class="c1">// an RDD of (i, j, v) matrix entries</span>
<span class="c1">// Create a CoordinateMatrix from an RDD[MatrixEntry].</span>
<span class="k">val</span> <span class="n">coordMat</span><span class="k">:</span> <span class="kt">CoordinateMatrix</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">CoordinateMatrix</span><span class="o">(</span><span class="n">entries</span><span class="o">)</span>
<span class="c1">// Transform the CoordinateMatrix to a BlockMatrix</span>
<span class="k">val</span> <span class="n">matA</span><span class="k">:</span> <span class="kt">BlockMatrix</span> <span class="o">=</span> <span class="n">coordMat</span><span class="o">.</span><span class="n">toBlockMatrix</span><span class="o">().</span><span class="n">cache</span><span class="o">()</span>
<span class="c1">// Validate whether the BlockMatrix is set up properly. Throws an Exception when it is not valid.</span>
<span class="c1">// Nothing happens if it is valid.</span>
<span class="n">matA</span><span class="o">.</span><span class="n">validate</span><span class="o">()</span>
<span class="c1">// Calculate A^T A.</span>
<span class="k">val</span> <span class="n">ata</span> <span class="k">=</span> <span class="n">matA</span><span class="o">.</span><span class="n">transpose</span><span class="o">.</span><span class="n">multiply</span><span class="o">(</span><span class="n">matA</span><span class="o">)</span></code></pre></div>
</div>
<div data-lang="java">
<p>A <a href="api/java/org/apache/spark/mllib/linalg/distributed/BlockMatrix.html"><code>BlockMatrix</code></a> can be
most easily created from an <code>IndexedRowMatrix</code> or <code>CoordinateMatrix</code> by calling <code>toBlockMatrix</code>.
<code>toBlockMatrix</code> creates blocks of size 1024 x 1024 by default.
Users may change the block size by supplying the values through <code>toBlockMatrix(rowsPerBlock, colsPerBlock)</code>.</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="kn">import</span> <span class="nn">org.apache.spark.api.java.JavaRDD</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.BlockMatrix</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.CoordinateMatrix</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.distributed.IndexedRowMatrix</span><span class="o">;</span>
<span class="n">JavaRDD</span><span class="o"><</span><span class="n">MatrixEntry</span><span class="o">></span> <span class="n">entries</span> <span class="o">=</span> <span class="o">...</span> <span class="c1">// a JavaRDD of (i, j, v) Matrix Entries</span>
<span class="c1">// Create a CoordinateMatrix from a JavaRDD<MatrixEntry>.</span>
<span class="n">CoordinateMatrix</span> <span class="n">coordMat</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">CoordinateMatrix</span><span class="o">(</span><span class="n">entries</span><span class="o">.</span><span class="na">rdd</span><span class="o">());</span>
<span class="c1">// Transform the CoordinateMatrix to a BlockMatrix</span>
<span class="n">BlockMatrix</span> <span class="n">matA</span> <span class="o">=</span> <span class="n">coordMat</span><span class="o">.</span><span class="na">toBlockMatrix</span><span class="o">().</span><span class="na">cache</span><span class="o">();</span>
<span class="c1">// Validate whether the BlockMatrix is set up properly. Throws an Exception when it is not valid.</span>
<span class="c1">// Nothing happens if it is valid.</span>
<span class="n">matA</span><span class="o">.</span><span class="na">validate</span><span class="o">();</span>
<span class="c1">// Calculate A^T A.</span>
<span class="n">BlockMatrix</span> <span class="n">ata</span> <span class="o">=</span> <span class="n">matA</span><span class="o">.</span><span class="na">transpose</span><span class="o">().</span><span class="na">multiply</span><span class="o">(</span><span class="n">matA</span><span class="o">);</span></code></pre></div>
</div>
</div>
</div> <!-- /container -->
<script src="js/vendor/jquery-1.8.0.min.js"></script>
<script src="js/vendor/bootstrap.min.js"></script>
<script src="js/vendor/anchor.min.js"></script>
<script src="js/main.js"></script>
<!-- MathJax Section -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: { equationNumbers: { autoNumber: "AMS" } }
});
</script>
<script>
// Note that we load MathJax this way to work with local file (file://), HTTP and HTTPS.
// We could use "//cdn.mathjax...", but that won't support "file://".
(function(d, script) {
script = d.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.onload = function(){
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ["$", "$"], ["\\\\(","\\\\)"] ],
displayMath: [ ["$$","$$"], ["\\[", "\\]"] ],
processEscapes: true,
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
}
});
};
script.src = ('https:' == document.location.protocol ? 'https://' : 'http://') +
'cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML';
d.getElementsByTagName('head')[0].appendChild(script);
}(document));
</script>
</body>
</html>