-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_ISLR.html
7920 lines (6808 loc) · 416 KB
/
python_ISLR.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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2020-08-19 Wed 08:09 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>A Python Companion to ISLR</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Naresh Gurbuxani" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2019 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "center",
displayIndent: "0em",
"HTML-CSS": { scale: 100,
linebreaks: { automatic: "false" },
webFont: "TeX"
},
SVG: {scale: 100,
linebreaks: { automatic: "false" },
font: "TeX"},
NativeMML: {scale: 100},
TeX: { equationNumbers: {autoNumber: "AMS"},
MultLineWidth: "85%",
TagSide: "right",
TagIndent: ".8em"
}
});
</script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="content">
<h1 class="title">A Python Companion to ISLR</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org51a3d92">1. Introduction</a></li>
<li><a href="#orge18c8df">2. Statistical Learning</a>
<ul>
<li><a href="#orge487959">2.1. What is Statistical Learning?</a></li>
<li><a href="#orgeb9d610">2.2. Assessing Model Accuracy</a></li>
<li><a href="#org13974b2">2.3. Lab: Introduction to Python</a>
<ul>
<li><a href="#org2037bff">2.3.1. Basic Commands</a></li>
<li><a href="#orgb312b27">2.3.2. Graphics</a></li>
<li><a href="#org5e49b84">2.3.3. Indexing Data</a></li>
<li><a href="#orgd28dc33">2.3.4. Loading Data</a></li>
<li><a href="#org845481c">2.3.5. Additional Graphical and Numerical Summaries</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org0d58fd3">3. Linear Regression</a>
<ul>
<li><a href="#org0c24ba2">3.1. Simple Linear Regression</a></li>
<li><a href="#org8d955fe">3.2. Multiple Linear Regression</a></li>
<li><a href="#orge002d68">3.3. Other Considerations in the Regression Model</a></li>
<li><a href="#orge5b7046">3.4. The Marketing Plan</a></li>
<li><a href="#orge13beec">3.5. Comparison of Linear Regression with K-Nearest Neighbors</a></li>
<li><a href="#org3834f17">3.6. Lab: Linear Regression</a>
<ul>
<li><a href="#org42774ca">3.6.1. Libraries</a></li>
<li><a href="#orgef16a59">3.6.2. Simple Linear Regression</a></li>
<li><a href="#org3f68780">3.6.3. Multiple Linear Regression</a></li>
<li><a href="#org3477c07">3.6.4. Interaction Terms</a></li>
<li><a href="#org05f453d">3.6.5. Non-linear Transformations of the Predictors</a></li>
<li><a href="#orgd0f74b1">3.6.6. Qualitative Predictors</a></li>
<li><a href="#orgd293012">3.6.7. Calling <code>R</code> from <code>Python</code></a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org1fea058">4. Classification</a>
<ul>
<li><a href="#org8c46dbf">4.1. An Overview of Classification</a></li>
<li><a href="#org9bda174">4.2. Why Not Linear Regression?</a></li>
<li><a href="#orgda2ee54">4.3. Logistic Regression</a></li>
<li><a href="#orga747611">4.4. Linear Discriminant Analysis</a></li>
<li><a href="#orgf4eb929">4.5. A Comparison of Classification Methods</a></li>
<li><a href="#org1b0d56b">4.6. Lab: Logistic Regression, LDA, QDA, and KNN</a>
<ul>
<li><a href="#org9de03ac">4.6.1. The Stock Market Data</a></li>
<li><a href="#org71ef131">4.6.2. Logistc Regression</a></li>
<li><a href="#orgbcfd3bf">4.6.3. Linear Discriminant Analysis</a></li>
<li><a href="#org1d9b702">4.6.4. Quadratic Discriminant Analysis</a></li>
<li><a href="#orge5a8491">4.6.5. K-Nearest Neightbors</a></li>
<li><a href="#org790d805">4.6.6. An Application to Caravan Insurance Data</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org6c34bb7">5. Resampling Methods</a>
<ul>
<li><a href="#org3d5343e">5.1. Cross-Validation</a></li>
<li><a href="#org4fb86ce">5.2. The Bootstrap</a></li>
<li><a href="#org0379a7d">5.3. Lab: Cross-Validation and the Bootstrap</a>
<ul>
<li><a href="#org8dcc4da">5.3.1. The Validation Set Approach</a></li>
<li><a href="#org3c66e7c">5.3.2. Leave-One-Out Cross-Validation</a></li>
<li><a href="#orgafb980e">5.3.3. k-Fold Cross-Validation</a></li>
<li><a href="#orge2c382c">5.3.4. The Bootstrap</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org104cb2b">6. Linear Model Selection and Regularization</a>
<ul>
<li><a href="#org28de162">6.1. Subset Selection</a></li>
<li><a href="#orgc55df27">6.2. Shrinkage Methods</a></li>
<li><a href="#org2c9ebf8">6.3. Dimension Reduction Methods</a></li>
<li><a href="#org27036ec">6.4. Considerations in High Dimensions</a></li>
<li><a href="#org6b80217">6.5. Lab 1: Subset Selection Methods</a>
<ul>
<li><a href="#org20f73c1">6.5.1. Best Subset Selection</a></li>
<li><a href="#orgba9499e">6.5.2. Forward and Backward Stepwise Selection</a></li>
<li><a href="#orga5f0ec4">6.5.3. Choosing Among Models Using the Validation Set Approach and Cross-Validation</a></li>
</ul>
</li>
<li><a href="#orga1868ca">6.6. Lab 2: Ridge Regression and the Lasso</a>
<ul>
<li><a href="#orgef6a96f">6.6.1. Ridge Regression</a></li>
<li><a href="#org70f708b">6.6.2. The Lasso</a></li>
</ul>
</li>
<li><a href="#org3ee433d">6.7. Lab 3: PCR and PLS Regression</a>
<ul>
<li><a href="#org96f12f7">6.7.1. Principal Components Regression</a></li>
<li><a href="#orga100fd3">6.7.2. Partial Least Squares</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org2bdd141">7. Moving Beyond Linearity</a>
<ul>
<li><a href="#org8076280">7.1. Polynomial Regression</a></li>
<li><a href="#org324bcc2">7.2. Step Functions</a></li>
<li><a href="#org7acaba5">7.3. Basis Functions</a></li>
<li><a href="#org8c2eca0">7.4. Regression Splines</a></li>
<li><a href="#org3bb9aaf">7.5. Lab: Non-linear Modeling</a>
<ul>
<li><a href="#orgac959b8">7.5.1. Polynomial Regression and Step Functions</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgd2c3957">8. Tree-Based Models</a>
<ul>
<li><a href="#orgf4b805e">8.1. The Basics of Decision Trees</a>
<ul>
<li><a href="#org0112364">8.1.1. Regression Trees</a></li>
<li><a href="#orge46b2de">8.1.2. Classification Trees</a></li>
<li><a href="#org0bf0e9f">8.1.3. Trees versus Linear Models</a></li>
</ul>
</li>
<li><a href="#org60ad82b">8.2. Bagging, Random Forests, Boosting</a></li>
<li><a href="#org91a2f29">8.3. Lab: Decision Trees</a>
<ul>
<li><a href="#org8aae0c0">8.3.1. Fitting Classification Trees</a></li>
<li><a href="#org434f007">8.3.2. Fitting Regression Trees</a></li>
<li><a href="#org04ccd68">8.3.3. Bagging and Random Forests</a></li>
<li><a href="#org06402c2">8.3.4. Boosting</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgd98a3f9">9. Support Vector Machines</a>
<ul>
<li><a href="#org6220cf1">9.1. Maximal Margin Classifier</a>
<ul>
<li><a href="#org5e3604c">9.1.1. What is a Hyperplane?</a></li>
<li><a href="#org05ccb32">9.1.2. Classification Using a Separating Hyperplane</a></li>
<li><a href="#orgf3614f9">9.1.3. The Maximal Margin Classifier</a></li>
<li><a href="#org5d157d6">9.1.4. Construction of the Maximal Margin Classifier</a></li>
<li><a href="#org6025292">9.1.5. The Non-separable Case</a></li>
</ul>
</li>
<li><a href="#org1d8a47d">9.2. Support Vector Classifiers</a>
<ul>
<li><a href="#org9046c36">9.2.1. Overview of the Support Vector Classifier</a></li>
<li><a href="#orge4e3096">9.2.2. Details of the Support Vector Classifier</a></li>
</ul>
</li>
<li><a href="#org1933591">9.3. Support Vector Machines</a>
<ul>
<li><a href="#org513f10d">9.3.1. Classification with Non-linear Decision Boundaries</a></li>
<li><a href="#org4a732e4">9.3.2. The Support Vector Machine</a></li>
<li><a href="#org58a8f30">9.3.3. An Application to the Heart Disease Data</a></li>
</ul>
</li>
<li><a href="#orga42ffa9">9.4. SVMs with More than Two Classes</a>
<ul>
<li><a href="#org639a905">9.4.1. One-Versus-One Classification</a></li>
<li><a href="#org8e5bf9e">9.4.2. One-Versus-All Classification</a></li>
</ul>
</li>
<li><a href="#org7b0e323">9.5. Relationship with Logistic Regression</a></li>
<li><a href="#org65da4b4">9.6. Lab: Support Vector Machines</a>
<ul>
<li><a href="#org914fad5">9.6.1. Support Vector Classifier</a></li>
<li><a href="#org5c3cdef">9.6.2. Support Vector Machine</a></li>
<li><a href="#org86df7af">9.6.3. ROC Curves</a></li>
<li><a href="#org1bf1cd1">9.6.4. SVM with Multiple Classes</a></li>
<li><a href="#orgb372ca7">9.6.5. Application to Gene Expression Data</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org79ac5fb">10. Unsupervised Learning</a>
<ul>
<li><a href="#orgbec34e7">10.1. The Challenge of Unsupervised Learning</a></li>
<li><a href="#org3b3f9b3">10.2. Principal Component Analysis</a>
<ul>
<li><a href="#orgc831dc8">10.2.1. What are Principal Components?</a></li>
<li><a href="#org1f1971c">10.2.2. Another Interpretation of Principal Components</a></li>
<li><a href="#org9d74a40">10.2.3. More on PCA</a></li>
</ul>
</li>
<li><a href="#org202cb85">10.3. Clustering Methods</a>
<ul>
<li><a href="#org4f92fc9">10.3.1. K-Means Clustering</a></li>
<li><a href="#orgfa686cb">10.3.2. Hierarchical Clustering</a></li>
</ul>
</li>
<li><a href="#org9cfd91f">10.4. Lab 1: Principal Components Analysis</a></li>
<li><a href="#org2e7b44a">10.5. Lab 2: Clustering</a>
<ul>
<li><a href="#org5f24036">10.5.1. K-Means Clustering</a></li>
<li><a href="#org9cb02e0">10.5.2. Hierarchical Clustering</a></li>
</ul>
</li>
<li><a href="#orgd639e92">10.6. Lab 3: NCI60 Data Example</a>
<ul>
<li><a href="#org2247c90">10.6.1. PCA on NCI60 Data</a></li>
<li><a href="#orgaadd3da">10.6.2. Clustering the Observations of the NCI60 Data</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-org51a3d92" class="outline-2">
<h2 id="org51a3d92"><span class="section-number-2">1</span> Introduction</h2>
<div class="outline-text-2" id="text-1">
<p>
Figure <a href="#orgce502d8">1</a> shows graphs of Wage versus three variables.
</p>
<div id="orgce502d8" class="figure">
<p><img src="figures/fig1_1.png" alt="fig1_1.png" />
</p>
<p><span class="figure-number">Figure 1: </span><code>Wage</code> data, which contains income survey information for males from the central Atlantic region of the United States. Left: <code>wage</code> as a function of <code>age</code>. On average, <code>wage</code> increases with <code>age</code> until about 60 years of age, at which point it begins to decline. Center: <code>wage</code> as a function of <code>year</code>. There is a slow but steady increase of approximately $10,000 in the average <code>wage</code> between 2003 and 2009. Right: Boxplots displaying <code>wage</code> as a function of <code>education</code>, with 1 indicating the lowest level (no highschool diploma) and 5 the highest level (an advanced graduate degree). On average, <code>wage</code> increases with the level of <code>education</code>.</p>
</div>
<p>
Figure <a href="#orgc0f5bc5">2</a> shows boxplots of previous days' percentage changes in S&P
500 grouped according to today's change <code>Up</code> or <code>Down</code>.
</p>
<div id="orgc0f5bc5" class="figure">
<p><img src="figures/fig1_2.png" alt="fig1_2.png" />
</p>
<p><span class="figure-number">Figure 2: </span>Left: Boxplots of the previous day's percentage change in the S&P 500 index for the days for which the market increased or decreased, obtained from the <code>Smarket</code> data. Center and Right: Same as left panel, but the percentage changes for two and three days previous are shown.</p>
</div>
</div>
</div>
<div id="outline-container-orge18c8df" class="outline-2">
<h2 id="orge18c8df"><span class="section-number-2">2</span> Statistical Learning</h2>
<div class="outline-text-2" id="text-2">
<p>
<a id="orgce1fc8b"></a>
</p>
</div>
<div id="outline-container-orge487959" class="outline-3">
<h3 id="orge487959"><span class="section-number-3">2.1</span> What is Statistical Learning?</h3>
<div class="outline-text-3" id="text-2-1">
<p>
Figure <a href="#org8710e1d">3</a> shows scatter plots of <code>sales</code> versus <code>TV</code>, <code>radio</code>,
and <code>newspaper</code> advertising. In each panel, the figure also includes an OLS
regression line.
</p>
<div id="org8710e1d" class="figure">
<p><img src="figures/fig2_1.png" alt="fig2_1.png" />
</p>
<p><span class="figure-number">Figure 3: </span>The <code>Advertising</code> data set. The plot displays <code>sales</code>, in thousands of units, as a function of <code>TV</code>, <code>radio</code>, and <code>newspaper</code> budgets, in thousands of dollars, for 200 different markets. In each plot we show the simple least squares fit of <code>sales</code> to that variable. In other words, each red line represents a simple model that can be used to predict <code>sales</code> using <code>TV</code>, <code>radio</code>, and <code>newspaper</code>, respectively.</p>
</div>
<p>
Figure <a href="#orga8a0b15">4</a> is a plot of <code>Income</code> versus <code>Years of Education</code> from the
Income data set. In the left panel, the ``true'' function (given by blue line)
is actually my guess.
</p>
<div id="orga8a0b15" class="figure">
<p><img src="figures/fig2_2.png" alt="fig2_2.png" />
</p>
<p><span class="figure-number">Figure 4: </span>The <code>Income</code> data set. Left: The red dots are the observed values of <code>income</code> (in tens of thousands of dollars) and <code>years of education</code> for 30 individuals. Right: The blue curve represents the true underlying relationship between <code>income</code> and <code>years of education</code>, which is generally unknown (but is known in this case because the data are simulated). The vertical lines represent the error associated with each observation. Note that some of the errors are positive (when an observation lies above the blue curve) and some are negative (when an observation lies below the curve). Overall, these errors have approximately mean zero.</p>
</div>
<p>
Figure <a href="#orgdf20213">5</a> is a plot of <code>Income</code> versus <code>Years of Education</code> and
<code>Seniority</code> from the <code>Income</code> data set. Since the book does not provide the
true values of <code>Income</code>, ``true'' values shown in the plot are actually third
order polynomial fit.
</p>
<div id="orgdf20213" class="figure">
<p><img src="figures/fig2_3.png" alt="fig2_3.png" />
</p>
<p><span class="figure-number">Figure 5: </span>The plot displays <code>income</code> as a function of <code>years of education</code> and <code>seniority</code> in the <code>Income</code> data set. The blue surface represents the true underlying relationship between <code>income</code> and <code>years of education</code> and <code>seniority</code>, which is known since the data are simulated. The red dots indicate the observed values of these quantities for 30 individuals.</p>
</div>
<p>
Figure <a href="#org4581c1a">6</a> shows an example of the parametric approach applied to
the <code>Income</code> data from previous figure.
</p>
<div id="org4581c1a" class="figure">
<p><img src="figures/fig2_4.png" alt="fig2_4.png" />
</p>
<p><span class="figure-number">Figure 6: </span>A linear model fit by least squares to the <code>Income</code> data from figure <a href="#orgdf20213">5</a>. The observations are shown in red, and the blue plane indicates the least squares fit to the data.</p>
</div>
<p>
Figure <a href="#org0f419f0">7</a> provides an illustration of the trade-off between
flexibility and interpretability for some of the methods covered in this book.
</p>
<div id="org0f419f0" class="figure">
<p><img src="figures/figure2_7.png" alt="figure2_7.png" />
</p>
<p><span class="figure-number">Figure 7: </span>A representation of the tradeoff between flexibility and interpretability, using different statistical learning methods. In general, as the flexibility of a method increases, its interpretability decreases.</p>
</div>
<p>
Figure <a href="#org3614b05">8</a> provides a simple illustration of the clustering problem.
</p>
<div id="org3614b05" class="figure">
<p><img src="figures/fig2_8.png" alt="fig2_8.png" />
</p>
<p><span class="figure-number">Figure 8: </span>A clustering data set involving three groups. Each group is shown using a different colored symbol. Left: The three groups are well-separated. In this setting, a clustering approach should successfully identify the three groups. Right: There is some overlap among the groups. Now the clustering taks is more challenging.</p>
</div>
</div>
</div>
<div id="outline-container-orgeb9d610" class="outline-3">
<h3 id="orgeb9d610"><span class="section-number-3">2.2</span> Assessing Model Accuracy</h3>
<div class="outline-text-3" id="text-2-2">
<p>
Figure <a href="#org43f7e4e">9</a> illustrates the tradeoff between training MSE and test
MSE. We select a ``true function'' whose shape is similar to that shown in the
book. In the left panel, the orange, blue, and green curves illustrate three possible estimates
for \(f\) given by the black curve. The orange line is the linear regression
fit, which is relatively inflexible. The blue and green curves were produced
using <i>smoothing splines</i> from <code>UnivariateSpline</code> function in <code>scipy</code> package.
We obtain different levels of flexibility by varying the parameter <code>s</code>, which
affects the number of knots.
</p>
<p>
For the right panel, we have chosen polynomial fits. The degree of polynomial
represents the level of flexibility. This is because the function
<code>UnivariateSpline</code> does not more than five degrees of freedom.
</p>
<p>
When we repeat the simulations for figure <a href="#org43f7e4e">9</a>, we see considerable
variation in the right panel MSE plots. But the overall conclusion remains the
same.
</p>
<div id="org43f7e4e" class="figure">
<p><img src="figures/fig2_9.png" alt="fig2_9.png" />
</p>
<p><span class="figure-number">Figure 9: </span>Left: Data simulated from \(f\), shown in black. Three estimates of \(f\) are shown: the linear regression line (orange curve), and two smoothing spline fits (blue and green curves). Right: Training MSE (grey curve), test MSE (red curve), and minimum possible test MSE over all methods (dashed grey line).</p>
</div>
<p>
Figure <a href="#org9a4ea7b">10</a> provides another example in which the true \(f\) is
approximately linear.
</p>
<div id="org9a4ea7b" class="figure">
<p><img src="figures/fig2_10.png" alt="fig2_10.png" />
</p>
<p><span class="figure-number">Figure 10: </span>Details are as in figure <a href="#org43f7e4e">9</a> using a different true \(f\) that is much closer to linear. In this setting, linear regression provides a very good fit to the data.</p>
</div>
<p>
Figure <a href="#orgb45f0cf">11</a> displays an example in which \(f\) is highly
non-linear. The training and test MSE curves still exhibit the same general
patterns.
</p>
<div id="orgb45f0cf" class="figure">
<p><img src="figures/fig2_11.png" alt="fig2_11.png" />
</p>
<p><span class="figure-number">Figure 11: </span>Details are as in figure <a href="#org43f7e4e">9</a>, using a different \(f\) that is far from linear. In this setting, linear regression provides a very poor fit to the data.</p>
</div>
<p>
Figure <a href="#org25bb645">12</a> displays the relationship between bias, variance, and
test MSE. This relationship is referred to as <i>bias-variance trade-off</i>. When
simulations are repeated, we see considerable variation in different graphs,
especially for MSE lines. But overall shape remains the same.
</p>
<div id="org25bb645" class="figure">
<p><img src="figures/fig2_12.png" alt="fig2_12.png" />
</p>
<p><span class="figure-number">Figure 12: </span>Squared bias (blue curve), variance (orange curve), \(Var(\epsilon)\) (dashed line), and test MSE (red curve) for the three data sets in figures <a href="#org43f7e4e">9</a> - <a href="#orgb45f0cf">11</a>. The vertical dotted line indicates the flexibility level corresponding to the smallest test MSE.</p>
</div>
<p>
Figure <a href="#org0d2d113">13</a> provides an example using a simulated data set in
two-dimensional space consisting of predictors \(X_1\) and \(X_2\).
</p>
<div id="org0d2d113" class="figure">
<p><img src="figures/fig2_13.png" alt="fig2_13.png" />
</p>
<p><span class="figure-number">Figure 13: </span>A simulated data set consisting of 200 observations in two groups, indicated in blue and orange. The dashed line represents the Bayes decision boundary. The orange background grid indicates the region in which a test observation will be assigned to the orange class, and blue background grid indicates the region in which a test observation will be assigned to the blue class.</p>
</div>
<p>
Figure <a href="#org4f7b532">14</a> displays the KNN decision boundary, using \(K=10\), when
applied to the simulated data set from figure <a href="#org0d2d113">13</a>. Even though
the true distribution is not known by the KNN classifier, the KNN decision
making boundary is very close to that of the Bayes classifier.
</p>
<div id="org4f7b532" class="figure">
<p><img src="figures/fig2_15.png" alt="fig2_15.png" />
</p>
<p><span class="figure-number">Figure 14: </span>The firm line indicates the KNN decision boundary on the data from figure <a href="#org0d2d113">13</a>, using \(K = 10\). The Bayes decision boundary is shown as a dashed line. The KNN and Bayes decision boundaries are very similar.</p>
</div>
<div id="orgeee6d4c" class="figure">
<p><img src="figures/fig2_16.png" alt="fig2_16.png" />
</p>
<p><span class="figure-number">Figure 15: </span>A comparison of the KNN decision boundaries (solid curves) obtained using \(K=1\) and \(K=100\) on the data from figure <a href="#org0d2d113">13</a>. With \(K=1\), the decision boundary is overly flexible, while with \(K=100\) it is not sufficiently flexible. The Bayes decision boundary is shown as dashed line.</p>
</div>
<p>
In figure <a href="#orgff201e6">16</a> we have plotted the KNN test and training errors as
a function of \(\frac{1}{K}\). As \(\frac{1}{K}\) increases, the method becomes
more flexible. As in the regression setting, the training error rate
consistently declines as the flexibility increases. However, the test error
exhibits the characteristic U-shape, declining at first (with a minimum at
approximately \(K=10\)) before increasing again when the method becomes
excessively flexible and overfits.
</p>
<div id="orgff201e6" class="figure">
<p><img src="figures/fig2_17.png" alt="fig2_17.png" />
</p>
<p><span class="figure-number">Figure 16: </span>The KNN training error rate (blue, 200 observations) and test error rate (orange, 5,000 observations) on the data from figure <a href="#org0d2d113">13</a> as the level of flexibility (assessed using \(\frac{1}{K}\)) increases, or equivalently as the number of neighbors \(K\) decreases. The black dashed line indicates the Bayes error rate.</p>
</div>
</div>
</div>
<div id="outline-container-org13974b2" class="outline-3">
<h3 id="org13974b2"><span class="section-number-3">2.3</span> Lab: Introduction to Python</h3>
<div class="outline-text-3" id="text-2-3">
</div>
<div id="outline-container-org2037bff" class="outline-4">
<h4 id="org2037bff"><span class="section-number-4">2.3.1</span> Basic Commands</h4>
<div class="outline-text-4" id="text-2-3-1">
<p>
In <code>Python</code> a list can be created by enclosing comma-separated elements by
square brackets. Length of a list can be obtained using <code>len</code> function.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #BA36A5;">x</span> = [1, 3, 2, 5]
<span style="color: #0000FF;">print</span>(<span style="color: #006FE0;">len</span>(x))
<span style="color: #BA36A5;">y</span> = 3
<span style="color: #BA36A5;">z</span> = 5
<span style="color: #0000FF;">print</span>(y + z)
</pre>
</div>
<pre class="example">
4
8
</pre>
<p>
To create an array of numbers, use <code>array</code> function in <code>numpy</code> library. <code>numpy</code>
functions can be used to perform element-wise operations on arrays.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> numpy <span style="color: #0000FF;">as</span> np
<span style="color: #BA36A5;">x</span> = np.array([[1, 2], [3, 4]])
<span style="color: #BA36A5;">y</span> = np.array([6, 7, 8, 9]).reshape((2, 2))
<span style="color: #0000FF;">print</span>(x)
<span style="color: #0000FF;">print</span>(y)
<span style="color: #0000FF;">print</span>(x ** 2)
<span style="color: #0000FF;">print</span>(np.sqrt(y))
</pre>
</div>
<pre class="example">
[[1 2]
[3 4]]
[[6 7]
[8 9]]
[[ 1 4]
[ 9 16]]
[[2.44948974 2.64575131]
[2.82842712 3. ]]
</pre>
<p>
<code>numpy.random</code> has a number of functions to generate random variables that
follow a given distribution. Here we create two correlated sets of numbers, <code>x</code>
and <code>y</code>, and use <code>numpy.corrcoef</code> to calculate correlation between them.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> numpy <span style="color: #0000FF;">as</span> np
np.random.seed(911)
<span style="color: #BA36A5;">x</span> = np.random.normal(size=50)
<span style="color: #BA36A5;">y</span> = x + np.random.normal(loc=50, scale=0.1, size=50)
<span style="color: #0000FF;">print</span>(np.corrcoef(x, y))
<span style="color: #0000FF;">print</span>(np.corrcoef(x, y)[0, 1])
<span style="color: #0000FF;">print</span>(np.mean(x))
<span style="color: #0000FF;">print</span>(np.var(y))
<span style="color: #0000FF;">print</span>(np.std(y) ** 2)
</pre>
</div>
<pre class="example">
[[1. 0.99374931]
[0.99374931 1. ]]
0.9937493134584551
-0.020219724397254404
0.9330621750073689
0.9330621750073688
</pre>
</div>
</div>
<div id="outline-container-orgb312b27" class="outline-4">
<h4 id="orgb312b27"><span class="section-number-4">2.3.2</span> Graphics</h4>
<div class="outline-text-4" id="text-2-3-2">
<p>
<code>matplotlib</code> library has a number of functions to plot data in <code>Python</code>. It is
possible to view graphs on screen or save them in file for inclusion in a
document.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> numpy <span style="color: #0000FF;">as</span> np
<span style="color: #0000FF;">import</span> matplotlib <span style="color: #8D8D84;"># </span><span style="color: #8D8D84; font-style: italic;">only if we need to save figure in file</span>
matplotlib.use(<span style="color: #008000;">'Agg'</span>) <span style="color: #8D8D84;"># </span><span style="color: #8D8D84; font-style: italic;">only to save figure in file</span>
<span style="color: #0000FF;">import</span> matplotlib.pyplot <span style="color: #0000FF;">as</span> plt
<span style="color: #BA36A5;">x</span> = np.random.normal(size=100)
<span style="color: #BA36A5;">y</span> = np.random.normal(size=100)
plt.plot(x, y)
plt.xlabel(<span style="color: #008000;">'This is x-axis'</span>)
plt.ylabel(<span style="color: #008000;">'This is y-axis'</span>)
plt.title(<span style="color: #008000;">'Plot of X vs Y'</span>)
plt.savefig(<span style="color: #008000;">'xyPlot.png'</span>) <span style="color: #8D8D84;"># </span><span style="color: #8D8D84; font-style: italic;">only to save figure in a file</span>
</pre>
</div>
<p>
<code>numpy</code> function <code>linspace</code> can be used to create a sequence between a start and
an end of a given length.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> numpy <span style="color: #0000FF;">as</span> np
<span style="color: #0000FF;">import</span> matplotlib.pyplot <span style="color: #0000FF;">as</span> plt
<span style="color: #BA36A5;">x</span> = np.linspace(-np.pi, np.pi, num=50)
<span style="color: #BA36A5;">y</span> = x
<span style="color: #BA36A5;">xx</span>, <span style="color: #BA36A5;">yy</span> = np.meshgrid(x, y)
<span style="color: #BA36A5;">zz</span> = np.cos(yy) / (1 + xx ** 2)
plt.contour(xx, yy, zz)
<span style="color: #BA36A5;">fig</span>, <span style="color: #BA36A5;">ax</span> = plt.subplots()
<span style="color: #BA36A5;">zza</span> = (zz - zz.T) / 2.0
<span style="color: #BA36A5;">CS</span> = ax.contour(xx, yy, zza)
ax.clabel(CS, inline=1)
</pre>
</div>
</div>
</div>
<div id="outline-container-org5e49b84" class="outline-4">
<h4 id="org5e49b84"><span class="section-number-4">2.3.3</span> Indexing Data</h4>
<div class="outline-text-4" id="text-2-3-3">
<p>
To access elements of an array, specify indexes inside square brackets. It is
possible to access multiple rows and columns. <code>shape</code> method gives number of
rows followed by number of columns.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> numpy <span style="color: #0000FF;">as</span> np
<span style="color: #BA36A5;">A</span> = np.array(np.arange(1, 17))
<span style="color: #BA36A5;">A</span> = A.reshape(4, 4, order=<span style="color: #008000;">'F'</span>) <span style="color: #8D8D84;"># </span><span style="color: #8D8D84; font-style: italic;">column first, Fortran style</span>
<span style="color: #0000FF;">print</span>(A)
<span style="color: #0000FF;">print</span>(A[1, 2])
<span style="color: #0000FF;">print</span>(A[(0,2),:][:,(1,3)])
<span style="color: #0000FF;">print</span>(A[<span style="color: #006FE0;">range</span>(0,3),:][:,<span style="color: #006FE0;">range</span>(1,4)])
<span style="color: #0000FF;">print</span>(A[<span style="color: #006FE0;">range</span>(0, 2), :])
<span style="color: #0000FF;">print</span>(A[:, <span style="color: #006FE0;">range</span>(0, 2)])
<span style="color: #0000FF;">print</span>(A[0,:])
<span style="color: #0000FF;">print</span>(A.shape)
</pre>
</div>
<pre class="example">
[[ 1 5 9 13]
[ 2 6 10 14]
[ 3 7 11 15]
[ 4 8 12 16]]
10
[ 5 15]
[ 5 10 15]
[[ 1 5 9 13]
[ 2 6 10 14]]
[[1 5]
[2 6]
[3 7]
[4 8]]
(4, 4)
</pre>
</div>
</div>
<div id="outline-container-orgd28dc33" class="outline-4">
<h4 id="orgd28dc33"><span class="section-number-4">2.3.4</span> Loading Data</h4>
<div class="outline-text-4" id="text-2-3-4">
<p>
<code>pandas</code> library provides <code>read_csv</code> function to read files with data in
rectangular shape.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> pandas <span style="color: #0000FF;">as</span> pd
<span style="color: #BA36A5;">Auto</span> = pd.read_csv(<span style="color: #008000;">'data/Auto.csv'</span>)
<span style="color: #0000FF;">print</span>(Auto.head())
<span style="color: #0000FF;">print</span>(Auto.shape)
<span style="color: #0000FF;">print</span>(Auto.columns)
</pre>
</div>
<pre class="example">
mpg cylinders displacement ... year origin name
0 18.0 8 307.0 ... 70 1 chevrolet chevelle malibu
1 15.0 8 350.0 ... 70 1 buick skylark 320
2 18.0 8 318.0 ... 70 1 plymouth satellite
3 16.0 8 304.0 ... 70 1 amc rebel sst
4 17.0 8 302.0 ... 70 1 ford torino
[5 rows x 9 columns]
(397, 9)
Index(['mpg', 'cylinders', 'displacement', 'horsepower', 'weight',
'acceleration', 'year', 'origin', 'name'],
dtype='object')
</pre>
<p>
To load data from an <code>R</code> library, use <code>get_rdataset</code> function from
<code>statsmodels</code>. This function seems to work only if the computer is connected to
the internet.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">from</span> statsmodels <span style="color: #0000FF;">import</span> datasets
<span style="color: #BA36A5;">carseats</span> = datasets.get_rdataset(<span style="color: #008000;">'Carseats'</span>, package=<span style="color: #008000;">'ISLR'</span>).data
<span style="color: #0000FF;">print</span>(carseats.shape)
<span style="color: #0000FF;">print</span>(carseats.columns)
</pre>
</div>
<pre class="example">
(400, 11)
Index(['Sales', 'CompPrice', 'Income', 'Advertising', 'Population', 'Price',
'ShelveLoc', 'Age', 'Education', 'Urban', 'US'],
dtype='object')
</pre>
</div>
</div>
<div id="outline-container-org845481c" class="outline-4">
<h4 id="org845481c"><span class="section-number-4">2.3.5</span> Additional Graphical and Numerical Summaries</h4>
<div class="outline-text-4" id="text-2-3-5">
<p>
<code>plot</code> method can be directly applied to a <code>pandas</code> dataframe.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> pandas <span style="color: #0000FF;">as</span> pd
<span style="color: #BA36A5;">Auto</span> = pd.read_csv(<span style="color: #008000;">'data/Auto.csv'</span>)
Auto.boxplot(column=<span style="color: #008000;">'mpg'</span>, by=<span style="color: #008000;">'cylinders'</span>, grid=<span style="color: #D0372D;">False</span>)
</pre>
</div>
<p>
<code>hist</code> method can be applied to plot a histogram.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> pandas <span style="color: #0000FF;">as</span> pd
<span style="color: #BA36A5;">Auto</span> = pd.read_csv(<span style="color: #008000;">'data/Auto.csv'</span>)
Auto.hist(column=<span style="color: #008000;">'mpg'</span>)
Auto.hist(column=<span style="color: #008000;">'mpg'</span>, color=<span style="color: #008000;">'red'</span>)
Auto.hist(column=<span style="color: #008000;">'mpg'</span>, color=<span style="color: #008000;">'red'</span>, bins=15)
</pre>
</div>
<p>
For pairs plot, use <code>scatter_matrix</code> method in <code>pandas.plotting</code>.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> pandas <span style="color: #0000FF;">as</span> pd
<span style="color: #0000FF;">from</span> pandas <span style="color: #0000FF;">import</span> plotting
<span style="color: #BA36A5;">Auto</span> = pd.read_csv(<span style="color: #008000;">'data/Auto.csv'</span>)
plotting.scatter_matrix(Auto[[<span style="color: #008000;">'mpg'</span>, <span style="color: #008000;">'displacement'</span>, <span style="color: #008000;">'horsepower'</span>, <span style="color: #008000;">'weight'</span>,
<span style="color: #008000;">'acceleration'</span>]])
</pre>
</div>
<p>
On <code>pandas</code> dataframes, <code>describe</code> method produces a summary of each variable.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #0000FF;">import</span> pandas <span style="color: #0000FF;">as</span> pd