-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.html
1249 lines (1104 loc) · 50.4 KB
/
visualization.html
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Smile - Data Visualization</title>
<meta name="description" content="Statistical Machine Intelligence and Learning Engine">
<!-- prettify js and CSS -->
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?lang=scala&lang=kotlin&lang=clj"></script>
<style>
.prettyprint ol.linenums > li { list-style-type: decimal; }
</style>
<!-- Bootstrap core CSS -->
<link href="css/cerulean.min.css" rel="stylesheet">
<link href="css/custom.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!-- slider -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.carousel.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.transitions.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/owl-carousel/1.3.3/owl.theme.min.css" type="text/css" />
<!-- table of contents auto generator -->
<script src="js/toc.js" type="text/javascript"></script>
<!-- styles for pager and table of contents -->
<link rel="stylesheet" href="css/pager.css" type="text/css" />
<link rel="stylesheet" href="css/toc.css" type="text/css" />
<!-- Vega-Lite Embed -->
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-57GD08QCML"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-57GD08QCML');
</script>
<!-- Sidebar and testimonial-slider -->
<script type="text/javascript">
$(document).ready(function(){
// scroll/follow sidebar
// #sidebar is defined in the content snippet
// This script has to be executed after the snippet loaded.
// $.getScript("js/follow-sidebar.js");
$("#testimonial-slider").owlCarousel({
items: 1,
singleItem: true,
pagination: true,
navigation: false,
loop: true,
autoPlay: 10000,
stopOnHover: true,
transitionStyle: "backSlide",
touchDrag: true
});
});
</script>
</head>
<body>
<div class="container" style="max-width: 1200px;">
<header>
<div class="masthead">
<p class="lead">
<a href="index.html">
<img src="images/smile.jpg" style="height:100px; width:auto; vertical-align: bottom; margin-top: 20px; margin-right: 20px;">
<span class="tagline">Smile — Statistical Machine Intelligence and Learning Engine</span>
</a>
</p>
</div>
<nav class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Overview <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="quickstart.html">Quick Start</a></li>
<li><a href="overview.html">What's Machine Learning</a></li>
<li><a href="data.html">Data Processing</a></li>
<li><a href="visualization.html">Data Visualization</a></li>
<li><a href="vegalite.html">Declarative Visualization</a></li>
<li><a href="gallery.html">Gallery</a></li>
<li><a href="faq.html">FAQ</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Supervised Learning <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="classification.html">Classification</a></li>
<li><a href="regression.html">Regression</a></li>
<li><a href="deep-learning.html">Deep Learning</a></li>
<li><a href="feature.html">Feature Engineering</a></li>
<li><a href="validation.html">Model Validation</a></li>
<li><a href="missing-value-imputation.html">Missing Value Imputation</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Unsupervised Learning <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="clustering.html">Clustering</a></li>
<li><a href="vector-quantization.html">Vector Quantization</a></li>
<li><a href="association-rule.html">Association Rule Mining</a></li>
<li><a href="mds.html">Multi-Dimensional Scaling</a></li>
<li><a href="manifold.html">Manifold Learning</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">LLM & NLP <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="llm.html">Large Language Model (LLM)</a></li>
<li><a href="nlp.html">Natural Language Processing (NLP)</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Math <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="linear-algebra.html">Linear Algebra</a></li>
<li><a href="statistics.html">Statistics</a></li>
<li><a href="wavelet.html">Wavelet</a></li>
<li><a href="interpolation.html">Interpolation</a></li>
<li><a href="graph.html">Graph Data Structure</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">API <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="api/java/index.html" target="_blank">Java</a></li>
<li><a href="api/scala/index.html" target="_blank">Scala</a></li>
<li><a href="api/kotlin/index.html" target="_blank">Kotlin</a></li>
<li><a href="api/clojure/index.html" target="_blank">Clojure</a></li>
<li><a href="api/json/index.html" target="_blank">JSON</a></li>
</ul>
</li>
<li><a href="https://mybinder.org/v2/gh/haifengl/smile/notebook?urlpath=lab%2Ftree%2Fshell%2Fsrc%2Funiversal%2Fnotebooks%2Findex.ipynb" target="_blank">Try It Online</a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</nav>
</header>
<div id="content" class="row">
<div class="col-md-3 col-md-push-9 hidden-xs hidden-sm">
<div id="sidebar">
<div class="sidebar-toc" style="margin-bottom: 20px;">
<p class="toc-header">Contents</p>
<div id="toc"></div>
</div>
<div id="search">
<script>
(function() {
var cx = '010264411143030149390:ajvee_ckdzs';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:searchbox-only></gcse:searchbox-only>
</div>
</div>
</div>
<div class="col-md-9 col-md-pull-3">
<h1 id="visualization-top" class="title">Data Visualization</h1>
<p>A picture is worth a thousand words. In machine learning, we usually handle
high-dimensional data, which is impossible to draw on display directly. But
a variety of statistical plots are tremendously valuable for us to grasp
the characteristics of many data points. Smile provides data visualization tools
such as plots and maps for researchers to understand information more easily and quickly.</p>
<h2 id="scatter" class="title">Scatter Plot</h2>
<p>A scatter plot displays data as a collection of points. The points can be color-coded,
which is very useful for classification tasks.
The user can use <code>plot</code> functions to draw scatter plot easily.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_1" data-toggle="tab">Java</a></li>
<li><a href="#scala_1" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_1">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def plot(x: Array[Array[Double]], mark: Char = '*', color: Color = Color.BLACK): Canvas
def plot(x: Array[Array[Double]], y: Array[String], mark: Char): Canvas
def plot(x: Array[Array[Double]], y: Array[Int], mark: Char): Canvas
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_1">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public class ScatterPlot {
public static ScatterPlot of(double[][] points, char mark, Color color);
public static ScatterPlot of(double[][] x, String[] y, char mark);
public static ScatterPlot of(double[][] x, int[] y, char mark);
}
</code></pre>
</div>
</div>
</div>
<p>The legends are as follows.</p>
<ul>
<li> <code>.</code> : dot
<li> <code>+</code> : +
<li> <code>-</code> : -
<li> <code>|</code> : |
<li> <code>*</code> : star
<li> <code>x</code> : x
<li> <code>o</code> : circle
<li> <code>O</code> : large circle
<li> <code>@</code> : solid circle
<li> <code>#</code> : large solid circle
<li> <code>s</code> : square
<li> <code>S</code> : large square
<li> <code>q</code> : solid square
<li> <code>Q</code> : large solid square
</ul>
<p>For any other char, the data point will be drawn as a dot.</p>
<p>The functions return a Canvas, which can be used to control the plot
programmatically. The user can also use the popup context menu by right
mouse click to print, change the title, axis labels, and font, etc. To
display the canvas on desktop, call <code>show(canvas)</code>, which
will render the plot properly with an implicit renderer engine.</p>
<p>For both 2D and 3D plot, the user can zoom in/out by mouse wheel. For 2D plot,
the user can shift the coordinates by moving mouse after double click. The
user can also select an area by mouse for detailed view. For 3D plot, the user
can rotate the view by dragging mouse.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_2" data-toggle="tab">Java</a></li>
<li><a href="#scala_2" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_2">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val iris = read.arff("data/weka/iris.arff")
val canvas = plot(iris, "sepallength", "sepalwidth", "class", '*')
canvas.setAxisLabels("sepallength", "sepalwidth")
show(canvas)
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_2">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
import java.awt.Color;
import smile.io.*;
import smile.plot.swing.*;
import smile.stat.distribution.*;
import smile.math.matrix.*;
var iris = Read.arff("data/weka/iris.arff");
var canvas = ScatterPlot.of(iris, "sepallength", "sepalwidth", "class", '*').canvas();
canvas.setAxisLabels("sepallength", "sepalwidth");
canvas.window();
</code></pre>
</div>
</div>
</div>
<p>In this example, we plot the first two columns of Iris data. We use the class
label for legend and color coding.</p>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/iris2d.png" class="enlarge" style="width: 480px;" />
<div class="caption" style="min-width: 480px;">Iris 2D Scatter Plot</div>
</div>
<p>It is also easy to draw a 3D plot.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_3" data-toggle="tab">Java</a></li>
<li><a href="#scala_3" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_3">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val canvas = plot(iris, "sepallength", "sepalwidth", "petallength", "class", '*')
canvas.setAxisLabels("sepallength", "sepalwidth", "petallength")
show(canvas)
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_3">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
var canvas = ScatterPlot.of(iris, "sepallength", "sepalwidth", "petallength", "class", '*').canvas();
canvas.setAxisLabels("sepallength", "sepalwidth", "petallength");
canvas.window();
</code></pre>
</div>
</div>
</div>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/iris3d.png" class="enlarge" style="width: 480px;" />
<div class="caption" style="min-width: 480px;">Iris 3D Scatter Plot</div>
</div>
<p>However, the Iris data has four attributes. So even 3D plot is not sufficient to see the
whole picture. A general practice is plot all the attribute pairs. For example,</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_4" data-toggle="tab">Java</a></li>
<li><a href="#scala_4" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_4">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
show(plot(iris, "class", '*'))
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_4">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
var canvas = PlotGrid.splom(iris, '*', "class");
canvas.window();
</code></pre>
</div>
</div>
</div>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/iris.png" class="enlarge" style="width: 480px;" />
<div class="caption" style="min-width: 480px;">Iris Plot of All Attribute Pairs</div>
</div>
<h2 id="line" class="title">Line Chart</h2>
<p>A line chart connects points by straight lines.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_5" data-toggle="tab">Java</a></li>
<li><a href="#scala_5" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_5">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def line(data: Array[Array[Double]], style: Line.Style = Line.Style.SOLID, color: Color = Color.BLACK, mark: Char = ' ', label: String = null): Canvas
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_5">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public class LinePlot {
public static LinePlot of(double[][] data, Line.Style style, Color color);
public static LinePlot of(double[] y, Line.Style style, Color color);
}
</code></pre>
</div>
</div>
</div>
<p>Let's draw a heart with it!</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_6" data-toggle="tab">Java</a></li>
<li><a href="#scala_6" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_6">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val heart = -314 to 314 map { i =>
val t = i / 100.0
val x = 16 * pow(sin(t), 3)
val y = 13 * cos(t) - 5 * cos(2*t) - 2 * cos(3*t) - cos(4*t)
Array(x, y)
}
show(line(heart.toArray, color = RED))
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_6">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
import static java.lang.Math.*;
double[][] heart = new double[200][2];
for (int i = 0; i < 200; i++) {
double t = PI * (i - 100) / 100;
heart[i][0] = 16 * pow(sin(t), 3);
heart[i][1] = 13 * cos(t) - 5 * cos(2*t) - 2 * cos(3*t) - cos(4*t);
}
var canvas = LinePlot.of(heart, Color.RED).canvas();
canvas.window();
</code></pre>
</div>
</div>
</div>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/heart.png" class="enlarge" style="width: 480px;" />
</div>
<h2 id="box" class="title">Box Plot</h2>
<p>The box plot is a standardized way of displaying the distribution of data
based on the five number summary: minimum, first quartile, median,
third quartile, and maximum.</p>
<p>Box plots can be useful to display differences between populations without
making any assumptions of the underlying statistical distribution: they are
non-parametric. The spacings between the different parts of the box help
indicate the degree of dispersion (spread) and skewness in the data, and
identify outliers.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_7" data-toggle="tab">Java</a></li>
<li><a href="#scala_7" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_7">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def boxplot(data: Array[Double]*): Canvas
def boxplot(data: Array[Array[Double]], labels: Array[String]): Canvas
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_7">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public class BoxPlot {
public BoxPlot(double[][] data, String[] labels);
public static BoxPlot of(double[]... data);
}
</code></pre>
</div>
</div>
</div>
<p>Note that the parameter <code>data</code> is a matrix of which each row to
create a box plot.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_8" data-toggle="tab">Java</a></li>
<li><a href="#scala_8" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_8">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val groups = (iris("sepallength").toDoubleArray zip iris("class").toStringArray).groupBy(_._2)
val labels = groups.keys.toArray
val data = groups.values.map { a => a.map(_._1) }.toArray
val canvas = boxplot(data, labels)
canvas.setAxisLabels("", "sepallength")
show(canvas)
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_8">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
String[] labels = ((smile.data.measure.NominalScale) iris.schema().field("class").measure).levels();
double[][] data = new double[labels.length][];
for (int i = 0; i < data.length; i++) {
var label = labels[i];
data[i] = iris.stream().
filter(row -> row.getString("class").equals(label)).
mapToDouble(row -> row.getFloat("sepallength")).
toArray();
}
var canvas = new BoxPlot(data, labels).canvas();
canvas.setAxisLabels("", "sepallength");
canvas.window();
</code></pre>
</div>
</div>
</div>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/iris-boxplot.png" class="enlarge" style="width: 480px;" />
</div>
<h2 id="historgram" class="title">Histogram</h2>
<p>A histogram is a graphical representation of the distribution of numerical data.
The range of values is divided into a series of consecutive, non-overlapping intervals/bins.
The bins must be adjacent, and are usually equal size.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_9" data-toggle="tab">Java</a></li>
<li><a href="#scala_9" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_9">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def hist(data: Array[Double], k: Int = 10, prob: Boolean = false, color: Color = Color.BLUE): Canvas
def hist(data: Array[Double], breaks: Array[Double], prob: Boolean, color: Color): Canvas
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_9">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public class Histogram {
public static BarPlot of(double[] data);
public static BarPlot of(double[] data, int k, boolean prob);
public static BarPlot of(double[] data, int k, boolean prob, Color color);
public static BarPlot of(double[] data, double[] breaks, boolean prob);
public static BarPlot of(double[] data, double[] breaks, boolean prob, Color color);
}
</code></pre>
</div>
</div>
</div>
<p>where <code>k</code> is the number of bins (10 by default), or you can
also specify an array of the breakpoints between bins.</p>
<p>Let's apply the histogram to an interesting data: the wisdom of crowds.
The original experiment took place about a hundred years ago at a county fair in England.
The fair had a guess the weight of the ox contest. Francis Galton calculated the average
of all guesses, which is right to within one pound.</p>
<p>Recently, NPR Planet Money ran the experiment again. NPR posted a couple of pictures of a cow
(named Penelope) and asked people to guess her weight. They got over 17,000 responses. The average
of guesses was 1,287 pounds, which is pretty close to Penelope's weight 1,355 pounds. </p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_10" data-toggle="tab">Java</a></li>
<li><a href="#scala_10" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_10">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val cow = read.csv("data/stat/cow.txt", header=false)("V1").toDoubleArray
val canvas = hist(cow, 50)
canvas.setAxisLabels("Weight", "Probability")
show(canvas)
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_10">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
var cow = Read.csv("data/stat/cow.txt").column("V1").toDoubleArray();
var canvas = Histogram.of(cow, 50, true).canvas();
canvas.setAxisLabels("Weight", "Probability");
canvas.window();
</code></pre>
</div>
</div>
</div>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/penelope.png" class="enlarge" style="width: 480px;" />
</div>
<p>The histogram gives a rough sense of the distribution of crowd guess, which has a long tail.
Filter out the weights over 3500 pounds, the histogram shows more details.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_11" data-toggle="tab">Java</a></li>
<li><a href="#scala_11" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_11">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val canvas = hist(cow.filter(_ <= 3500), 50)
canvas.setAxisLabels("Weight", "Probability")
show(canvas)
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_11">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
var canvas = Histogram.of(Arrays.stream(cow).filter(w -> w <= 3500).toArray(), 50, true).canvas();
canvas.setAxisLabels("Weight", "Probability");
canvas.window();
</code></pre>
</div>
</div>
</div>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/penelope3500.png" class="enlarge" style="width: 480px;" />
</div>
<p>Smile also supports histograms that display the distribution of 2-dimensional data.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_12" data-toggle="tab">Java</a></li>
<li><a href="#scala_12" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_12">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def hist3(data: Array[Array[Double]], xbins: Int = 10, ybins: Int = 10, prob: Boolean = false, palette: Array[Color] = Palette.jet(16)): Canvas
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_12">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public class Histogram3D {
public static Histogram3D of(double[][] data);
public static Histogram3D of(double[][] data, int nbins, Color[] palette);
public static Histogram3D of(double[][] data, int nbins, boolean prob);
public static Histogram3D of(double[][] data, int nbins, boolean prob, Color[] palette);
}
</code></pre>
</div>
</div>
</div>
<p>Here we generate a data set from a 2-dimensional Gaussian distribution.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_13" data-toggle="tab">Java</a></li>
<li><a href="#scala_13" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_13">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val gauss = new MultivariateGaussianDistribution(Array(0.0, 0.0), Matrix.of(Array(Array(1.0, 0.6), Array(0.6, 2.0))))
val data = (0 until 10000) map { i: Int => gauss.rand }
show(hist3(data.toArray, 50, 50))
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_13">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
double[] mu = {0.0, 0.0};
double[][] v = { {1.0, 0.6}, {0.6, 2.0} };
var gauss = new MultivariateGaussianDistribution(mu, Matrix.of(v));
var data = Stream.generate(gauss::rand).limit(10000).toArray(double[][]::new);
Histogram3D.of(data, 50, false).canvas().window();
</code></pre>
</div>
</div>
</div>
<p>The corresponding histogram looks like</p>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/histogram3d.png" class="enlarge" style="width: 480px;" />
</div>
<h2 id="qq" class="title">Q-Q Plot</h2>
<p>A Q–Q plot ("Q" stands for quantile) is a probability plot for comparing two probability distributions
by plotting their quantiles against each other. A point (x, y) on the plot corresponds to one of
the quantiles of the second distribution (y-coordinate) plotted against the same quantile of
the first distribution (x-coordinate).</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_14" data-toggle="tab">Java</a></li>
<li><a href="#scala_14" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_14">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def qqplot(x: Array[Double]): Canvas
def qqplot(x: Array[Double], d: Distribution): Canvas
def qqplot(x: Array[Double], y: Array[Double]): Canvas
def qqplot(x: Array[Int], d: DiscreteDistribution): Canvas
def qqplot(x: Array[Int], y: Array[Int]): Canvas
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_14">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public class QQPlot {
public static QQPlot of(double[] x);
public static QQPlot of(double[] x, Distribution d);
public static QQPlot of(double[] x, double[] y);
public static QQPlot of(int[] x, DiscreteDistribution d);
public static QQPlot of(int[] x, int[] y);
}
</code></pre>
</div>
</div>
</div>
<p>Smile supports the Q-Q plot of samples to a given distribution and also of two sample sets.
The second distribution/samples is optional. If missing, we assume it the standard Gaussian distribution.</p>
<p>In what follows, we generate a random sample set from standard Gaussian distribution and draw its Q-Q plot.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_15" data-toggle="tab">Java</a></li>
<li><a href="#scala_15" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_15">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val gauss = new GaussianDistribution(0.0, 1.0)
val data = (0 until 1000) map { i: Int => gauss.rand }
show(qqplot(data.toArray))
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_15">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
var gauss = new GaussianDistribution(0.0, 1.0);
var data = DoubleStream.generate(gauss::rand).limit(1000).toArray();
QQPlot.of(data).canvas().window();
</code></pre>
</div>
</div>
</div>
<p>In fact, this is also a good visual way to verify the quality of our random number generator.</p>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/qqplot.png" class="enlarge" style="width: 480px;" />
</div>
<h2 id="heatmap" class="title">Heatmap</h2>
<p>A heat map is a graphical representation of data where the values in a matrix are represented as colors.
In cluster analysis, researchers often employs the heat map by permuting the rows and the columns
of a matrix to place similar values near each other according to the clustering.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_16" data-toggle="tab">Java</a></li>
<li><a href="#scala_16" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_16">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def heatmap(z: Array[Array[Double]], palette: Array[Color] = Palette.jet(16)): Canvas
def heatmap(x: Array[Double], y: Array[Double], z: Array[Array[Double]], palette: Array[Color]): Canvas
def heatmap(rowLabels: Array[String], columnLabels: Array[String], z: Array[Array[Double]], palette: Array[Color]): Canvas
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_16">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public class Heatmap {
public static Heatmap of(double[][] z);
public static Heatmap of(double[][] z, int k);
public static Heatmap of(double[] x, double[] y, double[][] z);
public static Heatmap of(double[] x, double[] y, double[][] z, int k);
public static Heatmap of(String[] rowLabels, String[] columnLabels, double[][] z);
public static Heatmap of(String[] rowLabels, String[] columnLabels, double[][] z, int k);
}
</code></pre>
</div>
</div>
</div>
<p>where <code>z</code> is the matrix to display and the optional parameters <code>x</code> and <code>y</code>
are the coordinates of data matrix cells, which must be in ascending order. Alternatively, one can also
provide labels as the coordinates, which is a common practice in cluster analysis.</p>
<p>In what follows, we display the heat map of a matrix. We start with a small <code>4 x 4</code> matrix and
enlarge it with bicubic interpolation. We also use the helper class <code>Palette</code> to generate the color
scheme. This class provides many other color schemes.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_17" data-toggle="tab">Java</a></li>
<li><a href="#scala_17" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_17">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
// the matrix to display
val z = Array(
Array(1.0, 2.0, 4.0, 1.0),
Array(6.0, 3.0, 5.0, 2.0),
Array(4.0, 2.0, 1.0, 5.0),
Array(5.0, 4.0, 2.0, 3.0)
)
// make the matrix larger with bicubic interpolation
val x = Array(0.0, 1.0, 2.0, 3.0)
val y = Array(0.0, 1.0, 2.0, 3.0)
val bicubic = new BicubicInterpolation(x, y, z)
val Z = Array.ofDim[Double](101, 101)
for (i <- 0 to 100) {
for (j <- 0 to 100)
Z(i)(j) = bicubic.interpolate(i * 0.03, j * 0.03)
}
show(heatmap(Z, Palette.jet(256)))
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_17">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
// the matrix to display
double[][] z = {
{1.0, 2.0, 4.0, 1.0},
{6.0, 3.0, 5.0, 2.0},
{4.0, 2.0, 1.0, 5.0},
{5.0, 4.0, 2.0, 3.0}
};
// make the matrix larger with bicubic interpolation
double[] x = {0.0, 1.0, 2.0, 3.0};
double[] y = {0.0, 1.0, 2.0, 3.0};
var bicubic = new BicubicInterpolation(x, y, z);
var Z = new double[101][101];
for (int i = 0; i <= 100; i++) {
for (int j = 0; j <= 100; j++)
Z[i][j] = bicubic.interpolate(i * 0.03, j * 0.03);
}
Heatmap.of(Z, Palette.jet(256)).canvas().window();
</code></pre>
</div>
</div>
</div>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/heatmap.png" class="enlarge" style="width: 480px;" />
</div>
<p>A special case of heat map is to draw the sparsity pattern of a matrix.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_18" data-toggle="tab">Java</a></li>
<li><a href="#scala_18" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_18">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def spy(matrix: SparseMatrix, k: Int = 1): Canvas
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_18">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public class SparseMatrixPlot {
public static SparseMatrixPlot of(SparseMatrix sparse);
public static SparseMatrixPlot of(SparseMatrix sparse, int k);
}
</code></pre>
</div>
</div>
</div>
<p>The structure of sparse matrix is critical in solving linear systems.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_19" data-toggle="tab">Java</a></li>
<li><a href="#scala_19" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_19">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val sparse = SparseMatrix.text(java.nio.file.Paths.get("data/matrix/mesh2em5.txt"))
val canvas = spy(sparse)
canvas.setTitle("mesh2em5")
show(canvas)
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_19">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
var sparse = SparseMatrix.text(java.nio.file.Paths.get("data/matrix/mesh2em5.txt"));
var canvas = SparseMatrixPlot.of(sparse).canvas();
canvas.setTitle("mesh2em5");
canvas.window();
</code></pre>
</div>
</div>
</div>
<div style="width: 100%; display: inline-block; text-align: center;">
<img src="images/mesh2em5.png" class="enlarge" style="width: 480px;" />
</div>
<p>Another variant is the hex map where hexagon cells replace rectangle cells.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_20" data-toggle="tab">Java</a></li>
<li><a href="#scala_20" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_20">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
def hexmap(z: Array[Array[Double]], palette: Array[Color] = Palette.jet(16)): Canvas
</code></pre>
</div>
</div>
<div class="tab-pane active" id="java_20">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-java"><code>
public class Hexmap {
public static Hexmap of(double[][] z);
public static Hexmap of(double[][] z, int k);
public static Hexmap of(double[][] z, Color[] palette);
} </code></pre>
</div>
</div>
</div>
<p>In machine learning, the hex map is often used to visualize self-organized map (SOM).
An SOM is a type of artificial neural network that is trained using unsupervised learning to
produce a low-dimensional (typically two-dimensional), discretized representation of
the input space of the training samples. An SOM consists of components called nodes or neurons.
Associated with each node are a weight vector of the same dimension as the input data vectors,
and a position in the map space. The U-Matrix value of a particular node is the average distance
between the node's weight vector and that of its closest neighbors. In practice, researchers
often use the hex map to visualize the U-Matrix.</p>
<p>In the following example, we train and visualize a SOM on the USPS training data set with <code>30 x 30</code>
nodes.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#java_21" data-toggle="tab">Java</a></li>
<li><a href="#scala_21" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_21">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code>
val zip = read.csv("data/usps/zip.train", delimiter = ' ', header = false)
val x = zip.drop(0).toArray
val lattice = SOM.lattice(30, 30, x)
val som = new SOM(lattice,
TimeFunction.constant(0.1),
Neighborhood.Gaussian(1, x.length * 10 / 4))
for (i <- 0 until 10) {
MathEx.permutate(x.length).foreach { j =>