-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMLinR_slides_Rladies2021.tex
1805 lines (1375 loc) · 65.3 KB
/
MLinR_slides_Rladies2021.tex
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
\PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
\PassOptionsToPackage{hyphens}{url}
\documentclass[12pt,ignorenonframetext,]{beamer}
\IfFileExists{pgfpages.sty}{\usepackage{pgfpages}}{}
\setbeamertemplate{caption}[numbered]
\setbeamertemplate{caption label separator}{: }
\setbeamercolor{caption name}{fg=normal text.fg}
\beamertemplatenavigationsymbolsempty
\setbeameroption{show notes}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
\usetheme[]{metropolis}
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\newif\ifbibliography
\usepackage[round]{natbib}
\bibliographystyle{plainnat}
\hypersetup{
pdftitle={Machine Learning in R},
pdfauthor={Kasun Bandara},
pdfborder={0 0 0},
breaklinks=true}
%\urlstyle{same} % Use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
% Prevent slide breaks in the middle of a paragraph:
\widowpenalties 1 10000
\raggedbottom
\AtBeginPart{
\let\insertpartnumber\relax
\let\partname\relax
\frame{\partpage}
}
\AtBeginSection{
\ifbibliography
\else
\let\insertsectionnumber\relax
\let\sectionname\relax
\frame{\sectionpage}
\fi
}
\AtBeginSubsection{
\let\insertsubsectionnumber\relax
\let\subsectionname\relax
\frame{\subsectionpage}
}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
\usepackage{subfig}
\title[]{Machine Learning in R}
\subtitle{R-Ladies Colombo}
\author[
Kasun Bandara
]{Kasun Bandara}
\institute[
]{
Melbourne Centre for Data Science, University of Melbourne, Australia.
}
\date[
29 March, 2021
]{
29 March, 2021
}
\begin{document}
% Hide progress bar and footline on titlepage
\begin{frame}[plain]
\titlepage
\end{frame}
\begin{frame}
\begin{figure}
\includegraphics[scale=0.22]{images/kasun}
\end{figure}
\end{frame}
\begin{frame}{About me}
\protect\hypertarget{about-me}{}
\begin{itemize}
\tightlist
\item
2015 Graduated in Computer Science from University of Colombo School
of Computing
\item
2015 Joined WSO2 Inc.~as a Software Engineer
\item
2016-2020 Ph.D.~in Computer Science, Monash University, Australia
\begin{itemize}
\tightlist
\item
Topic: Forecasting In Big Data With Recurrent Neural Networks
\item
Machine Learning for Time Series Forecasting
\item
Research Internship at Walmart Labs, San Francisco, USA
\item
Research Scientist at Turning Point, Melbourne, Australia
\item
Data Science Tutor, Faculty of IT, Monash University
\end{itemize}
\item
2021 Research Fellow, University of Melbourne
\end{itemize}
\end{frame}
\begin{frame}{About me (2)}
\protect\hypertarget{about-me-2}{}
\begin{itemize}
\tightlist
\item
Research Interests
\begin{itemize}
\tightlist
\item
Global Forecasting Models
\item
Hierarchical Forecasting
\item
Retail sales/demand forecasting
\item
Renewable energy production forecasting (solar)
\end{itemize}
\item
Competition Fanatic
\begin{itemize}
\tightlist
\item
M5 Forecasting Competition (\textbf{Gold Medalist})
\item
IEEE CIS Energy Forecasting Competition (\textbf{4th Place})
\item
Air-Liquide Energy Forecasting Competition (\textbf{4th Place})
\item
ANZ Customer Segmentation Challenge (\textbf{Top Performer})
\end{itemize}
\end{itemize}
\end{frame}
\hypertarget{data-science}{%
\section{Data Science}\label{data-science}}
\begin{frame}{What is Data Science ?}
\protect\hypertarget{what-is-data-science}{}
Data Science is an interdisciplinary field that permits you to extract
information from organized or unstructured data.
\begin{figure}
\includegraphics[width=.5\textwidth,height=.5\textheight,keepaspectratio]{images/datascience.jpeg}
\caption{An intersection of many fields of science%
\footnote{%
\tiny{Image source: https://medium.com/believing-these-8-myths-about-what-is-data-science-keeps-you-from-growing-528f1bd240dc}
}%
}
\end{figure}
\end{frame}
\begin{frame}{Data Science Life Cycle}
\protect\hypertarget{data-science-life-cycle}{}
Known as the O.S.E.M.N. framework.
\begin{figure}
\includegraphics[width=.8\textwidth,height=.8\textheight,keepaspectratio]{images/osemn.png}
\caption{Data Science Process%
\footnote{%
\tiny{Image source: https://towardsdatascience.com/5-steps-of-a-data-science-project-lifecycle-26c50372b492}
}%
}
\end{figure}
\end{frame}
\begin{frame}{Obtain (O)}
\protect\hypertarget{obtain-o}{}
\begin{itemize}
\tightlist
\item
Retrieving data from multiple sources of inputs.\\
\begin{itemize}
\item Structured Data: RDBMS, Tabular Data, CSV, TSV.
\item Unstructured Data: NoSQL Databases, API Data (Twitter, Facebook)
\end{itemize}
\vspace{2mm}
\item
Databases: \texttt{\{odbc\}} \vspace{2mm}
\item
Scraping data from websites: \texttt{\{rvest\}} \vspace{2mm}
\item
Data platforms: \textbf{Kaggle}, \textbf{UCI}, \textbf{Competition
Datasets}, \textbf{Government APIs}
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Example of \texttt{\{rvest\}}}
\protect\hypertarget{example-of}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(rvest)}
\KeywordTok{library}\NormalTok{(dplyr)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# reading the HTML page (Lord of the Rings)}
\NormalTok{lor_movie <-}\StringTok{ }\KeywordTok{read_html}\NormalTok{(}\StringTok{"https://www.imdb.com/title/tt0120737/"}\NormalTok{)}
\CommentTok{# Scraping the movie rating.}
\NormalTok{lor_movie }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{html_node}\NormalTok{(}\StringTok{"strong span"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{html_text}\NormalTok{() }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{as.numeric}\NormalTok{()}
\CommentTok{#[1] 8.8}
\CommentTok{# Scraping the cast.}
\NormalTok{lor_movie }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{html_nodes}\NormalTok{(}\StringTok{"#titleCast .itemprop span"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{html_text}\NormalTok{()}
\CommentTok{# Scraping the movie poster.}
\NormalTok{lor_movie }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{html_nodes}\NormalTok{(}\StringTok{"#img_primary img"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{html_attr}\NormalTok{(}\StringTok{"src"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\normalsize
\end{frame}
\hypertarget{pre-processing}{%
\section{Pre-processing}\label{pre-processing}}
\begin{frame}{Scrub (S)}
\protect\hypertarget{scrub-s}{}
\begin{itemize}
\tightlist
\item
Also known as \textbf{data pre-processing}, \textbf{data wrangling}\\
\vspace{2mm}
\item
Converting the data into a unified, suitable format
\begin{itemize}
\item Easier for the data exploration process
\item What your predictive algorithm expects ?
\item \textbf{tidyverse} \texttt{\{dplyr,tidyr,stringr,tibble,purr,ggplot2\}}
\end{itemize}
\vspace{2mm}
\item
Handles data issues
\begin{itemize}
\item Cleaning: Missing values, Outliers, Noisy data
\item Transformation: Normalisation, Feature Discretization
\item Reduction: Feature selection, Dimensionality reduction
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Missing Value Imputation}
\protect\hypertarget{missing-value-imputation}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(simputation)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# Loading iris dataset and randomly inserting NAs.}
\NormalTok{df <-}\StringTok{ }\NormalTok{iris}
\NormalTok{df_NA <-}\StringTok{ }\KeywordTok{as.data.frame}\NormalTok{(}\KeywordTok{lapply}\NormalTok{(df, }\ControlFlowTok{function}\NormalTok{(imp) imp[ }\KeywordTok{sample}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\OtherTok{TRUE}\NormalTok{, }\OtherTok{NA}\NormalTok{), }
\DataTypeTok{prob =} \KeywordTok{c}\NormalTok{(}\FloatTok{0.85}\NormalTok{, }\FloatTok{0.15}\NormalTok{), }\DataTypeTok{size =} \KeywordTok{length}\NormalTok{(imp), }\DataTypeTok{replace =} \OtherTok{TRUE}\NormalTok{)]))}
\CommentTok{# Using median to impute the missing values.}
\NormalTok{median_imputed <-}\StringTok{ }\KeywordTok{impute_median}\NormalTok{(df_NA, }
\NormalTok{ Sepal.Length }\OperatorTok{~}\StringTok{ }\NormalTok{Species)}
\CommentTok{# Using linear regression to impute the missing values.}
\NormalTok{linear_imputed <-}\StringTok{ }\KeywordTok{impute_lm}\NormalTok{(df_NA, Sepal.Length }\OperatorTok{~}\StringTok{ }\NormalTok{Sepal.Width }\OperatorTok{+}\StringTok{ }\NormalTok{Species)}
\CommentTok{# Using CART algorithm to impute the missing values.}
\NormalTok{cart_imputed <-}\StringTok{ }\KeywordTok{impute_cart}\NormalTok{(df_NA, Species }\OperatorTok{~}\StringTok{ }\NormalTok{.)}
\CommentTok{# Imputing multiple variables at once.}
\NormalTok{multivariable_imputed <-}\StringTok{ }\KeywordTok{impute_rlm}\NormalTok{(df_NA, Sepal.Length }\OperatorTok{+}\StringTok{ }\NormalTok{Sepal.Width }
\OperatorTok{~}\StringTok{ }\NormalTok{Petal.Length }\OperatorTok{+}\StringTok{ }\NormalTok{Species)}
\CommentTok{# Imputing using a pre-trained model.}
\NormalTok{model <-}\StringTok{ }\KeywordTok{lm}\NormalTok{(Sepal.Length }\OperatorTok{~}\StringTok{ }\NormalTok{Sepal.Width }\OperatorTok{+}\StringTok{ }\NormalTok{Species, }\DataTypeTok{data=}\NormalTok{iris)}
\NormalTok{model_imputed <-}\StringTok{ }\KeywordTok{impute}\NormalTok{(df_NA, Sepal.Length }\OperatorTok{~}\StringTok{ }\NormalTok{model)}
\end{Highlighting}
\end{Shaded}
\normalsize
\end{frame}
\begin{frame}{Dealing with Outliers}
\protect\hypertarget{dealing-with-outliers}{}
\begin{itemize}
\tightlist
\item
A data point that differs significantly from other observations\\
\vspace{2mm}
\item
Observations that distort your analysis
\begin{itemize}
\item Boxplot visualisation: \texttt{\{ggplot2\}}
\item Grubbs’s test, Dixon’s test, Rosner’s test: \texttt{\{outliers\}}
\item Outlier detection algorithms: \texttt{\{OutlierDetection\}}
\item \textbf{outlierTest()} from \texttt{\{car\}}
\item \textbf{lofactor()} from \texttt{\{DMwR\}} (Local Outlier Factor)
\end{itemize}
\vspace{2mm}
\item
Anomaly detection is itself a different research area !
\begin{itemize}
\item One Class SVM, IsolationForest
\item Unsupervised algorithms (Clustering)
\item Time series: \texttt{\{tsoutliers,oddstream,stray\}}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{Feature Selection}
\protect\hypertarget{feature-selection}{}
\begin{itemize}
\tightlist
\item
Removing redundant features from the dataset \vspace{2mm}
\item
Computational complexity, Address model overfitting \vspace{2mm}
\item
\textbf{Filter Methods}
\begin{itemize}
\item Features are selected based on a statistical score
\item Independent of any machine learning algorithm
\item \textbf{Pearson’s Correlation, Chi-Square, PCA}
\end{itemize}
\vspace{2mm}
\item
\textbf{Wrapper Methods}
\begin{itemize}
\item A subset of features are used to train a model
\item Forward, Backward, Recursive elimination
\item Inbuilt penalization functions: \textbf{LASSO, RIDGE} regression
\item \texttt{\{Boruta,caret,glmnet\}}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Using Correlation}
\protect\hypertarget{using-correlation}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(GGally)}
\KeywordTok{library}\NormalTok{(dplyr)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# Plotting the feature correlations.}
\NormalTok{iris }\OperatorTok{%>%}\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{Species) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{ggpairs}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics[width=0.7\linewidth,height=0.6\textheight]{figs/unnamed-chunk-1} \end{center}
\normalsize
\end{frame}
\begin{frame}[fragile]{Using PCR}
\protect\hypertarget{using-pcr}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(dplyr)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# Plotting the feature importance.}
\NormalTok{pcomp_df <-}\StringTok{ }\NormalTok{iris }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{select}\NormalTok{(}\OperatorTok{-}\NormalTok{Species) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{prcomp}\NormalTok{(}\DataTypeTok{scale. =}\NormalTok{ T, }\DataTypeTok{center =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{plot}\NormalTok{(}\DataTypeTok{type=}\StringTok{"l"}\NormalTok{, }\DataTypeTok{main =} \StringTok{"Principle Components"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics[width=0.7\linewidth,height=0.6\textheight]{figs/unnamed-chunk-2} \end{center}
\normalsize
\end{frame}
\begin{frame}[fragile]{Example of \texttt{\{Boruta\}}}
\protect\hypertarget{example-of-1}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(Boruta)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# Boruta is a feature selection algorithm based on the random forests algorithm.}
\NormalTok{boruta_df <-}\StringTok{ }\KeywordTok{Boruta}\NormalTok{(Species }\OperatorTok{~}\StringTok{ }\NormalTok{., }\DataTypeTok{data=}\NormalTok{iris, }\DataTypeTok{doTrace=}\DecValTok{0}\NormalTok{)}
\CommentTok{# Plotting the feature importance.}
\KeywordTok{plot}\NormalTok{(boruta_df, }\DataTypeTok{xlab=}\StringTok{"Features"}\NormalTok{, }\DataTypeTok{main=}\StringTok{"Variable Importance"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics[width=0.7\linewidth,height=0.6\textheight]{figs/unnamed-chunk-3} \end{center}
\normalsize
\end{frame}
\begin{frame}[fragile]{Example of \texttt{\{caret\}}}
\protect\hypertarget{example-of-2}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(caret)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# Build a decision tree model using rpart (Recursive Partitioning And Regression Trees)}
\NormalTok{rPart_df <-}\StringTok{ }\KeywordTok{train}\NormalTok{(Species }\OperatorTok{~}\StringTok{ }\NormalTok{., }\DataTypeTok{data=}\NormalTok{iris, }\DataTypeTok{method=}\StringTok{"rpart"}\NormalTok{)}
\NormalTok{rPart_imp <-}\StringTok{ }\KeywordTok{varImp}\NormalTok{(rPart_df)}
\CommentTok{# Plotting the feature importance.}
\KeywordTok{plot}\NormalTok{(rPart_imp, }\DataTypeTok{top =} \DecValTok{3}\NormalTok{, }\DataTypeTok{main=}\StringTok{'Variable Importance'}\NormalTok{, }\DataTypeTok{ylab =} \StringTok{"Features"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics[width=0.7\linewidth,height=0.6\textheight]{figs/unnamed-chunk-4} \end{center}
\normalsize
\end{frame}
\hypertarget{data-visualisation}{%
\section{Data Visualisation}\label{data-visualisation}}
\begin{frame}{Explore (E)}
\protect\hypertarget{explore-e}{}
\begin{itemize}
\tightlist
\item
Examination of data, features, and their characteristics \vspace{2mm}
\begin{itemize}
\item Data types: numerical, ordinal, and nominal data
\item Summary statistics
\item Feature distributions
\item Feature correlations (positive, negative)
\item Classification: class distribution (\textbf{Class Imbalance?})
\end{itemize}
\vspace{2mm}
\item
Invest your time more on the data exploration process
\begin{itemize}
\item Frequency distribution: \textbf{Histograms}
\item Outlier detection: \textbf{Box plots}
\item Feature correlation analysis: \textbf{Scatter plots}
\item Time series analysis: \textbf{Trend and Seasonal plots}
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}{Tools available for Exploration}
\protect\hypertarget{tools-available-for-exploration}{}
\begin{figure}
\includegraphics[width=.9\textwidth,height=.9\textheight,keepaspectratio]{images/data_exploration.png}
\caption{Plots available from \texttt{\{ggplot2\}}%
\footnote{%
\tiny{Image source: https://www.pinterest.com.au/pin/281686151677624808/}
}%
}
\end{figure}
\end{frame}
\begin{frame}{Seasonal plot from \texttt{\{feasts\}}}
\protect\hypertarget{seasonal-plot-from}{}
\begin{figure}
\includegraphics[width=.55\textwidth,height=.55\textheight,keepaspectratio]{images/daily_seasonal.png}
\caption{The presence of multiple seasonal cycles%
\footnote{%
\tiny{Github repo: https://github.com/kasungayan/Meldatathon2020}
}%
}
\end{figure}
\end{frame}
\hypertarget{model-prediction}{%
\section{Model Prediction}\label{model-prediction}}
\begin{frame}{Model Development (MD)}
\protect\hypertarget{model-development-md}{}
\begin{itemize}
\tightlist
\item
Model parameter estimation, Hyper-parameter tuning \vspace{2mm}
\begin{figure}
\includegraphics[width=.6\textwidth,height=.6\textheight,keepaspectratio]{images/model_validation.PNG}
\caption{Model Training and Validation%
\footnote{%
\tiny{Image source: https://towardsdatascience.com/5-steps-of-a-data-science-project-lifecycle-26c50372b492}
}%
}
\end{figure}
\end{itemize}
\end{frame}
\begin{frame}{Model Development Techniques}
\protect\hypertarget{model-development-techniques}{}
\begin{itemize}
\tightlist
\item
\textbf{Supervised Learning} or \textbf{Unsupervised Learning}
\vspace{2mm}
\begin{itemize}
\item Regression: Linear-regression, Support Vector Machine (SVM), Lasso-Regression
\item Classification: Naive Bayes, Random Forest, K-Nearest Neighbors
\item Clustering: K-Means, Fuzzy C-Means, Self Organising Maps (SOM)
\end{itemize}
\vspace{2mm}
\item
Neural Networks: Multilayer Perceptron (\textbf{MLP}), Recurrent
Neural Network (\textbf{RNN}), Convolutional Neural Network
(\textbf{CNN}) \vspace{2mm}
\item
Different applications: \textbf{Spam Detection}, \textbf{Market
Segmentation}, \textbf{Image Classification}, \textbf{Time Series
Forecasting}, \textbf{Language Translation}
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Naive Bayes classifier}
\protect\hypertarget{naive-bayes-classifier}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(mlbench) }\CommentTok{# multiple benchmark datasets for different machine learning tasks.}
\KeywordTok{library}\NormalTok{(caret) }\CommentTok{# multiple inbuilt regression and classification algorithms.}
\KeywordTok{library}\NormalTok{(rsample) }\CommentTok{# data splitting.}
\KeywordTok{library}\NormalTok{(dplyr)}
\KeywordTok{data}\NormalTok{(BreastCancer)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# Splitting the data into train and test sets.}
\NormalTok{df_BreastCancer_split <-}\StringTok{ }\KeywordTok{initial_split}\NormalTok{(BreastCancer, }\DataTypeTok{prop =} \FloatTok{.7}\NormalTok{)}
\CommentTok{# Similar splitting using caTools}
\CommentTok{# df_BreastCancer_split <- sample.split(BreastCancer,SplitRatio = 0.7)}
\NormalTok{df_BreastCancer_train <-}\StringTok{ }\KeywordTok{training}\NormalTok{(df_BreastCancer_split)}
\NormalTok{df_BreastCancer_test <-}\StringTok{ }\KeywordTok{testing}\NormalTok{(df_BreastCancer_split)}
\CommentTok{# Checking for class distribution.}
\KeywordTok{table}\NormalTok{(df_BreastCancer_train}\OperatorTok{$}\NormalTok{Class) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{prop.table}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
##
## benign malignant
## 0.6571429 0.3428571
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{table}\NormalTok{(df_BreastCancer_test}\OperatorTok{$}\NormalTok{Class) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{prop.table}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
##
## benign malignant
## 0.6507177 0.3492823
\end{verbatim}
\normalsize
\end{frame}
\begin{frame}[fragile]{Naive Bayes classifier Cont.}
\protect\hypertarget{naive-bayes-classifier-cont.}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# create feature and class attributes.}
\NormalTok{features <-}\StringTok{ }\KeywordTok{setdiff}\NormalTok{(}\KeywordTok{names}\NormalTok{(df_BreastCancer_train), }\StringTok{"Class"}\NormalTok{)}
\NormalTok{train_features <-}\StringTok{ }\NormalTok{df_BreastCancer_train[, features]}
\NormalTok{train_class <-}\StringTok{ }\NormalTok{df_BreastCancer_train}\OperatorTok{$}\NormalTok{Class}
\CommentTok{# Define a 10-fold cross validation procedure.}
\NormalTok{train_control <-}\StringTok{ }\KeywordTok{trainControl}\NormalTok{(}\DataTypeTok{method =} \StringTok{"cv"}\NormalTok{, }\DataTypeTok{number =} \DecValTok{10}\NormalTok{)}
\CommentTok{# train the naive bayes model}
\NormalTok{model_nb1 <-}\StringTok{ }\KeywordTok{train}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ train_features, }\DataTypeTok{y =}\NormalTok{ train_class, }\DataTypeTok{method =} \StringTok{"nb"}\NormalTok{, }\DataTypeTok{trControl =}\NormalTok{ train_control)}
\CommentTok{#Show the confusion matrix.}
\KeywordTok{confusionMatrix}\NormalTok{(model_nb1)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## Cross-Validated (10 fold) Confusion Matrix
##
## (entries are percentual average cell counts across resamples)
##
## Reference
## Prediction benign malignant
## benign 63.5 0.4
## malignant 2.2 33.9
##
## Accuracy (average) : 0.9735
\end{verbatim}
\normalsize
\end{frame}
\begin{frame}[fragile]{Naive Bayes classifier Cont.}
\protect\hypertarget{naive-bayes-classifier-cont.-1}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# hyper-parameter grid}
\NormalTok{hyper_search_grid <-}\StringTok{ }\KeywordTok{expand.grid}\NormalTok{(}\DataTypeTok{usekernel =} \KeywordTok{c}\NormalTok{(}\OtherTok{TRUE}\NormalTok{, }\OtherTok{FALSE}\NormalTok{), }\DataTypeTok{fL =} \DecValTok{0}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DataTypeTok{adjust =} \KeywordTok{seq}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{5}\NormalTok{, }\DataTypeTok{by =} \DecValTok{1}\NormalTok{))}
\CommentTok{# train the naive bayes model using a hyper-parameter grid.}
\NormalTok{model_nb2 <-}\StringTok{ }\KeywordTok{train}\NormalTok{(}\DataTypeTok{x =}\NormalTok{ train_features, }\DataTypeTok{y =}\NormalTok{ train_class, }\DataTypeTok{method =} \StringTok{"nb"}\NormalTok{, }
\DataTypeTok{trControl =}\NormalTok{ train_control, }\DataTypeTok{tuneGrid =}\NormalTok{ hyper_search_grid)}
\CommentTok{# Printing best models.}
\CommentTok{# model_nb2$results %>% }
\CommentTok{# arrange(desc(Accuracy)) %>% head(4)}
\CommentTok{# results for best model}
\KeywordTok{confusionMatrix}\NormalTok{(model_nb2)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## Cross-Validated (10 fold) Confusion Matrix
##
## (entries are percentual average cell counts across resamples)
##
## Reference
## Prediction benign malignant
## benign 63.5 0.8
## malignant 2.2 33.5
##
## Accuracy (average) : 0.9694
\end{verbatim}
\normalsize
\end{frame}
\begin{frame}[fragile]{Naive Bayes results on the testset}
\protect\hypertarget{naive-bayes-results-on-the-testset}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Applying the best model to unseen (test) dataset.}
\NormalTok{prediction_test <-}\StringTok{ }\KeywordTok{predict}\NormalTok{(model_nb2, }\DataTypeTok{newdata =}\NormalTok{ df_BreastCancer_test)}
\CommentTok{# Printing the confusing matrix on the test set.}
\KeywordTok{confusionMatrix}\NormalTok{(prediction_test, df_BreastCancer_test}\OperatorTok{$}\NormalTok{Class)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## Confusion Matrix and Statistics
##
## Reference
## Prediction benign malignant
## benign 132 1
## malignant 4 72
##
## Accuracy : 0.9761
## 95% CI : (0.9451, 0.9922)
## No Information Rate : 0.6507
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.9479
##
## Mcnemar's Test P-Value : 0.3711
##
## Sensitivity : 0.9706
## Specificity : 0.9863
## Pos Pred Value : 0.9925
## Neg Pred Value : 0.9474
## Prevalence : 0.6507
## Detection Rate : 0.6316
## Detection Prevalence : 0.6364
## Balanced Accuracy : 0.9784
##
## 'Positive' Class : benign
##
\end{verbatim}
\normalsize
\end{frame}
\begin{frame}[fragile]{Generating the ROC curve}
\protect\hypertarget{generating-the-roc-curve}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(caTools) }\CommentTok{#to generate ROC curves}
\NormalTok{prob_results <-}\StringTok{ }\KeywordTok{predict}\NormalTok{(model_nb2, df_BreastCancer_test, }\DataTypeTok{type =} \StringTok{"prob"}\NormalTok{)}
\CommentTok{# Generating the ROC curve for the test set.}
\NormalTok{caTools}\OperatorTok{::}\KeywordTok{colAUC}\NormalTok{(prob_results, df_BreastCancer_test[[}\StringTok{"Class"}\NormalTok{]], }\DataTypeTok{plotROC =} \OtherTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics[width=0.7\linewidth,height=0.6\textheight]{figs/unnamed-chunk-9} \end{center}
\begin{verbatim}
## benign malignant
## benign vs. malignant 0.9917405 0.9917405
\end{verbatim}
\normalsize
\end{frame}
\begin{frame}[fragile]{Decision tree}
\protect\hypertarget{decision-tree}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(caret)}
\KeywordTok{library}\NormalTok{(rpart)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# Remove incomplete records.}
\NormalTok{df_BreastCancer_train <-}\StringTok{ }\NormalTok{df_BreastCancer_train[}\KeywordTok{complete.cases}\NormalTok{(df_BreastCancer_train), ]}
\CommentTok{# Define a 10-fold cross validation procedure.}
\NormalTok{train_control <-}\StringTok{ }\KeywordTok{trainControl}\NormalTok{(}\DataTypeTok{method =} \StringTok{"cv"}\NormalTok{, }\DataTypeTok{number =} \DecValTok{10}\NormalTok{)}
\CommentTok{# train the naive bayes model.}
\NormalTok{model_dt <-}\StringTok{ }\KeywordTok{train}\NormalTok{(Class }\OperatorTok{~}\StringTok{ }\NormalTok{., }\DataTypeTok{data=}\NormalTok{df_BreastCancer_train, }\DataTypeTok{method =} \StringTok{"rpart"}\NormalTok{, }\DataTypeTok{trControl =}\NormalTok{ train_control)}
\CommentTok{#Show the confusion matrix.}
\KeywordTok{confusionMatrix}\NormalTok{(model_dt)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## Cross-Validated (10 fold) Confusion Matrix
##
## (entries are percentual average cell counts across resamples)
##
## Reference
## Prediction benign malignant
## benign 62.2 3.3
## malignant 3.1 31.3
##
## Accuracy (average) : 0.9357
\end{verbatim}
\normalsize
\end{frame}
\begin{frame}[fragile]{Visualising the decision tree}
\protect\hypertarget{visualising-the-decision-tree}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(rattle)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# Generating the decision tree.}
\CommentTok{# You can also separately use the rpart and rpart.plot to reproduce this.}
\KeywordTok{fancyRpartPlot}\NormalTok{(model_dt}\OperatorTok{$}\NormalTok{finalModel)}
\end{Highlighting}
\end{Shaded}
\begin{center}\includegraphics[width=0.8\linewidth,height=0.7\textheight]{figs/unnamed-chunk-11} \end{center}
\normalsize
\end{frame}
\begin{frame}[fragile]{Regression using \texttt{\{caret\}}}
\protect\hypertarget{regression-using}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(caret)}
\KeywordTok{library}\NormalTok{(dplyr)}
\KeywordTok{library}\NormalTok{(rsample)}
\KeywordTok{set.seed}\NormalTok{(}\DecValTok{1234}\NormalTok{)}
\CommentTok{# Reading the data.}
\NormalTok{df_households <-}\StringTok{ }\KeywordTok{read.csv}\NormalTok{(}\StringTok{"data/realestate.csv"}\NormalTok{)}
\NormalTok{df_households_filtered <-}\StringTok{ }\NormalTok{df_households[, }\KeywordTok{c}\NormalTok{(}\OperatorTok{-}\DecValTok{1}\NormalTok{,}\OperatorTok{-}\DecValTok{2}\NormalTok{)]}
\CommentTok{# Splitting the data.}
\KeywordTok{colnames}\NormalTok{(df_households_filtered)[}\DecValTok{6}\NormalTok{] <-}\StringTok{ "price"}
\NormalTok{data_split <-}\StringTok{ }\KeywordTok{initial_split}\NormalTok{(df_households_filtered, }\DataTypeTok{prop =} \FloatTok{.7}\NormalTok{) }
\NormalTok{train <-}\StringTok{ }\KeywordTok{training}\NormalTok{(data_split)}
\NormalTok{test <-}\StringTok{ }\KeywordTok{testing}\NormalTok{(data_split)}
\CommentTok{# Defining the feature variables.}
\NormalTok{x <-}\StringTok{ }\KeywordTok{model.matrix}\NormalTok{(price}\OperatorTok{~}\NormalTok{., train)}
\CommentTok{# Defining the class variable.}
\NormalTok{y <-}\StringTok{ }\NormalTok{train}\OperatorTok{$}\NormalTok{price}
\end{Highlighting}
\end{Shaded}
\normalsize
\end{frame}
\begin{frame}[fragile]{Regression using \texttt{\{caret\}} (2)}
\protect\hypertarget{regression-using-2}{}
\tiny
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Linear regression model training.}
\NormalTok{model <-}\StringTok{ }\KeywordTok{train}\NormalTok{(price }\OperatorTok{~}\StringTok{ }\NormalTok{., train, }\DataTypeTok{method =} \StringTok{"lm"}\NormalTok{,}\DataTypeTok{trControl =} \KeywordTok{trainControl}\NormalTok{(}\DataTypeTok{method =} \StringTok{"cv"}\NormalTok{,}
\DataTypeTok{number =} \DecValTok{10}\NormalTok{))}
\CommentTok{# Model summary.}
\KeywordTok{print}\NormalTok{(model)}
\end{Highlighting}
\end{Shaded}