-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickstart.html
1357 lines (1193 loc) · 57.6 KB
/
quickstart.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 - Quick Start</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="quickstart-top" class="title">Quick Start</h1>
<p>Smile is a fast and comprehensive machine learning system.
With advanced data structures and algorithms, Smile delivers the state-of-art performance.
Smile is self-contained and requires only Java standard library.
Since v1.4, Smile may optionally leverage native BLAS/LAPACK library too.
It also provides high-level operators in Scala and an interactive shell.
In practice, data scientists usually build models with high-level tools such as R, Matlab,
SAS, etc. However, developers have to spend a lot of time and energy to incorporate these
models in the production system that are often implemented in general purpose programming
languages such as Java and Scala. With Smile, data scientists and developers can work
in the same environment to build machine learning applications quickly!</p>
<h2 id="download">Download</h2>
<p>Get Smile from the <a href="https://github.com/haifengl/smile/releases">releases page</a> of
the project website. The universal tarball
is also available and can be used on Mac, Linux and Windows.</p>
<p>If you would like to build Smile from source, please first install Java 21, Scala 2.13
and SBT 1.0+. Then clone the repo and build the package:</p>
<pre class="prettyprint lang-sh"><code>
$ git clone https://github.com/haifengl/smile.git
$ cd smile
$ sbt package
</code></pre>
<p>To build with Scala 3, run</p>
<pre class="prettyprint lang-sh"><code>
$ sbt ++3.3.3 scala/package
</code></pre>
<p>To test the latest code, run the following</p>
<pre class="prettyprint lang-sh"><code>
$ git pull
$ bin/smile.sh
</code></pre>
<p>which will build the system and enter the Smile shell in Scala.
If you prefer Java, you may run
</p>
<pre class="prettyprint lang-sh"><code>
$ sbt shell/stage
$ cd shell/target/universal/stage
$ bin/jshell.sh
</code></pre>
<h2 id="shell">Shell</h2>
<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>
<li><a href="#kotlin_1" data-toggle="tab">Kotlin</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="scala_1">
<p>Smile comes with an interactive shell for Scala. In the home directory of Smile, type</p>
<pre class="prettyprint lang-sh"><code>
$ bin/smile
</code></pre>
<p>to enter the shell, which is based on Scala REPL.
If you prefer <a href="https://ammonite.io">Ammonite REPL</a>,
copy its jar to Smile's <code>lib</code> directory. Smile Shell
will switch to Ammonite once restarted.
In the shell, you can run any valid Scala expressions.
Besides, all high-level Smile operators are predefined
in the shell. By default, the shell uses up to 75% memory.
If you need more memory to handle large data, use the option
<code>-J-Xmx</code> or <code>-XX:MaxRAMPercentage</code>.
For example,
</p>
<pre class="prettyprint lang-sh"><code>
$ bin/smile -J-Xmx30G
</code></pre>
<p>You can also modify the configuration file <code>./conf/smile.ini</code>
for the memory and other JVM settings.</p>
<h3 id="basics">Basics</h3>
<p>When the shell starts, we should see something like the following:</p>
<pre class="prettyprint lang-scala"><code>
..::''''::..
.;'' ``;.
.... :: :: :: ::
,;' .;: () ..: :: :: :: ::
::. ..:,:;.,:;. . :: .::::. :: .:' :: :: `:. ::
'''::, :: :: :: `:: :: ;: .:: :: : : ::
,:'; ::; :: :: :: :: :: ::,::''. :: `:. .:' ::
`:,,,,;;' ,;; ,;;, ;;, ,;;, ,;;, `:,,,,:' `;..``::::''..;'
``::,,,,::''
Welcome to Smile Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the Smile Shell
Version 4.0.0, Scala 2.13.14, SBT 1.9.9 built at 2024-07-03 09:52:24.404-0400
===============================================================================
smile>
</code></pre>
<p>The <strong>smile></strong> line is the prompt that the shell is waiting for you to enter expressions.
To get help information of Smile high-level operators,
type <code>help</code>. You can also get detailed information on
each operator by typing <code>help("command")</code>, e.g.
<code>help("svm")</code>. To exit the shell, type <code>exit</code>.
</p>
<p>In the shell, type <code>demo</code> to bring up the demo window,
which shows off various Smile's machine learning capabilities.</p>
<p>You can also type <code>benchmark()</code> to see Smile's performance
on a couple of test data. You can run a particular benchmark by
<code>bencharm("test name")</code>, where test name could be "airline",
"usps", etc.
</p>
<p>On startup, the shell analyzes the classpath and creates a database of every visible package and path.
This is available via tab-completion analogous to the path-completion available in most shells.
If you type a partial path, tab will complete as far as it can and show you your options
if there is more than one.</p>
<pre class="prettyprint lang-scala"><code>
smile> smile.classification.r
randomForest rbfnet rda
</code></pre>
<h3 id="calculator">Calculator</h3>
<p>We can run any valid Scala expressions in the shell. In the
simplest case, you can use it as a calculator.</p>
<pre class="prettyprint lang-scala"><code>
smile> "Hello, World"
res0: String = Hello, World
smile> 2
res1: Int = 2
smile> 2+3
res2: Int = 5
</code></pre>
<p>We can also define variables and reuse them.</p>
<pre class="prettyprint lang-scala"><code>
smile> val x = 2 + 3
x: Int = 5
smile> print(x)
5
smile> val y = 2 * (x + 1)
z: Int = 12
</code></pre>
<p>Functions can be defined too. As Scala is a functional language, functions are
first class citizen, just like other values.</p>
<pre class="prettyprint lang-scala"><code>
smile> def sq(x: Double) = x * x
sq: (x: Double)Double
smile> sq(y)
res4: Double = 441.0
</code></pre>
<p>Scala is a powerful and complicated language that fuses object-oriented programming and functional
programming.
Although you don't need to know all the bells and whistles of Scala to use Smile, we strongly recommend
you to
learn some <a href="https://www.scala-lang.org/documentation/">basics</a>.</p>
<h3 id="script">Script</h3>
<p>We may also run Smile code in a script. The script
<code>examples/iris.sc</code> containing the following Smile code
</p>
<pre class="prettyprint lang-scala"><code>
val data = read.arff(Paths.getTestData("weka/iris.arff"))
println(data)
val formula = "class" ~ "."
val rf = smile.classification.randomForest(formula, data)
println(s"OOB error = %.2f%%" format 100 * rf.error)
</code></pre>
<p>It can be run directly from the shell:</p>
<pre class="prettyprint lang-sh"><code>
$ bin/smile examples/iris.sc
</code></pre>
<p>In this example, we use Fisher's Iris data in the <code>data</code> directory
(including many open data for research purpose). The data
is in Weka's ARFF format. The function <code>read.arff</code> returns an object of
<code>DataFrame</code>. The formula <code>"class" ~ </code> defines that
the column "class" will be used as the class label while the rest columns
are predictors. Finally, we train a random forest
with default parameters and print out its OOB (out of bag) error. We can apply
the model on new data samples with the method <code>predict</code>.
</p>
</div>
<div class="tab-pane active" id="java_1">
<p>Smile provides an integration with JShell, which is available from Java 9+.
In the home directory of Smile, type</p>
<pre class="prettyprint lang-sh"><code>
$ bin/jshell.sh
</code></pre>
<p>to enter the JShell with Smile libraries in the class path.
In the shell, you can run any valid Java expressions.
In the simplest case, you can use it as a calculator.
If you need more memory to handle large data, use the
option <code>-R-Xmx</code>. For example,</p>
<pre class="prettyprint lang-sh"><code>
$ bin/jshell.sh -R-Xmx30G
</code></pre>
<h3 id="basics_java">Basics</h3>
<p>When the shell starts, we should see something like the following:</p>
<pre class="prettyprint lang-java"><code>
..::''''::..
.;'' ``;.
.... :: :: :: ::
,;' .;: () ..: :: :: :: ::
::. ..:,:;.,:;. . :: .::::. :: .:' :: :: `:. ::
'''::, :: :: :: `:: :: ;: .:: :: : : ::
,:'; ::; :: :: :: :: :: ::,::''. :: `:. .:' ::
`:,,,,;;' ,;; ,;;, ;;, ,;;, ,;;, `:,,,,:' `;..``::::''..;'
``::,,,,::''
| Welcome to Smile -- Version 4.0.0
===============================================================================
| Welcome to JShell -- Version 21.0.3
| For an introduction type: /help intro
smile>
</code></pre>
<p>We pre-import Smile's definitions in JShell. To exit the shell, type <code>/exit</code>.</p>
<h3 id="calculator_java">Calculator</h3>
<p>With local variable type inference, it is easy to use JShell as a calculator.</p>
<pre class="prettyprint lang-java"><code>
smile> "Hello, World"
$2 ==> "Hello, World"
smile> 2
$3 ==> 2
smile> 2+3
$4 ==> 5
</code></pre>
<p>We can also define variables and reuse them.</p>
<pre class="prettyprint lang-java"><code>
smile> var x = 2 + 3
x ==> 5
smile> var y = 2 * (x + 1)
y ==> 12
</code></pre>
<h3 id="script_java">Script</h3>
<p>We may also run Smile code in a script. The script
<code>examples/iris.jsh</code> containing the following Smile code
</p>
<pre class="prettyprint lang-java"><code>
import smile.classification.RandomForest;
import smile.data.formula.Formula;
import smile.io.Read;
import smile.util.Paths;
var data = Read.arff(Paths.getTestData("weka/iris.arff"));
System.out.println(data);
var formula = Formula.lhs("class");
var rf = RandomForest.fit(formula, data);
System.out.println(rf.metrics());
</code></pre>
<p>It can be run directly from the shell:</p>
<pre class="prettyprint lang-sh"><code>
$ bin/jshell.sh examples/iris.jsh
</code></pre>
<p>In this example, we use Fisher's Iris data in the <code>data</code> directory
(including many open data for research purpose). The data
is in Weka's ARFF format. The function <code>Read.arff</code> returns an object of
<code>DataFrame</code>. The formula <code>Formula.lhs("class")</code> defines that
the column "class" will be used as the class label while the rest columns
are predictors. Finally, we train a random forest
with default parameters and print out its OOB (out of bag) error. We can apply
the model on new data samples with the method <code>predict</code>.
</p>
</div>
<div class="tab-pane" id="kotlin_1">
<p>Smile provides an integration with Kotlin REPL.
In the home directory of Smile, type</p>
<pre class="prettyprint lang-sh"><code>
$ bin/kshell.sh
</code></pre>
<p>to enter the Kotlin REPL with Smile libraries in the class path.
In the shell, you can run any valid Kotlin expressions.
In the simplest case, you can use it as a calculator.
If you need more memory to handle large data, use the
option <code>-J-Xmx</code>. For example,</p>
<pre class="prettyprint lang-sh"><code>
$ bin/kshell.sh -J-Xmx30G
</code></pre>
<h3 id="basics_kotlin">Basics</h3>
<p>When the shell starts, we should see something like the following:</p>
<pre class="prettyprint lang-kotlin"><code>
Welcome to Kotlin version 2.0.0 (JRE 21.0.3+7-LTS-152)
Type :help for help, :quit for quit
>>>
</code></pre>
<p>To exit the REPL, type <code>:quit</code>. Different from
Smile Shell, we don't pre-import any Smile's definitions in Kotlin REPL.</p>
<h3 id="calculator_kotlin">Calculator</h3>
<p>With local variable type inference, it is easy to use JShell as a calculator.</p>
<pre class="prettyprint lang-kotlin"><code>
>>> "Hello, World"
res0: kotlin.String = Hello, World
>>> 2
res1: kotlin.Int = 2
>>> 2+3
res2: kotlin.Int = 5
</code></pre>
<p>We can also define variables and reuse them.</p>
<pre class="prettyprint lang-kotlin"><code>
>>> var x = 2 + 3
>>> var y = 2 * (x + 1)
>>> y
res13: kotlin.Int = 12
</code></pre>
<h3 id="script_kotlin">Script</h3>
<p>We may also run Smile code in a script. The script
<code>examples/iris.kts</code> containing the following Smile code
</p>
<pre class="prettyprint lang-kotlin"><code>
import smile.*
import smile.classification.*
import smile.data.formula.Formula
import smile.util.Paths
val data = read.arff(Paths.getTestData("weka/iris.arff"))
println(data)
val formula = Formula.lhs("class")
val rf = randomForest(formula, data)
println(rf.metrics())
</code></pre>
<p>It can be run directly from the shell:</p>
<pre class="prettyprint lang-sh"><code>
$ bin/kshell.sh -Xuse-fir-lt=false -script examples/iris.kts
</code></pre>
<p>In this example, we use Fisher's Iris data in the <code>data</code> directory
(including many open data for research purpose). The data
is in Weka's ARFF format. The function <code>Read.arff</code> returns an object of
<code>DataFrame</code>. The formula <code>Formula.lhs("class")</code> defines that
the column "class" will be used as the class label while the rest columns
are predictors. Finally, we train a random forest
with default parameters and print out its OOB (out of bag) error. We can apply
the model on new data samples with the method <code>predict</code>.
</p>
</div>
</div>
<h3 id="cli">Training and Inference CLI</h3>
<p>A secret functionality of Smile Shell is that it can be used for training and
inference through command line (CLI).</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#training_1" data-toggle="tab">Training</a></li>
<li><a href="#predict_1" data-toggle="tab">Batch Inference</a></li>
<li><a href="#serve_1" data-toggle="tab">Online Serving</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="training_1">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-shell"><code style="white-space: preserve nowrap;">
$ bin/smile train
Smile 4.0.0
Usage: smile train [random_forest|gradient_boost|adaboost|cart|logistic|fisher|lda|qda|rda|mlp|svm|rbf|ols|lasso|ridge|elastic_net|gaussian_process] [options]
--formula <class ~ .> The model formula
--data <file> The training data file
--test <file> The optional test data file
--model <file> The model file to save
--format <csv,header=true,delimiter=\t,comment=#,escape=\,quote=">
The data file format
--kfold <value> The k-fold cross validation
--round <value> The number of rounds of repeated cross validation
--ensemble Ensemble cross validation models
--seed <value> The random number generator seed
Command: random_forest [options]
Random Forest
--regression To train a regression model
--trees <value> The number of trees
--mtry <value> The number of features to train node split
--split <GINI, ENTROPY, CLASSIFICATION_ERROR>
The split rule
--max_depth <value> The maximum tree depth
--max_nodes <value> The maximum number of leaf nodes
--node_size <value> The minimum leaf node size
--sampling <value> The sampling rate
--class_weight <value> The class weights
Command: gradient_boost [options]
Gradient Boosting
--regression To train a regression model
--trees <value> The number of trees
--shrinkage <value> The shrinkage parameter in (0, 1] controls the learning rate
--max_depth <value> The maximum tree depth
--max_nodes <value> The maximum number of leaf nodes
--node_size <value> The minimum leaf node size
--sampling <value> The sampling rate
Command: adaboost [options]
AdaBoost
--trees <value> The number of trees
--max_depth <value> The maximum tree depth
--max_nodes <value> The maximum number of leaf nodes
--node_size <value> The minimum leaf node size
Command: cart [options]
Classification and Regression Tree
--regression To train a regression model
--split <GINI, ENTROPY, CLASSIFICATION_ERROR>
The split rule
--max_depth <value> The maximum tree depth
--max_nodes <value> The maximum number of leaf nodes
--node_size <value> The minimum leaf node size
Command: logistic [options]
Logistic Regression
--transform <standardizer, winsor(0.01,0.99), minmax, MaxAbs, L1, L2, Linf>
The feature transformation
--lambda <value> The regularization on linear weights
--iterations <value> The maximum number of iterations
--tolerance <value> The tolerance to stop iterations
Command: fisher [options]
Fisher Linear Discriminant
--transform <standardizer, winsor(0.01,0.99), minmax, MaxAbs, L1, L2, Linf>
The feature transformation
--dimension <value> The dimensionality of mapped space
--tolerance <value> The tolerance if a covariance matrix is singular
Command: lda [options]
Linear Discriminant Analysis
--transform <standardizer, winsor(0.01,0.99), minmax, MaxAbs, L1, L2, Linf>
The feature transformation
--priori <value> The priori probability of each class
--tolerance <value> The tolerance if a covariance matrix is singular
Command: qda [options]
Quadratic Discriminant Analysis
--transform <standardizer, winsor(0.01,0.99), minmax, MaxAbs, L1, L2, Linf>
The feature transformation
--priori <value> The priori probability of each class
--tolerance <value> The tolerance if a covariance matrix is singular
Command: rda [options]
Regularized Discriminant Analysis
--transform <standardizer, winsor(0.01,0.99), minmax, MaxAbs, L1, L2, Linf>
The feature transformation
--alpha <value> The regularization factor in [0, 1] allows a continuum of models between LDA and QDA
--priori <value> The priori probability of each class
--tolerance <value> The tolerance if a covariance matrix is singular
Command: mlp [options]
Multilayer Perceptron
--regression To train a regression model
--transform <standardizer, winsor(0.01,0.99), minmax, MaxAbs, L1, L2, Linf>
The feature transformation
--layers <ReLU(100)|Sigmoid(30)>
The neural network layers
--epochs <value> The number of training epochs
--mini_batch <value> The split rule
--learning_rate <0.01, linear(0.01, 10000, 0.001), piecewise(...), polynomial(...), inverse(...), exp(...)>
The learning rate schedule
--momentum <value> The momentum schedule
--weight_decay <value> The weight decay
--clip_norm <value> The gradient clipping norm
--clip_value <value> The gradient clipping value
--rho <value> RMSProp rho
--epsilon <value> RMSProp epsilon
Command: svm [options]
Support Vector Machine
--transform <standardizer, winsor(0.01,0.99), minmax, MaxAbs, L1, L2, Linf>
The feature transformation
--kernel <value> The kernel function
--C <value> The soft margin penalty parameter
--epsilon <value> The parameter of epsilon-insensitive hinge loss
--ovr One vs Rest strategy for multiclass classification
--ovo One vs One strategy for multiclass classification
--tolerance <value> The tolerance of convergence test
Command: rbf [options]
Radial Basis Function Network
--regression To train a regression model
--transform <standardizer, winsor(0.01,0.99), minmax, MaxAbs, L1, L2, Linf>
The feature transformation
--neurons <value> The number of neurons (radial basis functions)
--normalize Normalized RBF network
Command: ols [options]
Ordinary Least Squares
--method <qr, svd> The fitting method
--stderr Compute the standard errors of the estimate of parameters.
--recursive Recursive least squares
Command: lasso [options]
LASSO - Least Absolute Shrinkage and Selection Operator
--lambda <value> The regularization on linear weights
--iterations <value> The maximum number of iterations
--tolerance <value> The tolerance to stop iterations (relative target duality gap)
Command: ridge [options]
Ridge Regression
--lambda <value> The regularization on linear weights
Command: elastic_net [options]
Elastic Net
--lambda1 <value> The L1 regularization on linear weights
--lambda2 <value> The L2 regularization on linear weights
--iterations <value> The maximum number of iterations
--tolerance <value> The tolerance to stop iterations (relative target duality gap)
Command: gaussian_process [options]
Gaussian Process Regression
--transform <standardizer, winsor(0.01,0.99), minmax, MaxAbs, L1, L2, Linf>
The feature transformation
--kernel <value> The kernel function
--noise <value> The noise variance
--normalize Normalize the response variable
--iterations <value> The maximum number of HPO iterations
--tolerance <value> The stopping tolerance for HPO
</code></pre>
</div>
</div>
<div class="tab-pane" id="predict_1">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-shell"><code>
$ bin/smile predict
Smile 4.0.0
Usage: smile predict [options]
--model <value> The model file
--data <value> The data file
--format <value> The data file format/schema
--probability Output the posteriori probabilities for soft classifier
</code></pre>
</div>
</div>
<div class="tab-pane" id="serve_1">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-shell"><code>
$ bin/smile serve
Smile 4.0.0
Usage: smile serve [options]
--model <value> The model file
--probability Output the posteriori probabilities for soft classifier
</code></pre>
</div>
</div>
</div>
<p>To train a model, one should specify the data file, the output model file,
the machine learning algorithm and its hyperparameters, and the model formula.
Once the training done, it saves the model to the specified path and
also prints the training metrics on the console.
If the optional test data is provided too, the validation metrics will be
computed and displayed too.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#train_2" data-toggle="tab">Training</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="train_2">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-shell"><code style="white-space: preserve nowrap;">
$ bin/smile train random_forest --data data/weka/iris.arff --formula "class ~ ." --model iris_random_forest.model
[main] INFO smile.io.Arff - Read ARFF relation iris
[ForkJoinPool.commonPool-worker-3] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 88.89%
[ForkJoinPool.commonPool-worker-2] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 95.35%
[ForkJoinPool.commonPool-worker-1] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 96.67%
...
[main] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 92.73%
[ForkJoinPool.commonPool-worker-3] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 94.44%
[main] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 92.98%
[ForkJoinPool.commonPool-worker-3] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 92.31%
[main] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 95.00%
[ForkJoinPool.commonPool-worker-3] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 96.30%
[main] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 89.47%
[ForkJoinPool.commonPool-worker-3] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 92.98%
[main] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 92.45%
[ForkJoinPool.commonPool-worker-3] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 96.30%
[main] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 89.83%
[ForkJoinPool.commonPool-worker-3] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 90.57%
[main] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 94.64%
[main] INFO smile.classification.RandomForest - Decision tree OOB accuracy: 97.92%
Training metrics: {
fit time: 191.678 ms,
score time: 17.059 ms,
validation data size: 150,
error: 6,
accuracy: 96.00%,
cross entropy: 0.1316
}
</code></pre>
</div>
</div>
</div>
<p>To run a batch inference on a file, run <code>smile predict</code> command
with the model file and data file path. In this example,
we also specify the optional flag <code>--probability</code> to compute
the posterior probability. If you don't need it, simply skip this option.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#predict_2" data-toggle="tab">Batch Inference</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="predict_2">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-shell"><code>
$ bin/smile predict --model iris_random_forest.model --data data/weka/iris.arff --probability
0 [0.9599005925578108, 0.0205362892965633, 0.01956311814562593]
0 [0.9591845549270528, 0.020962057229598156, 0.01985338784334913]
0 [0.959796981740008, 0.02072034636162096, 0.019482671898371082]
0 [0.959796981740008, 0.02072034636162096, 0.019482671898371082]
0 [0.9598769149423004, 0.020548128104318476, 0.0195749569533811]
0 [0.9583053192555514, 0.021164037082801724, 0.020530643661646933]
0 [0.9598769149423004, 0.020548128104318476, 0.0195749569533811]
0 [0.9598769149423004, 0.020548128104318476, 0.0195749569533811]
0 [0.9591845549270528, 0.020962057229598156, 0.01985338784334913]
0 [0.959796981740008, 0.02072034636162096, 0.019482671898371082]
0 [0.9583053192555514, 0.021164037082801724, 0.020530643661646933]
0 [0.9598769149423004, 0.020548128104318476, 0.0195749569533811]
0 [0.9591845549270528, 0.020962057229598156, 0.01985338784334913]
0 [0.9591845549270528, 0.020962057229598156, 0.01985338784334913]
0 [0.9027507265438325, 0.07089506779135216, 0.026354205664815347]
0 [0.911215084168623, 0.0632270620116508, 0.025557853819726185]
0 [0.9583053192555514, 0.021164037082801724, 0.020530643661646933]
0 [0.9599005925578108, 0.0205362892965633, 0.01956311814562593]
0 [0.911215084168623, 0.0632270620116508, 0.025557853819726185]
...
</code></pre>
</div>
</div>
</div>
<p>It is also easy to create an endpoint to serve online requests.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#serve_2" data-toggle="tab">Online Serving</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="serve_2">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-shell"><code>
$ bin/smile serve --model iris_random_forest.model --probability
[smile-akka.actor.default-dispatcher-4] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
Smile online at http://localhost:8080/v1/infer
Press RETURN to stop...
</code></pre>
</div>
</div>
</div>
<p>The endpoint is at <code>/v1/infer</code>. Here is an example how to make an inference request.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#serve_3" data-toggle="tab">Online Serving</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="serve_3">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-shell"><code>
$ curl -X POST http://localhost:8080/v1/infer -H "Content-Type: application/json" \
-d '{
"sepallength": 5.1,
"sepalwidth": 3.5,
"petallength": 1.4,
"petalwidth": 0.2
}'
[{"class":0,"probability":[0.9599005925578108,0.0205362892965633,0.01956311814562593]}]
</code></pre>
</div>
</div>
</div>
<p>To infer on multiple samples, simply provides JSON array or JSON Lines (JSONL)
in the request body. CSV is also supported.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#serve_4" data-toggle="tab">Online Serving</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="serve_4">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-shell"><code>
$ curl -X POST http://localhost:8080/v1/infer -H "Content-Type: application/json" \
-d '{"sepallength": 5.1, "sepalwidth": 3.5, "petallength": 1.4,"petalwidth": 0.2}
{"sepallength": 6.3, "sepalwidth": 3.3, "petallength": 6.0,"petalwidth": 2.5}'
[{"class":0,"probability":[0.9599005925578108,0.0205362892965633,0.01956311814562593]},
{"class":2,"probability":[0.023727657781681327,0.051035220743102516,0.9252371214752161]}]
</code></pre>
</div>
</div>
</div>
<p>In fact, Smile serving endpoint is an end-to-end streaming API
which applies back pressure throughout the entire stack. It can process the
request body (e.g., a JSON array or CSV stream) on an element-by-element basis,
and render the response immediately without waiting for the rest inference
to complete first. Therefore, it is safe to send very large requests (multi-GB) to
the endpoint!</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#serve_5" data-toggle="tab">Online Serving with Streaming</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="serve_5">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-shell"><code style="white-space: preserve nowrap;">
$ for i in {1..1000}; do tail -n 153 data/weka/iris.arff | head -n 150 >> iris.txt; done
$ cat iris.txt | curl -H "Content-Type: text/csv" -X POST --data-binary @- http://localhost:8080/v1/infer?format=csv
</code></pre>
</div>
</div>
</div>
<p>By default, SmileServe binds at localhost:8080. If you prefer a different port and/or
want to expose the server to other hosts, you may set the binding interface and port
with <code>-J-Dakka.http.server.interface=0.0.0.0</code> and
<code>-J-Dakka.http.server.port=8000</code>, for example.</p>
<h2 id="notebook">Notebooks</h2>
<p>You can also use Smile in your favorite Notebook.
We recommend JupyterLab and provide <code>jupyterlab.sh</code>
to setup the conda environment of Jupyter Lab for Smile with
kernels for Scala and Kotlin. When you run
<code>jupyterlab.sh</code> the first time, it will set up the environment
automatically. You can update the environment with the option
<code>--update</code> later when needed.
</p>
<p>In Scala notebooks, it is helpful to add the following
code to the notebook. We provide many notebook examples in
the <code>notebooks</code> directory.</p>
<ul class="nav nav-tabs">
<li class="active"><a href="#notebook_1" data-toggle="tab">Scala</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="notebook_1">
<div class="code" style="text-align: left;">
<pre class="prettyprint lang-scala"><code style="white-space: preserve nowrap;">
import $ivy.`com.github.haifengl::smile-scala:4.2.0`
import scala.language.postfixOps
import org.apache.commons.csv.CSVFormat
import smile._
import smile.util._
import smile.math._
import smile.math.MathEx.{log2, logistic, factorial, lfactorial, choose, lchoose, random, randomInt, permutate, c, cbind, rbind, sum, mean, median, q1, q3, `var` => variance, sd, mad, min, max, whichMin, whichMax, unique, dot, distance, pdist, KullbackLeiblerDivergence => kld, JensenShannonDivergence => jsd, cov, cor, spearman, kendall, norm, norm1, norm2, normInf, standardize, normalize, scale, unitize, unitize1, unitize2, root}
import smile.math.distance._
import smile.math.kernel._
import smile.math.matrix._
import smile.math.matrix.Matrix._
import smile.math.rbf._
import smile.stat.distribution._
import smile.data._
import smile.data.formula._
import smile.data.measure._
import smile.data.`type`._
import smile.json._
import smile.interpolation._
import smile.validation._
import smile.association._
import smile.base.cart.SplitRule
import smile.base.mlp._
import smile.base.rbf.RBF
import smile.classification._