1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
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">0.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>
|