diff options
author | Reynold Xin <rxin@apache.org> | 2015-09-08 23:20:31 +0000 |
---|---|---|
committer | Reynold Xin <rxin@apache.org> | 2015-09-08 23:20:31 +0000 |
commit | 443d7fc272a34a818df4dd589bb251ec1087ae11 (patch) | |
tree | 58b176846888d8824cd113146bd59568ea5354f6 /site/docs/1.5.0/mllib-data-types.html | |
parent | 1037fcd3d980ca1bf8e79ccfecd1f5234545b6ff (diff) | |
download | spark-website-443d7fc272a34a818df4dd589bb251ec1087ae11.tar.gz spark-website-443d7fc272a34a818df4dd589bb251ec1087ae11.tar.bz2 spark-website-443d7fc272a34a818df4dd589bb251ec1087ae11.zip |
Added 1.5.0 docs.
Diffstat (limited to 'site/docs/1.5.0/mllib-data-types.html')
-rw-r--r-- | site/docs/1.5.0/mllib-data-types.html | 860 |
1 files changed, 860 insertions, 0 deletions
diff --git a/site/docs/1.5.0/mllib-data-types.html b/site/docs/1.5.0/mllib-data-types.html new file mode 100644 index 000000000..e029e2890 --- /dev/null +++ b/site/docs/1.5.0/mllib-data-types.html @@ -0,0 +1,860 @@ +<!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.5.0 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.5.0</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.5.0</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">Local vector</a></li> + <li><a href="#labeled-point">Labeled point</a></li> + <li><a href="#local-matrix">Local matrix</a></li> + <li><a href="#distributed-matrix">Distributed matrix</a> <ul> + <li><a href="#rowmatrix">RowMatrix</a></li> + <li><a href="#indexedrowmatrix">IndexedRowMatrix</a></li> + <li><a href="#coordinatematrix">CoordinateMatrix</a></li> + <li><a href="#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 order, and sparse matrices, whose non-zero entry values are stored in the Compressed Sparse +Column (CSC) format in column-major order. For example, the following dense 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 two +implementations: <a href="api/scala/index.html#org.apache.spark.mllib.linalg.DenseMatrix"><code>DenseMatrix</code></a>, +and <a href="api/scala/index.html#org.apache.spark.mllib.linalg.SparseMatrix"><code>SparseMatrix</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. Remember, local matrices in MLlib are stored in column-major order.</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> + +<span class="c1">// Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0))</span> +<span class="k">val</span> <span class="n">sm</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">sparse</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="mi">0</span><span class="o">,</span> <span class="mi">1</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="mi">1</span><span class="o">),</span> <span class="nc">Array</span><span class="o">(</span><span class="mi">9</span><span class="o">,</span> <span class="mi">6</span><span class="o">,</span> <span class="mi">8</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 two +implementations: <a href="api/java/org/apache/spark/mllib/linalg/DenseMatrix.html"><code>DenseMatrix</code></a>, +and <a href="api/java/org/apache/spark/mllib/linalg/SparseMatrix.html"><code>SparseMatrix</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. Remember, local matrices in MLlib are stored in column-major order.</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> + +<span class="c1">// Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0))</span> +<span class="n">Matrix</span> <span class="n">sm</span> <span class="o">=</span> <span class="n">Matrices</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="mi">2</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">1</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="mi">1</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="mi">9</span><span class="o">,</span> <span class="mi">6</span><span class="o">,</span> <span class="mi">8</span><span class="o">});</span></code></pre></div> + + </div> + +<div data-lang="python"> + + <p>The base class of local matrices is +<a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.Matrix"><code>Matrix</code></a>, and we provide two +implementations: <a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.DenseMatrix"><code>DenseMatrix</code></a>, +and <a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.SparseMatrix"><code>SparseMatrix</code></a>. +We recommend using the factory methods implemented +in <a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.Matrices"><code>Matrices</code></a> to create local +matrices. Remember, local matrices in MLlib are stored in column-major order.</p> + + <div class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">org.apache.spark.mllib.linalg.</span><span class="p">{</span><span class="n">Matrix</span><span class="p">,</span> <span class="n">Matrices</span><span class="p">}</span> + +<span class="o">//</span> <span class="n">Create</span> <span class="n">a</span> <span class="n">dense</span> <span class="n">matrix</span> <span class="p">((</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">5.0</span><span class="p">,</span> <span class="mf">6.0</span><span class="p">))</span> +<span class="n">dm2</span> <span class="o">=</span> <span class="n">Matrices</span><span class="o">.</span><span class="n">dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">])</span> + +<span class="o">//</span> <span class="n">Create</span> <span class="n">a</span> <span class="n">sparse</span> <span class="n">matrix</span> <span class="p">((</span><span class="mf">9.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">8.0</span><span class="p">),</span> <span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">6.0</span><span class="p">))</span> +<span class="n">sm</span> <span class="o">=</span> <span class="n">Matrices</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="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</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="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">9</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">])</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 and decompositions. +<a href="https://en.wikipedia.org/wiki/QR_decomposition">QR decomposition</a> is of the form A = QR where Q is an orthogonal matrix and R is an upper triangular matrix. +For <a href="https://en.wikipedia.org/wiki/Singular_value_decomposition">singular value decomposition (SVD)</a> and <a href="https://en.wikipedia.org/wiki/Principal_component_analysis">principal component analysis (PCA)</a>, please refer to <a href="mllib-dimensionality-reduction.html">Dimensionality reduction</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.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> + +<span class="c1">// QR decomposition </span> +<span class="k">val</span> <span class="n">qrResult</span> <span class="k">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">tallSkinnyQR</span><span class="o">(</span><span class="kc">true</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> + +<span class="c1">// QR decomposition </span> +<span class="n">QRDecomposition</span><span class="o"><</span><span class="n">RowMatrix</span><span class="o">,</span> <span class="n">Matrix</span><span class="o">></span> <span class="n">result</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="na">tallSkinnyQR</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span></code></pre></div> + + </div> + +<div data-lang="python"> + + <p>A <a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.distributed.RowMatrix"><code>RowMatrix</code></a> can be +created from an <code>RDD</code> of vectors.</p> + + <div class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">pyspark.mllib.linalg.distributed</span> <span class="kn">import</span> <span class="n">RowMatrix</span> + +<span class="c"># Create an RDD of vectors.</span> +<span class="n">rows</span> <span class="o">=</span> <span class="n">sc</span><span class="o">.</span><span class="n">parallelize</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">]])</span> + +<span class="c"># Create a RowMatrix from an RDD of vectors.</span> +<span class="n">mat</span> <span class="o">=</span> <span class="n">RowMatrix</span><span class="p">(</span><span class="n">rows</span><span class="p">)</span> + +<span class="c"># Get its size.</span> +<span class="n">m</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numRows</span><span class="p">()</span> <span class="c"># 4</span> +<span class="n">n</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numCols</span><span class="p">()</span> <span class="c"># 3</span> + +<span class="c"># Get the rows as an RDD of vectors again.</span> +<span class="n">rowsRDD</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">rows</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 data-lang="python"> + + <p>An <a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.distributed.IndexedRowMatrix"><code>IndexedRowMatrix</code></a> +can be created from an <code>RDD</code> of <code>IndexedRow</code>s, where +<a href="api/python/pyspark.mllib.html#pyspark.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-python" data-lang="python"><span class="kn">from</span> <span class="nn">pyspark.mllib.linalg.distributed</span> <span class="kn">import</span> <span class="n">IndexedRow</span><span class="p">,</span> <span class="n">IndexedRowMatrix</span> + +<span class="c"># Create an RDD of indexed rows.</span> +<span class="c"># - This can be done explicitly with the IndexedRow class:</span> +<span class="n">indexedRows</span> <span class="o">=</span> <span class="n">sc</span><span class="o">.</span><span class="n">parallelize</span><span class="p">([</span><span class="n">IndexedRow</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]),</span> + <span class="n">IndexedRow</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]),</span> + <span class="n">IndexedRow</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]),</span> + <span class="n">IndexedRow</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">])])</span> +<span class="c"># - or by using (long, vector) tuples:</span> +<span class="n">indexedRows</span> <span class="o">=</span> <span class="n">sc</span><span class="o">.</span><span class="n">parallelize</span><span class="p">([(</span><span class="mi">0</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]),</span> + <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">])])</span> + +<span class="c"># Create an IndexedRowMatrix from an RDD of IndexedRows.</span> +<span class="n">mat</span> <span class="o">=</span> <span class="n">IndexedRowMatrix</span><span class="p">(</span><span class="n">indexedRows</span><span class="p">)</span> + +<span class="c"># Get its size.</span> +<span class="n">m</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numRows</span><span class="p">()</span> <span class="c"># 4</span> +<span class="n">n</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numCols</span><span class="p">()</span> <span class="c"># 3</span> + +<span class="c"># Get the rows as an RDD of IndexedRows.</span> +<span class="n">rowsRDD</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">rows</span> + +<span class="c"># Convert to a RowMatrix by dropping the row indices.</span> +<span class="n">rowMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toRowMatrix</span><span class="p">()</span> + +<span class="c"># Convert to a CoordinateMatrix.</span> +<span class="n">coordinateMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toCoordinateMatrix</span><span class="p">()</span> + +<span class="c"># Convert to a BlockMatrix.</span> +<span class="n">blockMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toBlockMatrix</span><span class="p">()</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 data-lang="python"> + + <p>A <a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.distributed.CoordinateMatrix"><code>CoordinateMatrix</code></a> +can be created from an <code>RDD</code> of <code>MatrixEntry</code> entries, where +<a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.distributed.MatrixEntry"><code>MatrixEntry</code></a> is a +wrapper over <code>(long, long, float)</code>. A <code>CoordinateMatrix</code> can be converted to a <code>RowMatrix</code> by +calling <code>toRowMatrix</code>, or to an <code>IndexedRowMatrix</code> with sparse rows by calling <code>toIndexedRowMatrix</code>.</p> + + <div class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">pyspark.mllib.linalg.distributed</span> <span class="kn">import</span> <span class="n">CoordinateMatrix</span><span class="p">,</span> <span class="n">MatrixEntry</span> + +<span class="c"># Create an RDD of coordinate entries.</span> +<span class="c"># - This can be done explicitly with the MatrixEntry class:</span> +<span class="n">entries</span> <span class="o">=</span> <span class="n">sc</span><span class="o">.</span><span class="n">parallelize</span><span class="p">([</span><span class="n">MatrixEntry</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">),</span> <span class="n">MatrixEntry</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">2.1</span><span class="p">),</span> <span class="n">MatrixEntry</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">3.7</span><span class="p">)])</span> +<span class="c"># - or using (long, long, float) tuples:</span> +<span class="n">entries</span> <span class="o">=</span> <span class="n">sc</span><span class="o">.</span><span class="n">parallelize</span><span class="p">([(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">2.1</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">3.7</span><span class="p">)])</span> + +<span class="c"># Create an CoordinateMatrix from an RDD of MatrixEntries.</span> +<span class="n">mat</span> <span class="o">=</span> <span class="n">CoordinateMatrix</span><span class="p">(</span><span class="n">entries</span><span class="p">)</span> + +<span class="c"># Get its size.</span> +<span class="n">m</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numRows</span><span class="p">()</span> <span class="c"># 3</span> +<span class="n">n</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numCols</span><span class="p">()</span> <span class="c"># 2</span> + +<span class="c"># Get the entries as an RDD of MatrixEntries.</span> +<span class="n">entriesRDD</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">entries</span> + +<span class="c"># Convert to a RowMatrix.</span> +<span class="n">rowMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toRowMatrix</span><span class="p">()</span> + +<span class="c"># Convert to an IndexedRowMatrix.</span> +<span class="n">indexedRowMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toIndexedRowMatrix</span><span class="p">()</span> + +<span class="c"># Convert to a BlockMatrix.</span> +<span class="n">blockMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toBlockMatrix</span><span class="p">()</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 data-lang="python"> + + <p>A <a href="api/python/pyspark.mllib.html#pyspark.mllib.linalg.distributed.BlockMatrix"><code>BlockMatrix</code></a> +can be created from an <code>RDD</code> of sub-matrix blocks, where a sub-matrix block is a +<code>((blockRowIndex, blockColIndex), sub-matrix)</code> tuple.</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">Matrices</span> +<span class="kn">from</span> <span class="nn">pyspark.mllib.linalg.distributed</span> <span class="kn">import</span> <span class="n">BlockMatrix</span> + +<span class="c"># Create an RDD of sub-matrix blocks.</span> +<span class="n">blocks</span> <span class="o">=</span> <span class="n">sc</span><span class="o">.</span><span class="n">parallelize</span><span class="p">([((</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="n">Matrices</span><span class="o">.</span><span class="n">dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">])),</span> + <span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="n">Matrices</span><span class="o">.</span><span class="n">dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">]))])</span> + +<span class="c"># Create a BlockMatrix from an RDD of sub-matrix blocks.</span> +<span class="n">mat</span> <span class="o">=</span> <span class="n">BlockMatrix</span><span class="p">(</span><span class="n">blocks</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> + +<span class="c"># Get its size.</span> +<span class="n">m</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numRows</span><span class="p">()</span> <span class="c"># 6</span> +<span class="n">n</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">numCols</span><span class="p">()</span> <span class="c"># 2</span> + +<span class="c"># Get the blocks as an RDD of sub-matrix blocks.</span> +<span class="n">blocksRDD</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">blocks</span> + +<span class="c"># Convert to a LocalMatrix.</span> +<span class="n">localMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toLocalMatrix</span><span class="p">()</span> + +<span class="c"># Convert to an IndexedRowMatrix.</span> +<span class="n">indexedRowMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toIndexedRowMatrix</span><span class="p">()</span> + +<span class="c"># Convert to a CoordinateMatrix.</span> +<span class="n">coordinateMat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">toCoordinateMatrix</span><span class="p">()</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> |