-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotebook.tex
1805 lines (1456 loc) · 88.2 KB
/
notebook.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
% Default to the notebook output style
% Inherit from the specified cell style.
\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
% Nicer default font (+ math font) than Computer Modern for most use cases
\usepackage{mathpazo}
% Basic figure setup, for now with no caption control since it's done
% automatically by Pandoc (which extracts  syntax from Markdown).
\usepackage{graphicx}
% We will generate all images so they have a width \maxwidth. This means
% that they will get their normal width if they fit onto the page, but
% are scaled down if they would overflow the margins.
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
\else\Gin@nat@width\fi}
\makeatother
\let\Oldincludegraphics\includegraphics
% Set max figure width to be 80% of text width, for now hardcoded.
\renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=.8\maxwidth]{#1}}
% Ensure that by default, figures have no caption (until we provide a
% proper Figure object with a Caption API and a way to capture that
% in the conversion process - todo).
\usepackage{caption}
\DeclareCaptionLabelFormat{nolabel}{}
\captionsetup{labelformat=nolabel}
\usepackage{adjustbox} % Used to constrain images to a maximum size
\usepackage{xcolor} % Allow colors to be defined
\usepackage{enumerate} % Needed for markdown enumerations to work
\usepackage{geometry} % Used to adjust the document margins
\usepackage{amsmath} % Equations
\usepackage{amssymb} % Equations
\usepackage{textcomp} % defines textquotesingle
% Hack from http://tex.stackexchange.com/a/47451/13684:
\AtBeginDocument{%
\def\PYZsq{\textquotesingle}% Upright quotes in Pygmentized code
}
\usepackage{upquote} % Upright quotes for verbatim code
\usepackage{eurosym} % defines \euro
\usepackage[mathletters]{ucs} % Extended unicode (utf-8) support
\usepackage[utf8x]{inputenc} % Allow utf-8 characters in the tex document
\usepackage{fancyvrb} % verbatim replacement that allows latex
\usepackage{grffile} % extends the file name processing of package graphics
% to support a larger range
% The hyperref package gives us a pdf with properly built
% internal navigation ('pdf bookmarks' for the table of contents,
% internal cross-reference links, web links for URLs, etc.)
\usepackage{hyperref}
\usepackage{longtable} % longtable support required by pandoc >1.10
\usepackage{booktabs} % table support for pandoc > 1.12.2
\usepackage[inline]{enumitem} % IRkernel/repr support (it uses the enumerate* environment)
\usepackage[normalem]{ulem} % ulem is needed to support strikethroughs (\sout)
% normalem makes italics be italics, not underlines
% Colors for the hyperref package
\definecolor{urlcolor}{rgb}{0,.145,.698}
\definecolor{linkcolor}{rgb}{.71,0.21,0.01}
\definecolor{citecolor}{rgb}{.12,.54,.11}
% ANSI colors
\definecolor{ansi-black}{HTML}{3E424D}
\definecolor{ansi-black-intense}{HTML}{282C36}
\definecolor{ansi-red}{HTML}{E75C58}
\definecolor{ansi-red-intense}{HTML}{B22B31}
\definecolor{ansi-green}{HTML}{00A250}
\definecolor{ansi-green-intense}{HTML}{007427}
\definecolor{ansi-yellow}{HTML}{DDB62B}
\definecolor{ansi-yellow-intense}{HTML}{B27D12}
\definecolor{ansi-blue}{HTML}{208FFB}
\definecolor{ansi-blue-intense}{HTML}{0065CA}
\definecolor{ansi-magenta}{HTML}{D160C4}
\definecolor{ansi-magenta-intense}{HTML}{A03196}
\definecolor{ansi-cyan}{HTML}{60C6C8}
\definecolor{ansi-cyan-intense}{HTML}{258F8F}
\definecolor{ansi-white}{HTML}{C5C1B4}
\definecolor{ansi-white-intense}{HTML}{A1A6B2}
% commands and environments needed by pandoc snippets
% extracted from the output of `pandoc -s`
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\newenvironment{Shaded}{}{}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}}
\newcommand{\RegionMarkerTok}[1]{{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
\newcommand{\NormalTok}[1]{{#1}}
% Additional commands for more recent versions of Pandoc
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{{#1}}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{{#1}}}
\newcommand{\ImportTok}[1]{{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{{#1}}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{{#1}}}
\newcommand{\BuiltInTok}[1]{{#1}}
\newcommand{\ExtensionTok}[1]{{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{{#1}}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
% Define a nice break command that doesn't care if a line doesn't already
% exist.
\def\br{\hspace*{\fill} \\* }
% Math Jax compatability definitions
\def\gt{>}
\def\lt{<}
% Document parameters
\title{customer\_segments}
% Pygments definitions
\makeatletter
\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%
\let\PY@ul=\relax \let\PY@tc=\relax%
\let\PY@bc=\relax \let\PY@ff=\relax}
\def\PY@tok#1{\csname PY@tok@#1\endcsname}
\def\PY@toks#1+{\ifx\relax#1\empty\else%
\PY@tok{#1}\expandafter\PY@toks\fi}
\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%
\PY@it{\PY@bf{\PY@ff{#1}}}}}}}
\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}
\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}}
\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}}
\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}}
\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}}
\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}}
\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}}
\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit}
\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf}
\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}
\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@fm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@vm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@sa\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@dl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@ch\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@cpf\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\def\PYZbs{\char`\\}
\def\PYZus{\char`\_}
\def\PYZob{\char`\{}
\def\PYZcb{\char`\}}
\def\PYZca{\char`\^}
\def\PYZam{\char`\&}
\def\PYZlt{\char`\<}
\def\PYZgt{\char`\>}
\def\PYZsh{\char`\#}
\def\PYZpc{\char`\%}
\def\PYZdl{\char`\$}
\def\PYZhy{\char`\-}
\def\PYZsq{\char`\'}
\def\PYZdq{\char`\"}
\def\PYZti{\char`\~}
% for compatibility with earlier versions
\def\PYZat{@}
\def\PYZlb{[}
\def\PYZrb{]}
\makeatother
% Exact colors from NB
\definecolor{incolor}{rgb}{0.0, 0.0, 0.5}
\definecolor{outcolor}{rgb}{0.545, 0.0, 0.0}
% Prevent overflowing lines due to hard-to-break entities
\sloppy
% Setup hyperref package
\hypersetup{
breaklinks=true, % so long urls are correctly broken across lines
colorlinks=true,
urlcolor=urlcolor,
linkcolor=linkcolor,
citecolor=citecolor,
}
% Slightly bigger margins than the latex defaults
\geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\begin{document}
\maketitle
\hypertarget{machine-learning-engineer-nanodegree}{%
\section{Machine Learning Engineer
Nanodegree}\label{machine-learning-engineer-nanodegree}}
\hypertarget{unsupervised-learning}{%
\subsection{Unsupervised Learning}\label{unsupervised-learning}}
\hypertarget{project-creating-customer-segments}{%
\subsection{Project: Creating Customer
Segments}\label{project-creating-customer-segments}}
Welcome to the third project of the Machine Learning Engineer
Nanodegree! In this notebook, some template code has already been
provided for you, and it will be your job to implement the additional
functionality necessary to successfully complete this project. Sections
that begin with \textbf{`Implementation'} in the header indicate that
the following block of code will require additional functionality which
you must provide. Instructions will be provided for each section and the
specifics of the implementation are marked in the code block with a
\texttt{\textquotesingle{}TODO\textquotesingle{}} statement. Please be
sure to read the instructions carefully!
In addition to implementing code, there will be questions that you must
answer which relate to the project and your implementation. Each section
where you will answer a question is preceded by a \textbf{`Question X'}
header. Carefully read each question and provide thorough answers in the
following text boxes that begin with \textbf{`Answer:'}. Your project
submission will be evaluated based on your answers to each of the
questions and the implementation you provide.
\begin{quote}
\textbf{Note:} Code and Markdown cells can be executed using the
\textbf{Shift + Enter} keyboard shortcut. In addition, Markdown cells
can be edited by typically double-clicking the cell to enter edit mode.
\end{quote}
\hypertarget{getting-started}{%
\subsection{Getting Started}\label{getting-started}}
In this project, you will analyze a dataset containing data on various
customers' annual spending amounts (reported in \emph{monetary units})
of diverse product categories for internal structure. One goal of this
project is to best describe the variation in the different types of
customers that a wholesale distributor interacts with. Doing so would
equip the distributor with insight into how to best structure their
delivery service to meet the needs of each customer.
The dataset for this project can be found on the
\href{https://archive.ics.uci.edu/ml/datasets/Wholesale+customers}{UCI
Machine Learning Repository}. For the purposes of this project, the
features \texttt{\textquotesingle{}Channel\textquotesingle{}} and
\texttt{\textquotesingle{}Region\textquotesingle{}} will be excluded in
the analysis --- with focus instead on the six product categories
recorded for customers.
Run the code block below to load the wholesale customers dataset, along
with a few of the necessary Python libraries required for this project.
You will know the dataset loaded successfully if the size of the dataset
is reported.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}58}]:} \PY{c+c1}{\PYZsh{} Import libraries necessary for this project}
\PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k}{as} \PY{n+nn}{np}
\PY{k+kn}{import} \PY{n+nn}{pandas} \PY{k}{as} \PY{n+nn}{pd}
\PY{k+kn}{from} \PY{n+nn}{IPython}\PY{n+nn}{.}\PY{n+nn}{display} \PY{k}{import} \PY{n}{display} \PY{c+c1}{\PYZsh{} Allows the use of display() for DataFrames}
\PY{c+c1}{\PYZsh{} Import supplementary visualizations code visuals.py}
\PY{k+kn}{import} \PY{n+nn}{visuals} \PY{k}{as} \PY{n+nn}{vs}
\PY{c+c1}{\PYZsh{} Pretty display for notebooks}
\PY{o}{\PYZpc{}}\PY{k}{matplotlib} inline
\PY{c+c1}{\PYZsh{} Load the wholesale customers dataset}
\PY{k}{try}\PY{p}{:}
\PY{n}{data} \PY{o}{=} \PY{n}{pd}\PY{o}{.}\PY{n}{read\PYZus{}csv}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{customers.csv}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\PY{n}{data}\PY{o}{.}\PY{n}{drop}\PY{p}{(}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Region}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Channel}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{,} \PY{n}{axis} \PY{o}{=} \PY{l+m+mi}{1}\PY{p}{,} \PY{n}{inplace} \PY{o}{=} \PY{k+kc}{True}\PY{p}{)}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Wholesale customers dataset has }\PY{l+s+si}{\PYZob{}\PYZcb{}}\PY{l+s+s2}{ samples with }\PY{l+s+si}{\PYZob{}\PYZcb{}}\PY{l+s+s2}{ features each.}\PY{l+s+s2}{\PYZdq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{o}{*}\PY{n}{data}\PY{o}{.}\PY{n}{shape}\PY{p}{)}\PY{p}{)}
\PY{k}{except}\PY{p}{:}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Dataset could not be loaded. Is the dataset missing?}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
Wholesale customers dataset has 440 samples with 6 features each.
\end{Verbatim}
\hypertarget{data-exploration}{%
\subsection{Data Exploration}\label{data-exploration}}
In this section, you will begin exploring the data through
visualizations and code to understand how each feature is related to the
others. You will observe a statistical description of the dataset,
consider the relevance of each feature, and select a few sample data
points from the dataset which you will track through the course of this
project.
Run the code block below to observe a statistical description of the
dataset. Note that the dataset is composed of six important product
categories: \textbf{`Fresh'}, \textbf{`Milk'}, \textbf{`Grocery'},
\textbf{`Frozen'}, \textbf{`Detergents\_Paper'}, and
\textbf{`Delicatessen'}. Consider what each category represents in terms
of products you could purchase.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}59}]:} \PY{c+c1}{\PYZsh{} Display a description of the dataset}
\PY{n}{display}\PY{p}{(}\PY{n}{data}\PY{o}{.}\PY{n}{describe}\PY{p}{(}\PY{p}{)}\PY{p}{)}
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen \
count 440.000000 440.000000 440.000000 440.000000
mean 12000.297727 5796.265909 7951.277273 3071.931818
std 12647.328865 7380.377175 9503.162829 4854.673333
min 3.000000 55.000000 3.000000 25.000000
25% 3127.750000 1533.000000 2153.000000 742.250000
50% 8504.000000 3627.000000 4755.500000 1526.000000
75% 16933.750000 7190.250000 10655.750000 3554.250000
max 112151.000000 73498.000000 92780.000000 60869.000000
Detergents_Paper Delicatessen
count 440.000000 440.000000
mean 2881.493182 1524.870455
std 4767.854448 2820.105937
min 3.000000 3.000000
25% 256.750000 408.250000
50% 816.500000 965.500000
75% 3922.000000 1820.250000
max 40827.000000 47943.000000
\end{verbatim}
\hypertarget{implementation-selecting-samples}{%
\subsubsection{Implementation: Selecting
Samples}\label{implementation-selecting-samples}}
To get a better understanding of the customers and how their data will
transform through the analysis, it would be best to select a few sample
data points and explore them in more detail. In the code block below,
add \textbf{three} indices of your choice to the \texttt{indices} list
which will represent the customers to track. It is suggested to try
different sets of samples until you obtain customers that vary
significantly from one another.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}60}]:} \PY{c+c1}{\PYZsh{} TODO: Select three indices of your choice you wish to sample from the dataset}
\PY{n}{indices} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{11}\PY{p}{,} \PY{l+m+mi}{111}\PY{p}{]}
\PY{c+c1}{\PYZsh{} Create a DataFrame of the chosen samples}
\PY{n}{samples} \PY{o}{=} \PY{n}{pd}\PY{o}{.}\PY{n}{DataFrame}\PY{p}{(}\PY{n}{data}\PY{o}{.}\PY{n}{loc}\PY{p}{[}\PY{n}{indices}\PY{p}{]}\PY{p}{,} \PY{n}{columns} \PY{o}{=} \PY{n}{data}\PY{o}{.}\PY{n}{keys}\PY{p}{(}\PY{p}{)}\PY{p}{)}\PY{o}{.}\PY{n}{reset\PYZus{}index}\PY{p}{(}\PY{n}{drop} \PY{o}{=} \PY{k+kc}{True}\PY{p}{)}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Chosen samples of wholesale customers dataset:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\PY{n}{display}\PY{p}{(}\PY{n}{samples}\PY{p}{)}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
Chosen samples of wholesale customers dataset:
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
0 7057 9810 9568 1762 3293 1776
1 13146 1124 4523 1420 549 497
2 12579 11114 17569 805 6457 1519
\end{verbatim}
\hypertarget{question-1}{%
\subsubsection{Question 1}\label{question-1}}
Consider the total purchase cost of each product category and the
statistical description of the dataset above for your sample customers.
\begin{itemize}
\tightlist
\item
What kind of establishment (customer) could each of the three samples
you've chosen represent?
\end{itemize}
\textbf{Hint:} Examples of establishments include places like markets,
cafes, delis, wholesale retailers, among many others. Avoid using names
for establishments, such as saying \emph{``McDonalds''} when describing
a sample customer as a restaurant. You can use the mean values for
reference to compare your samples with. The mean values are as follows:
\begin{itemize}
\tightlist
\item
Fresh: 12000.2977
\item
Milk: 5796.2
\item
Grocery: 3071.9
\item
Detergents\_paper: 2881.4
\item
Delicatessen: 1524.8
\end{itemize}
Knowing this, how do your samples compare? Does that help in driving
your insight into what kind of establishments they might be?
\textbf{Answer:}
\textbf{Data Index 1 -} This customer purchases adeqate amount of fresh
products. Milk uses are extremly high as compare to mean value. Grocery
and delicatessen uses are slightly more than average. Frozen products
uses are less. Detergent uses are more than average. Use of just
sufficient amount of detergent indicates towards restaurants. But fresh
products use are just normal but milk products use are extremy high, so
this must be a \texttt{sweet\ restaurants} ulilises good amount of
grocery and delicatessen.
\textbf{Data Index 11 -} This customer buys fresh material more than
average (mean). Milk purchase is very less below 25\%. Grocery is in
sufficient amount around 50 percentile. Frozen product quantity is very
low. Detergents and delicatessen are like negligible compared to
average. So I have very strong opinion for this cutomer being a
\texttt{street\ vendor} which sells fresh food items (like salad and
many more) on daily basis. Uses very low detergent strongly back up this
opinion for street vendor (uses one time plates and disposals). Grocery
is in sufficient use.
\textbf{Data Index 111 -} This customer has almost all the item
categories in large amount (more than average). This could be
\texttt{SuperMarket\ (retailer\ Grocery\ Store)} based on their higher
than average purchase costs across all product categories. The
detergents quantity is unexpectedly high as comared to fresh and milk
products, so this might also be a \texttt{hostel\ mess} (just a guess).
\hypertarget{implementation-feature-relevance}{%
\subsubsection{Implementation: Feature
Relevance}\label{implementation-feature-relevance}}
One interesting thought to consider is if one (or more) of the six
product categories is actually relevant for understanding customer
purchasing. That is to say, is it possible to determine whether
customers purchasing some amount of one category of products will
necessarily purchase some proportional amount of another category of
products? We can make this determination quite easily by training a
supervised regression learner on a subset of the data with one feature
removed, and then score how well that model can predict the removed
feature.
In the code block below, you will need to implement the following: -
Assign \texttt{new\_data} a copy of the data by removing a feature of
your choice using the \texttt{DataFrame.drop} function. - Use
\texttt{sklearn.cross\_validation.train\_test\_split} to split the
dataset into training and testing sets. - Use the removed feature as
your target label. Set a \texttt{test\_size} of \texttt{0.25} and set a
\texttt{random\_state}. - Import a decision tree regressor, set a
\texttt{random\_state}, and fit the learner to the training data. -
Report the prediction score of the testing set using the regressor's
\texttt{score} function.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}61}]:} \PY{c+c1}{\PYZsh{} TODO: Make a copy of the DataFrame, using the \PYZsq{}drop\PYZsq{} function to drop the given feature}
\PY{n}{target} \PY{o}{=} \PY{n}{data}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Frozen}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
\PY{n}{new\PYZus{}data} \PY{o}{=} \PY{n}{data}\PY{o}{.}\PY{n}{drop}\PY{p}{(}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Frozen}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{,} \PY{n}{axis} \PY{o}{=} \PY{l+m+mi}{1}\PY{p}{)}
\PY{c+c1}{\PYZsh{} TODO: Split the data into training and testing sets(0.25) using the given feature as the target}
\PY{c+c1}{\PYZsh{} Set a random state.}
\PY{k+kn}{from} \PY{n+nn}{sklearn}\PY{n+nn}{.}\PY{n+nn}{model\PYZus{}selection} \PY{k}{import} \PY{n}{train\PYZus{}test\PYZus{}split}
\PY{n}{X\PYZus{}train}\PY{p}{,} \PY{n}{X\PYZus{}test}\PY{p}{,} \PY{n}{y\PYZus{}train}\PY{p}{,} \PY{n}{y\PYZus{}test} \PY{o}{=} \PY{n}{train\PYZus{}test\PYZus{}split}\PY{p}{(}\PY{n}{new\PYZus{}data}\PY{p}{,} \PY{n}{target}\PY{p}{,} \PY{n}{test\PYZus{}size} \PY{o}{=} \PY{l+m+mf}{0.25}\PY{p}{,} \PY{n}{random\PYZus{}state} \PY{o}{=} \PY{l+m+mi}{42}\PY{p}{)}
\PY{c+c1}{\PYZsh{} TODO: Create a decision tree regressor and fit it to the training set}
\PY{k+kn}{from} \PY{n+nn}{sklearn}\PY{n+nn}{.}\PY{n+nn}{tree} \PY{k}{import} \PY{n}{DecisionTreeRegressor}
\PY{n}{regressor} \PY{o}{=} \PY{n}{DecisionTreeRegressor}\PY{p}{(}\PY{n}{random\PYZus{}state} \PY{o}{=} \PY{l+m+mi}{42}\PY{p}{)}
\PY{n}{regressor}\PY{o}{.}\PY{n}{fit}\PY{p}{(}\PY{n}{X\PYZus{}train}\PY{p}{,} \PY{n}{y\PYZus{}train}\PY{p}{)}
\PY{c+c1}{\PYZsh{} TODO: Report the score of the prediction using the testing set}
\PY{n}{score} \PY{o}{=} \PY{n}{regressor}\PY{o}{.}\PY{n}{score}\PY{p}{(}\PY{n}{X\PYZus{}test}\PY{p}{,} \PY{n}{y\PYZus{}test}\PY{p}{)}
\PY{n+nb}{print}\PY{p}{(}\PY{n}{score}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Frozen : \PYZhy{}0.210135890125}
\PY{c+c1}{\PYZsh{} Grocery : 0.681884008544}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
-0.210135890125
\end{Verbatim}
\hypertarget{question-2}{%
\subsubsection{Question 2}\label{question-2}}
\begin{itemize}
\tightlist
\item
Which feature did you attempt to predict?
\item
What was the reported prediction score?
\item
Is this feature necessary for identifying customers' spending habits?
\end{itemize}
\textbf{Hint:} The coefficient of determination, \texttt{R\^{}2}, is
scored between 0 and 1, with 1 being a perfect fit. A negative
\texttt{R\^{}2} implies the model fails to fit the data. If you get a
low score for a particular feature, that lends us to beleive that that
feature point is hard to predict using the other features, thereby
making it an important feature to consider when considering relevance.
\textbf{Answer:}
I choose to predict \texttt{Frozen}. But the prdiction score is negative
\texttt{-0.210135890125}. Since \texttt{R\^{}2} score comes negative,
our model failes to fit the data. This implies that this feature frozen
is completely independent and don't have any relationship with other
features. Thus removing it, we are depriving the model from very
relevent information. This feature provides a lot of information gain.
We cannot fit the model with the data without this feature, expecting it
to be predicted. Thus it is necessary for identifying customers'
spending habits.
On the other hand, I also analysed the relationship of another feature
\texttt{Grocery} and found it to be having a comparatively good (can
say) relationship with other features as \texttt{R\^{}2} score is
\texttt{0.681884008544}. Thus this feature (if like to) can be removed
(not recommended) as it is not that much of critical value.
\hypertarget{visualize-feature-distributions}{%
\subsubsection{Visualize Feature
Distributions}\label{visualize-feature-distributions}}
To get a better understanding of the dataset, we can construct a scatter
matrix of each of the six product features present in the data. If you
found that the feature you attempted to predict above is relevant for
identifying a specific customer, then the scatter matrix below may not
show any correlation between that feature and the others. Conversely, if
you believe that feature is not relevant for identifying a specific
customer, the scatter matrix might show a correlation between that
feature and another feature in the data. Run the code block below to
produce a scatter matrix.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}62}]:} \PY{c+c1}{\PYZsh{} Produce a scatter matrix for each pair of features in the data}
\PY{n}{pd}\PY{o}{.}\PY{n}{plotting}\PY{o}{.}\PY{n}{scatter\PYZus{}matrix}\PY{p}{(}\PY{n}{data}\PY{p}{,} \PY{n}{alpha} \PY{o}{=} \PY{l+m+mf}{0.3}\PY{p}{,} \PY{n}{figsize} \PY{o}{=} \PY{p}{(}\PY{l+m+mi}{14}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{)}\PY{p}{,} \PY{n}{diagonal} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{kde}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{;}
\end{Verbatim}
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_15_0.png}
\end{center}
{ \hspace*{\fill} \\}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}63}]:} \PY{c+c1}{\PYZsh{} Getting the feature correlations and visualize them using a heatmap }
\PY{k+kn}{import} \PY{n+nn}{seaborn} \PY{k}{as} \PY{n+nn}{sns}
\PY{n}{sns}\PY{o}{.}\PY{n}{heatmap}\PY{p}{(}\PY{n}{data}\PY{o}{.}\PY{n}{corr}\PY{p}{(}\PY{p}{)}\PY{p}{,} \PY{n}{annot}\PY{o}{=}\PY{k+kc}{True}\PY{p}{)}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
{\color{outcolor}Out[{\color{outcolor}63}]:} <matplotlib.axes.\_subplots.AxesSubplot at 0x7f66684c45c0>
\end{Verbatim}
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_16_1.png}
\end{center}
{ \hspace*{\fill} \\}
\hypertarget{question-3}{%
\subsubsection{Question 3}\label{question-3}}
\begin{itemize}
\tightlist
\item
Using the scatter matrix as a reference, discuss the distribution of
the dataset, specifically talk about the normality, outliers, large
number of data points near 0 among others. If you need to sepearate
out some of the plots individually to further accentuate your point,
you may do so as well.
\item
Are there any pairs of features which exhibit some degree of
correlation?
\item
Does this confirm or deny your suspicions about the relevance of the
feature you attempted to predict?
\item
How is the data for those features distributed?
\end{itemize}
\textbf{Hint:} Is the data normally distributed? Where do most of the
data points lie? You can use
\href{https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.corr.html}{corr()}
to get the feature correlations and then visualize them using a
\href{http://seaborn.pydata.org/generated/seaborn.heatmap.html}{heatmap}
(the data that would be fed into the heatmap would be the correlation
values, for eg: \texttt{data.corr()}) to gain further insight.
\textbf{Answer:}
We can easily see in scatter plot as well as heat map, there are not
much of correlation between almost any features except \texttt{Grocery}
with \texttt{Milk} and \texttt{Detergent\_paper}. Here Grocery seems to
be sharing moderate relationship with milk having correlation score of
0.73 and apparently higher with detergents\_paper having correlation
score of 0.92. One among them can be excluded (only if required) without
losing much of information.
From the density scatter plot we can infer that the data graph for these
features are \texttt{highly\ skewed\ to\ the\ right} (positively skewed)
or are not normally distributed.
We can depict linear density distributon between features
\texttt{Grocery} and \texttt{Detergents\_Paper} from scatter plot which
shows that both are correlated and high correlation score from heat map
supports it. Also as predicted for previous question, feature
\texttt{Frozen} correlation scores with the other features are very low
or negative, meaning that there is almost no correlation between this
and other features.
All the graph shows high density of data close to 0, but few higher data
points points sparsely distributed. These data points seems to be
\texttt{outliers}.
\hypertarget{data-preprocessing}{%
\subsection{Data Preprocessing}\label{data-preprocessing}}
In this section, you will preprocess the data to create a better
representation of customers by performing a scaling on the data and
detecting (and optionally removing) outliers. Preprocessing data is
often times a critical step in assuring that results you obtain from
your analysis are significant and meaningful.
\hypertarget{implementation-feature-scaling}{%
\subsubsection{Implementation: Feature
Scaling}\label{implementation-feature-scaling}}
If data is not normally distributed, especially if the mean and median
vary significantly (indicating a large skew), it is most
\href{http://econbrowser.com/archives/2014/02/use-of-logarithms-in-economics}{often
appropriate} to apply a non-linear scaling --- particularly for
financial data. One way to achieve this scaling is by using a
\href{http://scipy.github.io/devdocs/generated/scipy.stats.boxcox.html}{Box-Cox
test}, which calculates the best power transformation of the data that
reduces skewness. A simpler approach which can work in most cases would
be applying the natural logarithm.
In the code block below, you will need to implement the following: -
Assign a copy of the data to \texttt{log\_data} after applying
logarithmic scaling. Use the \texttt{np.log} function for this. - Assign
a copy of the sample data to \texttt{log\_samples} after applying
logarithmic scaling. Again, use \texttt{np.log}.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}64}]:} \PY{c+c1}{\PYZsh{} TODO: Scale the data using the natural logarithm}
\PY{n}{log\PYZus{}data} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{log}\PY{p}{(}\PY{n}{data}\PY{p}{)}
\PY{c+c1}{\PYZsh{} TODO: Scale the sample data using the natural logarithm}
\PY{n}{log\PYZus{}samples} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{log}\PY{p}{(}\PY{n}{samples}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Produce a scatter matrix for each pair of newly\PYZhy{}transformed features}
\PY{n}{pd}\PY{o}{.}\PY{n}{plotting}\PY{o}{.}\PY{n}{scatter\PYZus{}matrix}\PY{p}{(}\PY{n}{log\PYZus{}data}\PY{p}{,} \PY{n}{alpha} \PY{o}{=} \PY{l+m+mf}{0.3}\PY{p}{,} \PY{n}{figsize} \PY{o}{=} \PY{p}{(}\PY{l+m+mi}{14}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{)}\PY{p}{,} \PY{n}{diagonal} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{kde}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{;}
\end{Verbatim}
\begin{center}
\adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{output_21_0.png}
\end{center}
{ \hspace*{\fill} \\}
\hypertarget{observation}{%
\subsubsection{Observation}\label{observation}}
After applying a natural logarithm scaling to the data, the distribution
of each feature should appear much more normal. For any pairs of
features you may have identified earlier as being correlated, observe
here whether that correlation is still present (and whether it is now
stronger or weaker than before).
Run the code below to see how the sample data has changed after having
the natural logarithm applied to it.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}65}]:} \PY{c+c1}{\PYZsh{} Display the log\PYZhy{}transformed sample data}
\PY{n}{display}\PY{p}{(}\PY{n}{log\PYZus{}samples}\PY{p}{)}
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
0 8.861775 9.191158 9.166179 7.474205 8.099554 7.482119
1 9.483873 7.024649 8.416931 7.258412 6.308098 6.208590
2 9.439784 9.315961 9.773891 6.690842 8.772920 7.325808
\end{verbatim}
\hypertarget{implementation-outlier-detection}{%
\subsubsection{Implementation: Outlier
Detection}\label{implementation-outlier-detection}}
Detecting outliers in the data is extremely important in the data
preprocessing step of any analysis. The presence of outliers can often
skew results which take into consideration these data points. There are
many ``rules of thumb'' for what constitutes an outlier in a dataset.
Here, we will use
\href{http://datapigtechnologies.com/blog/index.php/highlighting-outliers-in-your-data-with-the-tukey-method/}{Tukey's
Method for identfying outliers}: An \emph{outlier step} is calculated as
1.5 times the interquartile range (IQR). A data point with a feature
that is beyond an outlier step outside of the IQR for that feature is
considered abnormal.
In the code block below, you will need to implement the following: -
Assign the value of the 25th percentile for the given feature to
\texttt{Q1}. Use \texttt{np.percentile} for this. - Assign the value of
the 75th percentile for the given feature to \texttt{Q3}. Again, use
\texttt{np.percentile}. - Assign the calculation of an outlier step for
the given feature to \texttt{step}. - Optionally remove data points from
the dataset by adding indices to the \texttt{outliers} list.
\textbf{NOTE:} If you choose to remove any outliers, ensure that the
sample data does not contain any of these points!\\
Once you have performed this implementation, the dataset will be stored
in the variable \texttt{good\_data}.
\begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}66}]:} \PY{k+kn}{from} \PY{n+nn}{collections} \PY{k}{import} \PY{n}{Counter}
\PY{c+c1}{\PYZsh{} For each feature find the data points with extreme high or low values}
\PY{n}{outliers} \PY{o}{=} \PY{p}{[}\PY{p}{]}
\PY{k}{for} \PY{n}{feature} \PY{o+ow}{in} \PY{n}{log\PYZus{}data}\PY{o}{.}\PY{n}{keys}\PY{p}{(}\PY{p}{)}\PY{p}{:}
\PY{c+c1}{\PYZsh{} TODO: Calculate Q1 (25th percentile of the data) for the given feature}
\PY{n}{Q1} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{percentile}\PY{p}{(}\PY{n}{log\PYZus{}data}\PY{p}{[}\PY{n}{feature}\PY{p}{]}\PY{p}{,} \PY{l+m+mi}{25}\PY{p}{)}
\PY{c+c1}{\PYZsh{} TODO: Calculate Q3 (75th percentile of the data) for the given feature}
\PY{n}{Q3} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{percentile}\PY{p}{(}\PY{n}{log\PYZus{}data}\PY{p}{[}\PY{n}{feature}\PY{p}{]}\PY{p}{,} \PY{l+m+mi}{75}\PY{p}{)}
\PY{c+c1}{\PYZsh{} TODO: Use the interquartile range to calculate an outlier step (1.5 times the interquartile range)}
\PY{n}{step} \PY{o}{=} \PY{p}{(}\PY{n}{Q3} \PY{o}{\PYZhy{}} \PY{n}{Q1}\PY{p}{)} \PY{o}{*} \PY{l+m+mf}{1.5}
\PY{c+c1}{\PYZsh{} Display the outliers}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Data points considered outliers for the feature }\PY{l+s+s2}{\PYZsq{}}\PY{l+s+si}{\PYZob{}\PYZcb{}}\PY{l+s+s2}{\PYZsq{}}\PY{l+s+s2}{:}\PY{l+s+s2}{\PYZdq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{feature}\PY{p}{)}\PY{p}{)}
\PY{n}{display}\PY{p}{(}\PY{n}{log\PYZus{}data}\PY{p}{[}\PY{o}{\PYZti{}}\PY{p}{(}\PY{p}{(}\PY{n}{log\PYZus{}data}\PY{p}{[}\PY{n}{feature}\PY{p}{]} \PY{o}{\PYZgt{}}\PY{o}{=} \PY{n}{Q1} \PY{o}{\PYZhy{}} \PY{n}{step}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{log\PYZus{}data}\PY{p}{[}\PY{n}{feature}\PY{p}{]} \PY{o}{\PYZlt{}}\PY{o}{=} \PY{n}{Q3} \PY{o}{+} \PY{n}{step}\PY{p}{)}\PY{p}{)}\PY{p}{]}\PY{p}{)}
\PY{n}{feature\PYZus{}outliers} \PY{o}{=} \PY{n}{log\PYZus{}data}\PY{o}{.}\PY{n}{index}\PY{p}{[}\PY{o}{\PYZti{}}\PY{p}{(}\PY{p}{(}\PY{n}{log\PYZus{}data}\PY{p}{[}\PY{n}{feature}\PY{p}{]} \PY{o}{\PYZgt{}}\PY{o}{=} \PY{n}{Q1} \PY{o}{\PYZhy{}} \PY{n}{step}\PY{p}{)} \PY{o}{\PYZam{}} \PY{p}{(}\PY{n}{log\PYZus{}data}\PY{p}{[}\PY{n}{feature}\PY{p}{]} \PY{o}{\PYZlt{}}\PY{o}{=} \PY{n}{Q3} \PY{o}{+} \PY{n}{step}\PY{p}{)}\PY{p}{)} \PY{o}{==} \PY{k+kc}{True}\PY{p}{]}\PY{o}{.}\PY{n}{tolist}\PY{p}{(}\PY{p}{)}
\PY{k}{for} \PY{n}{outlier} \PY{o+ow}{in} \PY{n}{feature\PYZus{}outliers}\PY{p}{:} \PY{n}{outliers}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{outlier}\PY{p}{)}
\PY{c+c1}{\PYZsh{} OPTIONAL: Select the indices for data points you wish to remove}
\PY{n}{count} \PY{o}{=} \PY{n}{Counter}\PY{p}{(}\PY{n}{outliers}\PY{p}{)}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{Total outliers:}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n+nb}{format}\PY{p}{(}\PY{n}{Counter}\PY{p}{(}\PY{n}{outliers}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{The total number of outliers from every features is:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n+nb}{format}\PY{p}{(}\PY{n+nb}{sum}\PY{p}{(}\PY{n}{Counter}\PY{p}{(}\PY{n}{outliers}\PY{p}{)}\PY{o}{.}\PY{n}{values}\PY{p}{(}\PY{p}{)}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\PY{n}{repeated\PYZus{}outliers} \PY{o}{=} \PY{n}{Counter}\PY{p}{(}\PY{n}{el} \PY{k}{for} \PY{n}{el} \PY{o+ow}{in} \PY{n}{count}\PY{o}{.}\PY{n}{elements}\PY{p}{(}\PY{p}{)} \PY{k}{if} \PY{n}{count}\PY{p}{[}\PY{n}{el}\PY{p}{]} \PY{o}{\PYZgt{}} \PY{l+m+mi}{1}\PY{p}{)}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{The number of repeating outliers is:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n+nb}{format}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n}{repeated\PYZus{}outliers}\PY{p}{)}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Repeated outliers:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n+nb}{format}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n}{repeated\PYZus{}outliers}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\PY{n}{display}\PY{p}{(}\PY{n}{log\PYZus{}data}\PY{o}{.}\PY{n}{iloc}\PY{p}{[}\PY{n+nb}{list}\PY{p}{(}\PY{n}{repeated\PYZus{}outliers}\PY{p}{)}\PY{p}{]}\PY{p}{)}
\PY{c+c1}{\PYZsh{} Remove the outliers, if any were specified}
\PY{n}{good\PYZus{}data} \PY{o}{=} \PY{n}{log\PYZus{}data}\PY{o}{.}\PY{n}{drop}\PY{p}{(}\PY{n}{log\PYZus{}data}\PY{o}{.}\PY{n}{index}\PY{p}{[}\PY{n}{outliers}\PY{p}{]}\PY{p}{)}\PY{o}{.}\PY{n}{reset\PYZus{}index}\PY{p}{(}\PY{n}{drop} \PY{o}{=} \PY{k+kc}{True}\PY{p}{)}
\PY{c+c1}{\PYZsh{} printing the shape of data without outliers}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{The shape of data without outliers is:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n+nb}{format}\PY{p}{(}\PY{n}{good\PYZus{}data}\PY{o}{.}\PY{n}{shape}\PY{p}{)}\PY{p}{)}
\end{Verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
Data points considered outliers for the feature 'Fresh':
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
65 4.442651 9.950323 10.732651 3.583519 10.095388 7.260523
66 2.197225 7.335634 8.911530 5.164786 8.151333 3.295837
81 5.389072 9.163249 9.575192 5.645447 8.964184 5.049856
95 1.098612 7.979339 8.740657 6.086775 5.407172 6.563856
96 3.135494 7.869402 9.001839 4.976734 8.262043 5.379897
128 4.941642 9.087834 8.248791 4.955827 6.967909 1.098612
171 5.298317 10.160530 9.894245 6.478510 9.079434 8.740337
193 5.192957 8.156223 9.917982 6.865891 8.633731 6.501290
218 2.890372 8.923191 9.629380 7.158514 8.475746 8.759669
304 5.081404 8.917311 10.117510 6.424869 9.374413 7.787382
305 5.493061 9.468001 9.088399 6.683361 8.271037 5.351858
338 1.098612 5.808142 8.856661 9.655090 2.708050 6.309918
353 4.762174 8.742574 9.961898 5.429346 9.069007 7.013016
355 5.247024 6.588926 7.606885 5.501258 5.214936 4.844187
357 3.610918 7.150701 10.011086 4.919981 8.816853 4.700480
412 4.574711 8.190077 9.425452 4.584967 7.996317 4.127134
\end{verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
Data points considered outliers for the feature 'Milk':
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
86 10.039983 11.205013 10.377047 6.894670 9.906981 6.805723
98 6.220590 4.718499 6.656727 6.796824 4.025352 4.882802
154 6.432940 4.007333 4.919981 4.317488 1.945910 2.079442
356 10.029503 4.897840 5.384495 8.057377 2.197225 6.306275
\end{verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
Data points considered outliers for the feature 'Grocery':
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
75 9.923192 7.036148 1.098612 8.390949 1.098612 6.882437
154 6.432940 4.007333 4.919981 4.317488 1.945910 2.079442
\end{verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
Data points considered outliers for the feature 'Frozen':
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
38 8.431853 9.663261 9.723703 3.496508 8.847360 6.070738
57 8.597297 9.203618 9.257892 3.637586 8.932213 7.156177
65 4.442651 9.950323 10.732651 3.583519 10.095388 7.260523
145 10.000569 9.034080 10.457143 3.737670 9.440738 8.396155
175 7.759187 8.967632 9.382106 3.951244 8.341887 7.436617
264 6.978214 9.177714 9.645041 4.110874 8.696176 7.142827
325 10.395650 9.728181 9.519735 11.016479 7.148346 8.632128
420 8.402007 8.569026 9.490015 3.218876 8.827321 7.239215
429 9.060331 7.467371 8.183118 3.850148 4.430817 7.824446
439 7.932721 7.437206 7.828038 4.174387 6.167516 3.951244
\end{verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
Data points considered outliers for the feature 'Detergents\_Paper':
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
75 9.923192 7.036148 1.098612 8.390949 1.098612 6.882437
161 9.428190 6.291569 5.645447 6.995766 1.098612 7.711101
\end{verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
Data points considered outliers for the feature 'Delicatessen':
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen Detergents_Paper \
66 2.197225 7.335634 8.911530 5.164786 8.151333
109 7.248504 9.724899 10.274568 6.511745 6.728629
128 4.941642 9.087834 8.248791 4.955827 6.967909
137 8.034955 8.997147 9.021840 6.493754 6.580639
142 10.519646 8.875147 9.018332 8.004700 2.995732
154 6.432940 4.007333 4.919981 4.317488 1.945910
183 10.514529 10.690808 9.911952 10.505999 5.476464
184 5.789960 6.822197 8.457443 4.304065 5.811141
187 7.798933 8.987447 9.192075 8.743372 8.148735
203 6.368187 6.529419 7.703459 6.150603 6.860664
233 6.871091 8.513988 8.106515 6.842683 6.013715
285 10.602965 6.461468 8.188689 6.948897 6.077642
289 10.663966 5.655992 6.154858 7.235619 3.465736
343 7.431892 8.848509 10.177932 7.283448 9.646593
Delicatessen
66 3.295837
109 1.098612
128 1.098612
137 3.583519
142 1.098612
154 2.079442
183 10.777768
184 2.397895
187 1.098612
203 2.890372
233 1.945910
285 2.890372
289 3.091042
343 3.610918
\end{verbatim}
\begin{Verbatim}[commandchars=\\\{\}]
Total outliers:
Counter(\{154: 3, 65: 2, 66: 2, 128: 2, 75: 2, 81: 1, 95: 1, 96: 1, 171: 1, 193: 1, 218: 1, 304: 1, 305: 1, 338: 1, 353: 1, 355: 1, 357: 1, 412: 1, 86: 1, 98: 1, 356: 1, 38: 1, 57: 1, 145: 1, 175: 1, 264: 1, 325: 1, 420: 1, 429: 1, 439: 1, 161: 1, 109: 1, 137: 1, 142: 1, 183: 1, 184: 1, 187: 1, 203: 1, 233: 1, 285: 1, 289: 1, 343: 1\})
The total number of outliers from every features is: 48
The number of repeating outliers is: 5
Repeated outliers: [65, 66, 128, 154, 75]
\end{Verbatim}
\begin{verbatim}
Fresh Milk Grocery Frozen Detergents_Paper Delicatessen
65 4.442651 9.950323 10.732651 3.583519 10.095388 7.260523
66 2.197225 7.335634 8.911530 5.164786 8.151333 3.295837
128 4.941642 9.087834 8.248791 4.955827 6.967909 1.098612
154 6.432940 4.007333 4.919981 4.317488 1.945910 2.079442
75 9.923192 7.036148 1.098612 8.390949 1.098612 6.882437
\end{verbatim}