-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
2121 lines (2079 loc) · 197 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.324">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Anurag Patil">
<title>Predicting customer behaviour using scikit learn</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="main_analysis_files/libs/clipboard/clipboard.min.js"></script>
<script src="main_analysis_files/libs/quarto-html/quarto.js"></script>
<script src="main_analysis_files/libs/quarto-html/popper.min.js"></script>
<script src="main_analysis_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="main_analysis_files/libs/quarto-html/anchor.min.js"></script>
<link href="main_analysis_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="main_analysis_files/libs/quarto-html/quarto-syntax-highlighting-dark.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="main_analysis_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="main_analysis_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="main_analysis_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="dark">
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" integrity="sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script type="application/javascript">define('jquery', [],function() {return window.jQuery;})</script>
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-full">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active" data-toc-expanded="99">
<h2 id="toc-title">Table of contents</h2>
<ul class="collapse">
<li><a href="#scenario" id="toc-scenario" class="nav-link active" data-scroll-target="#scenario">1. Scenario</a></li>
<li><a href="#data" id="toc-data" class="nav-link" data-scroll-target="#data">2. Data</a></li>
<li><a href="#defining-the-problem" id="toc-defining-the-problem" class="nav-link" data-scroll-target="#defining-the-problem">3. Defining the problem</a></li>
<li><a href="#data-cleaning-and-validation." id="toc-data-cleaning-and-validation." class="nav-link" data-scroll-target="#data-cleaning-and-validation.">4. Data cleaning and validation.</a></li>
<li><a href="#visual-inspection" id="toc-visual-inspection" class="nav-link" data-scroll-target="#visual-inspection">5. Visual inspection</a></li>
<li><a href="#preprocessing-and-building-pipeline." id="toc-preprocessing-and-building-pipeline." class="nav-link" data-scroll-target="#preprocessing-and-building-pipeline.">6. Preprocessing and Building Pipeline.</a></li>
<li><a href="#hyperparameter-tuning" id="toc-hyperparameter-tuning" class="nav-link" data-scroll-target="#hyperparameter-tuning">7. Hyperparameter Tuning</a></li>
<li><a href="#final-training." id="toc-final-training." class="nav-link" data-scroll-target="#final-training.">8. Final Training.</a></li>
<li><a href="#final-thougts" id="toc-final-thougts" class="nav-link" data-scroll-target="#final-thougts">9. Final Thougts</a></li>
</ul>
</nav>
</div>
<main class="content column-page-left" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<div class="quarto-title-block"><div><h1 class="title">Predicting customer behaviour using scikit learn</h1><button type="button" class="btn code-tools-button dropdown-toggle" id="quarto-code-tools-menu" data-bs-toggle="dropdown" aria-expanded="false"><i class="bi"></i> Code</button><ul class="dropdown-menu dropdown-menu-end" aria-labelelledby="quarto-code-tools-menu"><li><a id="quarto-show-all-code" class="dropdown-item" href="javascript:void(0)" role="button">Show All Code</a></li><li><a id="quarto-hide-all-code" class="dropdown-item" href="javascript:void(0)" role="button">Hide All Code</a></li><li><hr class="dropdown-divider"></li><li><a id="quarto-view-source" class="dropdown-item" href="javascript:void(0)" role="button">View Source</a></li></ul></div></div>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Anurag Patil </p>
</div>
</div>
</div>
</header>
<p><br></p>
<section id="before-getting-into-the-report" class="level4">
<h4 class="anchored" data-anchor-id="before-getting-into-the-report">Before getting into the report</h4>
<ol type="1">
<li>All the code cells are collapsed so the reader can focus on the analysis</li>
<li>Although if you are interested in looking at the code at certain part you can unfold the code block just by clicking on the <strong>“Code”</strong> button provided.</li>
<li>If you are interested in looking at all the code you can unfold all the code cells at once by using the option in right hand corner. Alternatively you can go to the github repositery where all the code for this is hosted. <a href="https://github.com/ANP-Oxy/GoalZone">repositery</a></li>
</ol>
<hr>
<ul>
<li>This is a revamped version of the original case study that I worked on for DataCamp’s <strong>“Data Scientist Associate certification”.</strong></li>
<li>The dataset as well as the problem scenario was provided by DataCamp.</li>
<li>In this report I will be working on a fictitious business scenario, dealing with a customer behaviour classification problem.</li>
<li>I will clean the data, validate it, do some visual insepction of the data and then work on modelling it.</li>
<li>The tools used for this analysis are python, numpy, pandas, matplotlib, seaborn, scikit-learn, imbalanced learn and the report was generated using Quarto.</li>
</ul>
</section>
<section id="scenario" class="level2">
<h2 class="anchored" data-anchor-id="scenario">1. Scenario</h2>
<ul>
<li>GoalZone is a fitness club chain in Canada.</li>
<li>GoalZone offers a range of fitness classes in two capacities - 25 and 15.</li>
<li>Some classes are always fully booked. Fully booked classes often have a low attendance rate.</li>
<li>GoalZone wants to increase the number of spaces available for classes.</li>
<li>They want to do this by predicting whether the member will attend the class or not.</li>
<li>If they can predict a member will not attend the class, they can make another space available.</li>
</ul>
<hr>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">2. Data</h2>
<div class="cell" data-execution_count="1">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Importing the necessary modules</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> seaborn <span class="im">as</span> sns</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>plt.style.use(<span class="st">"seaborn"</span>)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co"># let's get the data</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>url <span class="op">=</span> <span class="st">"https://s3.amazonaws.com/talent-assets.datacamp.com/fitness_class_2212.csv"</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="co"># this will fetch the data and put it into a dataframe. </span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a>data <span class="op">=</span> pd.read_csv(url)</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a>data.head()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>C:\Users\Anurag\AppData\Local\Temp\ipykernel_5904\1184041087.py:6: MatplotlibDeprecationWarning: The seaborn styles shipped by Matplotlib are deprecated since 3.6, as they no longer correspond to the styles shipped by seaborn. However, they will remain available as 'seaborn-v0_8-<style>'. Alternatively, directly use the seaborn API instead.
plt.style.use("seaborn")</code></pre>
</div>
<div class="cell-output cell-output-display" data-execution_count="163">
<div>
<table class="dataframe table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">booking_id</th>
<th data-quarto-table-cell-role="th">months_as_member</th>
<th data-quarto-table-cell-role="th">weight</th>
<th data-quarto-table-cell-role="th">days_before</th>
<th data-quarto-table-cell-role="th">day_of_week</th>
<th data-quarto-table-cell-role="th">time</th>
<th data-quarto-table-cell-role="th">category</th>
<th data-quarto-table-cell-role="th">attended</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>1</td>
<td>17</td>
<td>79.56</td>
<td>8</td>
<td>Wed</td>
<td>PM</td>
<td>Strength</td>
<td>0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>2</td>
<td>10</td>
<td>79.01</td>
<td>2</td>
<td>Mon</td>
<td>AM</td>
<td>HIIT</td>
<td>0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>3</td>
<td>16</td>
<td>74.53</td>
<td>14</td>
<td>Sun</td>
<td>AM</td>
<td>Strength</td>
<td>0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>4</td>
<td>5</td>
<td>86.12</td>
<td>10</td>
<td>Fri</td>
<td>AM</td>
<td>Cycling</td>
<td>0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>5</td>
<td>15</td>
<td>69.29</td>
<td>8</td>
<td>Thu</td>
<td>AM</td>
<td>HIIT</td>
<td>0</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p><strong>The data has 8 columns and 1500 rows</strong><br> The basic descriptions of the columns are.<br></p>
<ol type="1">
<li>unique booking id</li>
<li>The number of months as this fitness club member</li>
<li>weight</li>
<li>The number of days before the class the member registered</li>
<li>day of week of the class</li>
<li>time of class</li>
<li>category</li>
<li>The last column attended shows whether the member attended the class or not with 0 corresponding to “No” and 1 to “Yes”.</li>
</ol>
</section>
<section id="defining-the-problem" class="level2">
<h2 class="anchored" data-anchor-id="defining-the-problem">3. Defining the problem</h2>
<ul>
<li><strong>The business wants to predict whether members will attend using the data provided</strong></li>
<li>Here even though the specific task about predicting whether the member will attend the class or not we should keep in mind that the end goal to increase the number of members joining the class.</li>
<li>Keeping the big picture in the back of the mind before diving into analysis is necessary.</li>
<li>Now, about the data since our target variable is binary this is essentially a <strong>Binary Classification</strong> problem</li>
<li>We have to predict whether the member who has registered for the class will attend the class or not.</li>
</ul>
<p>Now that we have taken a glimpse of the data, decided what our end goal is let’s get into the actual analysis. we will go through this step by step by first validating the data then doing some Exploratory analysis and finally training some machine learning models.</p>
</section>
<section id="data-cleaning-and-validation." class="level2">
<h2 class="anchored" data-anchor-id="data-cleaning-and-validation.">4. Data cleaning and validation.</h2>
<p><br> Let’s take a look at some information about the columns</p>
<ol type="1">
<li>“months_as_member” : 1 is minimum value for this column</li>
<li>“weight” : 40.00 is minimum value</li>
<li>“days_before” : 1 is minimum value</li>
<li>“day_of_week” : “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun” are valid values</li>
<li>“time” : “AM” , “PM” are valid values</li>
<li>“category” : “Yoga”, “Aqua” , “Strength”, “HIIT”, “Cycling”, “unknown” are valid values</li>
<li>“attended” : 1, 0 are valid values</li>
</ol>
<p>Below I’m creating a python dictionary that contains some metadata about the data we have. This will be used later on to validate the integrity of the data.</p>
<div class="cell" data-execution_count="2">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>data_dict <span class="op">=</span> {<span class="st">"booking_id"</span> : <span class="st">"The unique identifier of the booking."</span>,</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"months_as_member"</span> : <span class="dv">1</span>, <span class="co"># minimum value for this column</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"weight"</span> : <span class="fl">40.00</span>, <span class="co"># minimum value</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"days_before"</span> : <span class="dv">1</span>, <span class="co"># minimum value</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"day_of_week"</span> : [<span class="st">"Mon"</span>, <span class="st">"Tue"</span>, <span class="st">"Wed"</span>, <span class="st">"Thu"</span>, <span class="st">"Fri"</span>, <span class="st">"Sat"</span>, <span class="st">"Sun"</span>], <span class="co"># valid categories</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a> <span class="st">"time"</span> : [<span class="st">"AM"</span> , <span class="st">"PM"</span>], <span class="co"># valid categories</span></span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a> <span class="st">"category"</span> : [<span class="st">"Yoga"</span>, <span class="st">"Aqua"</span> , <span class="st">"Strength"</span>, <span class="st">"HIIT"</span>, <span class="st">"Cycling"</span>, <span class="st">"unknown"</span>], <span class="co"># valid categories</span></span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a> <span class="st">"attended"</span> : [<span class="dv">1</span>, <span class="dv">0</span>] <span class="co"># valid categories</span></span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a> }</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<section id="lets-take-a-look-at-what-kind-of-problems-the-data-has." class="level4">
<h4 class="anchored" data-anchor-id="lets-take-a-look-at-what-kind-of-problems-the-data-has.">Let’s take a look at what kind of problems the data has. <br></h4>
<p>first we will check the categorical columns</p>
<div class="cell" data-execution_count="3">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Select the categorical columns</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Print value counts for each of those columns</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> column <span class="kw">in</span> data.select_dtypes(<span class="st">"object"</span>).columns: </span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"</span><span class="sc">{</span>column<span class="sc">}</span><span class="ss"> :</span><span class="ch">\n</span><span class="sc">{</span>data[column]<span class="sc">.</span>value_counts()<span class="sc">}</span><span class="ss"> </span><span class="ch">\n</span><span class="ss">"</span>) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<p><strong>I have disabled the output here because it is very long but you can execute the code and take a look at it</strong><br> <strong>The problems identified in the categorical columns are</strong></p>
<ol type="1">
<li>The days before column is not completely numeric and some cells have “days” string written alongside the number of days. This is data entry incosistency and needs to be fixed.</li>
<li>The day of the week also has data entry errors and the weekday names aren’t consistent.</li>
<li>The time columns seems fine</li>
<li>category column has “-” as values in few columns which will need to be replaced.</li>
</ol>
<p><strong>I will peform series of operations on the data and fix the problems mentioned above.</strong></p>
<ol type="1">
<li>first we fix the days before column by elimination string characters from it</li>
<li>next we fixed the day_of_week column by using the function defind above</li>
<li>then fix the category column by replacing “-” with “unknown”</li>
<li>after that we change the datatypes of the columns</li>
<li>lastly drop the “booking_id” column as it is not useful</li>
</ol>
<div class="cell" data-execution_count="4">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># this is a function to fix the day_of_week column</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> fix_dow(df):</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> df[<span class="st">'day_of_week'</span>] <span class="op">=</span> df[<span class="st">'day_of_week'</span>].<span class="bu">str</span>.replace(<span class="st">'Fri.'</span>, <span class="st">"Fri"</span>)</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> df[<span class="st">'day_of_week'</span>] <span class="op">=</span> df[<span class="st">'day_of_week'</span>].<span class="bu">str</span>.replace(<span class="st">'Monday'</span>, <span class="st">"Mon"</span>)</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> df[<span class="st">'day_of_week'</span>] <span class="op">=</span> df[<span class="st">'day_of_week'</span>].<span class="bu">str</span>.replace(<span class="st">'Wednesday'</span>, <span class="st">"Wed"</span>)</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> df[<span class="st">'day_of_week'</span>]</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a><span class="co"># this fuction will carry out cleaning steps outlined above</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> clean_data(data <span class="op">=</span> data):</span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> (data</span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> .assign(days_before <span class="op">=</span> data[<span class="st">'days_before'</span>].<span class="bu">str</span>.extract(<span class="vs">r'(\d+)'</span>),</span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> day_of_week <span class="op">=</span> fix_dow,</span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> category <span class="op">=</span> data[<span class="st">'category'</span>].replace(<span class="st">"-"</span>, <span class="st">"unknown"</span>))</span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a> .astype({<span class="st">"days_before"</span>:<span class="st">"int"</span>, <span class="st">"day_of_week"</span>:<span class="st">"category"</span>, <span class="st">"time"</span>:<span class="st">"category"</span>, </span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a> <span class="st">"category"</span>:<span class="st">"category"</span>, <span class="st">"attended"</span>:<span class="st">"category"</span>})</span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> .drop(<span class="st">"booking_id"</span>, axis<span class="op">=</span><span class="dv">1</span>) <span class="co"># drop booking id column</span></span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a><span class="co"># let's store the clean dataframe in a new variable</span></span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a>clean_data <span class="op">=</span> clean_data()</span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a>clean_data.head()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="166">
<div>
<table class="dataframe table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">months_as_member</th>
<th data-quarto-table-cell-role="th">weight</th>
<th data-quarto-table-cell-role="th">days_before</th>
<th data-quarto-table-cell-role="th">day_of_week</th>
<th data-quarto-table-cell-role="th">time</th>
<th data-quarto-table-cell-role="th">category</th>
<th data-quarto-table-cell-role="th">attended</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">0</td>
<td>17</td>
<td>79.56</td>
<td>8</td>
<td>Wed</td>
<td>PM</td>
<td>Strength</td>
<td>0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">1</td>
<td>10</td>
<td>79.01</td>
<td>2</td>
<td>Mon</td>
<td>AM</td>
<td>HIIT</td>
<td>0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">2</td>
<td>16</td>
<td>74.53</td>
<td>14</td>
<td>Sun</td>
<td>AM</td>
<td>Strength</td>
<td>0</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">3</td>
<td>5</td>
<td>86.12</td>
<td>10</td>
<td>Fri</td>
<td>AM</td>
<td>Cycling</td>
<td>0</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">4</td>
<td>15</td>
<td>69.29</td>
<td>8</td>
<td>Thu</td>
<td>AM</td>
<td>HIIT</td>
<td>0</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>We can look at the output of the cleaned data. Let’s validate this data using a utility functions I have written. This will check the columns to see if they contain valid categories or are in valid numerical range.</p>
<div class="cell" data-execution_count="5">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> utils <span class="im">import</span> validate</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>validate(clean_data, data_dict)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>"months_as_member" passed the validation test
"weight" passed the validation test
"days_before" passed the validation test
"day_of_week" passed the validation test
"time" passed the validation test
"category" passed the validation test
"attended" passed the validation test</code></pre>
</div>
</div>
<p>as we can see the data has passed the validation tests that were defined. <br></p>
</section>
<section id="does-the-data-have-missing-values" class="level4">
<h4 class="anchored" data-anchor-id="does-the-data-have-missing-values">Does the data have missing values?</h4>
<div class="cell" data-execution_count="6">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(clean_data.isna().<span class="bu">sum</span>())</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>months_as_member 0
weight 20
days_before 0
day_of_week 0
time 0
category 0
attended 0
dtype: int64</code></pre>
</div>
</div>
<ul>
<li>it looks like the weight column has few missing values</li>
<li>we will have to impute these values later before we train the model</li>
<li>let’s keep going for now <br> <strong>This ends the section on data validation and cleaning. Now we will start analyzing the data</strong></li>
</ul>
</section>
</section>
<section id="visual-inspection" class="level2">
<h2 class="anchored" data-anchor-id="visual-inspection">5. Visual inspection</h2>
<ul>
<li>now that we have cleaned the data it is time to start analyzing it.</li>
<li>Let’s take a look at basic summary statistics for the numerical variables</li>
</ul>
<div class="cell" data-execution_count="7">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>clean_data.describe()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="169">
<div>
<table class="dataframe table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">months_as_member</th>
<th data-quarto-table-cell-role="th">weight</th>
<th data-quarto-table-cell-role="th">days_before</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">count</td>
<td>1500.000000</td>
<td>1480.000000</td>
<td>1500.000000</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">mean</td>
<td>15.628667</td>
<td>82.610378</td>
<td>8.346667</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">std</td>
<td>12.926543</td>
<td>12.765859</td>
<td>4.077938</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">min</td>
<td>1.000000</td>
<td>55.410000</td>
<td>1.000000</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">25%</td>
<td>8.000000</td>
<td>73.490000</td>
<td>4.000000</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">50%</td>
<td>12.000000</td>
<td>80.760000</td>
<td>9.000000</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">75%</td>
<td>19.000000</td>
<td>89.520000</td>
<td>12.000000</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">max</td>
<td>148.000000</td>
<td>170.520000</td>
<td>29.000000</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>it doesn’t look like there are any anomalies in this data. so we can keep going</p>
<section id="lets-see-the-distribution-of-our-target-variable." class="level4">
<h4 class="anchored" data-anchor-id="lets-see-the-distribution-of-our-target-variable.">Let’s see the distribution of our target variable.</h4>
<div class="cell" data-execution_count="8">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>ax <span class="op">=</span> sns.countplot(data <span class="op">=</span> clean_data, x <span class="op">=</span> <span class="st">'attended'</span>, width<span class="op">=</span><span class="fl">0.5</span>)</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>ax.set_xticklabels(labels<span class="op">=</span>[<span class="st">'No'</span>, <span class="st">'Yes'</span>])</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>ax.set_yticks([])</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a>ax.bar_label(ax.containers[<span class="dv">0</span>], label_type<span class="op">=</span><span class="st">'center'</span>, color<span class="op">=</span><span class="st">'black'</span>)</span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a>ax.set_xlabel(<span class="st">"Attended the class or not?"</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="main_analysis_files/figure-html/cell-9-output-1.png" width="634" height="466" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<ul>
<li>We can see that the amount of people that did not attend the class is more than twice the amount of the people that did attend the class</li>
<li>So the classes for our target variable are imbalanced</li>
<li>Here we have to be careful due to this.</li>
<li>A model that predicts every Member as someone not attending the class will still have about 70% accuracy</li>
<li>Is machine learning even the right approach to solve this problem?</li>
<li>If the percentage of members who attend the class is pretty consistent you could just oversell the tickets?</li>
<li>There are lots of point of views from which you can think about this problem. for now we will simply continue the analysis</li>
</ul>
</section>
<section id="next-we-will-take-a-look-at-the-months_as_member-column" class="level4">
<h4 class="anchored" data-anchor-id="next-we-will-take-a-look-at-the-months_as_member-column">Next we will take a look at the “months_as_member” column</h4>
<div class="cell" data-execution_count="9">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>sns.histplot(data <span class="op">=</span> clean_data, x <span class="op">=</span> <span class="st">'months_as_member'</span>, kde<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">'Distribution of "months as member"'</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="main_analysis_files/figure-html/cell-10-output-1.png" width="665" height="486" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<ul>
<li>The distribution is right skewed</li>
<li>It looks like most of the members have been members for between 1 to 20 months and then there’s smaller proportion of people who have been members for longer than that.</li>
<li>There are few outliers here who have been members a lot longer than everyone else which have given a long right tail to the distribution</li>
</ul>
</section>
<section id="we-can-look-at-this-distribution-with-a-log-scale-on-x-axis" class="level4">
<h4 class="anchored" data-anchor-id="we-can-look-at-this-distribution-with-a-log-scale-on-x-axis">We can look at this distribution with a log scale on x-axis**</h4>
<div class="cell" data-execution_count="10">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>sns.histplot(data <span class="op">=</span> clean_data, x <span class="op">=</span> <span class="st">'months_as_member'</span>, kde<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>plt.xscale(<span class="st">'log'</span>)</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">'Distribution of "months as member (log scale)"'</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="main_analysis_files/figure-html/cell-11-output-1.png" width="665" height="493" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="lets-look-at-the-relationship-between-months_as_member-and-whether-the-person-attends-the-class-or-not" class="level4">
<h4 class="anchored" data-anchor-id="lets-look-at-the-relationship-between-months_as_member-and-whether-the-person-attends-the-class-or-not">Let’s look at the relationship between “months_as_member” and whether the person attends the class or not</h4>
<div class="cell" data-execution_count="11">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> plt.style.context(<span class="st">'dark_background'</span>):</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> fig, ax <span class="op">=</span> plt.subplots(<span class="dv">1</span>, <span class="dv">2</span>, figsize<span class="op">=</span>(<span class="dv">12</span>, <span class="dv">5</span>))</span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a> sns.stripplot(data <span class="op">=</span> clean_data, x<span class="op">=</span><span class="st">'attended'</span>, </span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a> y<span class="op">=</span><span class="st">'months_as_member'</span>, color<span class="op">=</span><span class="st">'steelblue'</span>, </span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a> alpha<span class="op">=</span><span class="fl">0.5</span>, ax<span class="op">=</span>ax[<span class="dv">0</span>])</span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a> sns.pointplot(data<span class="op">=</span>clean_data, x<span class="op">=</span><span class="st">'attended'</span>,</span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a> estimator<span class="op">=</span><span class="st">'mean'</span>, y<span class="op">=</span><span class="st">'months_as_member'</span>, </span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a> color<span class="op">=</span><span class="st">'white'</span>, ax<span class="op">=</span>ax[<span class="dv">0</span>])</span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true" tabindex="-1"></a> attended <span class="op">=</span> clean_data[<span class="st">'attended'</span>].replace({<span class="dv">0</span>:<span class="st">"No"</span>, <span class="dv">1</span>:<span class="st">"Yes"</span>})</span>
<span id="cb14-14"><a href="#cb14-14" aria-hidden="true" tabindex="-1"></a> sns.ecdfplot(data<span class="op">=</span>clean_data, x<span class="op">=</span><span class="st">'months_as_member'</span>, hue<span class="op">=</span>attended, ax<span class="op">=</span>ax[<span class="dv">1</span>])</span>
<span id="cb14-15"><a href="#cb14-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-16"><a href="#cb14-16" aria-hidden="true" tabindex="-1"></a> plt.setp(ax[<span class="dv">0</span>].lines, zorder<span class="op">=</span><span class="dv">100</span>)</span>
<span id="cb14-17"><a href="#cb14-17" aria-hidden="true" tabindex="-1"></a> plt.setp(ax[<span class="dv">0</span>].collections, zorder<span class="op">=</span><span class="dv">100</span>, label<span class="op">=</span><span class="st">""</span>)</span>
<span id="cb14-18"><a href="#cb14-18" aria-hidden="true" tabindex="-1"></a> ax[<span class="dv">0</span>].grid(<span class="va">False</span>)</span>
<span id="cb14-19"><a href="#cb14-19" aria-hidden="true" tabindex="-1"></a> ax[<span class="dv">1</span>].grid(<span class="va">False</span>)</span>
<span id="cb14-20"><a href="#cb14-20" aria-hidden="true" tabindex="-1"></a> ax[<span class="dv">0</span>].margins(x<span class="op">=</span><span class="fl">0.1</span>)</span>
<span id="cb14-21"><a href="#cb14-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-22"><a href="#cb14-22" aria-hidden="true" tabindex="-1"></a> ax[<span class="dv">0</span>].set_ylim(<span class="dv">0</span>)</span>
<span id="cb14-23"><a href="#cb14-23" aria-hidden="true" tabindex="-1"></a> ax[<span class="dv">0</span>].annotate(<span class="st">"Average for both categories"</span>, xy<span class="op">=</span>(<span class="fl">0.4</span>, <span class="dv">20</span>), xytext<span class="op">=</span>(<span class="fl">0.2</span>, <span class="dv">100</span>), arrowprops<span class="op">=</span><span class="bu">dict</span>(facecolor<span class="op">=</span><span class="st">'black'</span>, shrink<span class="op">=</span><span class="fl">0.05</span>))</span>
<span id="cb14-24"><a href="#cb14-24" aria-hidden="true" tabindex="-1"></a> ax[<span class="dv">0</span>].set_ylabel(<span class="st">"Number of months as member of club"</span>)</span>
<span id="cb14-25"><a href="#cb14-25" aria-hidden="true" tabindex="-1"></a> ax[<span class="dv">0</span>].set_xlabel(<span class="st">"Attended the class?"</span>)</span>
<span id="cb14-26"><a href="#cb14-26" aria-hidden="true" tabindex="-1"></a> ax[<span class="dv">0</span>].set_xticklabels([<span class="st">'No'</span>, <span class="st">"yes"</span>])</span>
<span id="cb14-27"><a href="#cb14-27" aria-hidden="true" tabindex="-1"></a> ax[<span class="dv">0</span>].annotate(xy<span class="op">=</span>(<span class="dv">0</span>, <span class="fl">0.95</span>), xytext<span class="op">=</span>(<span class="dv">0</span>, <span class="fl">0.85</span>), textcoords <span class="op">=</span><span class="st">'axes fraction'</span>, text<span class="op">=</span><span class="st">"The people who attend the class are </span><span class="ch">\n</span><span class="st"> more likely to have been member of </span><span class="ch">\n</span><span class="st"> the club for longer time"</span>)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="main_analysis_files/figure-html/cell-12-output-1.png" width="963" height="434" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="ok-there-is-a-lot-of-unpack-here" class="level4">
<h4 class="anchored" data-anchor-id="ok-there-is-a-lot-of-unpack-here">ok there is a lot of unpack here :)</h4>
<ol type="1">
<li>In the right graph we can see how many months each person has been member of the club and whether they attended the class or not.</li>
<li>The points for members who did not attend are closely stacked togethere whereas the points for people who did attend are more spread out and have longer range.</li>
<li><strong>The white points joind by a line show the average of <em>months as member</em> for both categories</strong></li>
<li>we can see a clear difference there. It’s not very big but it is definitely noticeble.</li>
<li>The right graph shows a ECDF for both categories.</li>
<li>we can see that people who attended the event are more likely to have higher number of months as members of the club <br> Ok! This was just some quick analysis. before we go any further into our analysis</li>
</ol>
<ul>
<li>we will split the data into train and test set first</li>
<li>we don’t want to learn patterns from test set too much so before doing a deeper analysis we will separate them</li>
</ul>
<div class="cell" data-execution_count="12">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Obtaining a train test split</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.model_selection <span class="im">import</span> train_test_split</span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a>X, y <span class="op">=</span> clean_data.drop(<span class="st">"attended"</span>, axis<span class="op">=</span><span class="dv">1</span>), clean_data[<span class="st">"attended"</span>]</span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a>X_train, X_test, y_train, y_test <span class="op">=</span> train_test_split(X, y, test_size <span class="op">=</span> <span class="fl">0.3</span>, stratify<span class="op">=</span>y, random_state<span class="op">=</span><span class="dv">241</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="first-of-all-lets-look-at-relationship-between-all-the-features-and-our-target-variable." class="level4">
<h4 class="anchored" data-anchor-id="first-of-all-lets-look-at-relationship-between-all-the-features-and-our-target-variable.">First of all let’s look at relationship between all the features and our target variable.</h4>
<p>i’m gonna go ahead and write a function that will provide us with plots for this</p>
<div class="cell" data-execution_count="13">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> utils <span class="im">import</span> get_relations</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>get_relations(dataframe <span class="op">=</span> X_train, y <span class="op">=</span> y_train, which<span class="op">=</span><span class="st">"numerics"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="main_analysis_files/figure-html/cell-14-output-1.png" width="963" height="503" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<ul>
<li>we can see here that the months_as_member and weight column have some correlation with the target variable.</li>
<li>Depending on whether the target variable is “yes” or “no” the distribution of these features has slight difference</li>
<li>on the other hand the days_before column doesn’t seem to have any correlation with the target variable as the distribution for both the category of target variable seem pretty similar</li>
<li>one other observation is that the months_as_member and weight column seem to few outliers that might affect the model performance later.</li>
</ul>
</section>
<section id="now-lets-take-a-look-at-categorical-columns" class="level4">
<h4 class="anchored" data-anchor-id="now-lets-take-a-look-at-categorical-columns">Now let’s take a look at categorical columns <br></h4>
<p>we will look at how the proportions of the categorical variables are distributed for our target variable.</p>
<div class="cell" data-execution_count="14">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>get_relations(dataframe <span class="op">=</span> X_train, y <span class="op">=</span> y_train, which<span class="op">=</span><span class="st">"categoricals"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="main_analysis_files/figure-html/cell-15-output-1.png" width="947" height="809" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<ul>
<li>what we can see in this plot is the variables “day_of_week”, “time” and “category” and how they are distributed across our target classes</li>
<li>I normalized the counts so we can look at the proportions and compare to see if any of these categorical features have any correlation with the target</li>
<li>looking at these plots it doesn’t look like there is any clear correlation.</li>
</ul>
</section>
<section id="next-we-will-take-a-look-at-how-the-numeric-feature-are-distributed-and-do-they-have-any-correlation-with-each-other" class="level4">
<h4 class="anchored" data-anchor-id="next-we-will-take-a-look-at-how-the-numeric-feature-are-distributed-and-do-they-have-any-correlation-with-each-other">Next, we will take a look at how the numeric feature are distributed and do they have any correlation with each other?</h4>
<div class="cell" data-execution_count="15">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>sns.pairplot(data <span class="op">=</span> X_train)<span class="op">;</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="main_analysis_files/figure-html/cell-16-output-1.png" width="709" height="709" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<ul>
<li>We already know that the “Months_as_member” and “weight” features have some correlation with the target</li>
<li>here it looks like they are also related to each other sightly</li>
<li>let’s take a look at these two columns</li>
</ul>
<div class="cell" data-execution_count="16">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>sns.scatterplot(data<span class="op">=</span>clean_data, x<span class="op">=</span><span class="st">'months_as_member'</span>, y<span class="op">=</span><span class="st">"weight"</span>, hue<span class="op">=</span>y_train)</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a>plt.ylim(<span class="dv">0</span>)</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a>plt.xscale(<span class="st">'log'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="main_analysis_files/figure-html/cell-17-output-1.png" width="665" height="473" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<ul>
<li>since the months as member feature has skewed distribution I Have to put it on a <strong>Log-Scale</strong></li>
<li>here we can see that it seems like there is some negative correlation between these two features</li>
<li>it makes sense that people with longer duration are likely to have lower weight as they are more likely to workout regularly.</li>
<li>another thing we can see is that the members with higher months as members are likely to attend the class they registered to</li>
</ul>
</section>
</section>
<section id="preprocessing-and-building-pipeline." class="level2">
<h2 class="anchored" data-anchor-id="preprocessing-and-building-pipeline.">6. Preprocessing and Building Pipeline.</h2>
<ul>
<li>We have looked at the distributions of the data</li>
<li>we also looked at how the various features are related to the target variable as well as relationship between categorical features</li>
<li>The next step is to prepare the data for training a machine learning model</li>
<li>We will create a pipeline that does all these things for us</li>
<li>I will also include a step for <strong>Removing Outliers</strong> from the data inside the pipeline. Since sci-kit learn doesn’t allow for this functionality I will be using <strong>imbalanced-learn</strong> to implement that. You can find out more about it <a href="https://imbalanced-learn.org/stable/auto_examples/applications/plot_outlier_rejections.html">here</a></li>
</ul>
<section id="the-process-of-removing-outliers." class="level4">
<h4 class="anchored" data-anchor-id="the-process-of-removing-outliers.">The process of removing outliers.</h4>
<ul>
<li>I will write a custom function which will do following things
<ol type="1">
<li>It will take X_train and Y_train</li>
<li>It will remove any rows corresponding to outliers from X_train and y_train</li>
<li>The outliers here will be any observations that are outside of 3 standard deviations of the distribution.</li>
</ol></li>
<li>Before removing outliers we will also do other preprocessing steps such as
<ol type="1">
<li>Imputing missing values for the weight column</li>
<li>Encoding the Categorical variables</li>
<li>after that outiler rejection will take place</li>
<li>and lastly we will scale and standardize the data</li>
</ol></li>
</ul>
<div class="cell" data-execution_count="17">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.impute <span class="im">import</span> SimpleImputer</span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.preprocessing <span class="im">import</span> StandardScaler</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.preprocessing <span class="im">import</span> OneHotEncoder</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.preprocessing <span class="im">import</span> OrdinalEncoder</span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> imblearn.pipeline <span class="im">import</span> Pipeline</span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> imblearn <span class="im">import</span> FunctionSampler</span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.compose <span class="im">import</span> ColumnTransformer</span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.linear_model <span class="im">import</span> LogisticRegression</span>
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.svm <span class="im">import</span> SVC</span>
<span id="cb20-10"><a href="#cb20-10" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.neighbors <span class="im">import</span> KNeighborsClassifier</span>
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.model_selection <span class="im">import</span> cross_val_score</span>
<span id="cb20-12"><a href="#cb20-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-13"><a href="#cb20-13" aria-hidden="true" tabindex="-1"></a>imputer <span class="op">=</span> SimpleImputer(strategy<span class="op">=</span><span class="st">'mean'</span>)</span>
<span id="cb20-14"><a href="#cb20-14" aria-hidden="true" tabindex="-1"></a>onehot <span class="op">=</span> OneHotEncoder()</span>
<span id="cb20-15"><a href="#cb20-15" aria-hidden="true" tabindex="-1"></a>ordEnc <span class="op">=</span> OrdinalEncoder()</span>
<span id="cb20-16"><a href="#cb20-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-17"><a href="#cb20-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-18"><a href="#cb20-18" aria-hidden="true" tabindex="-1"></a>models <span class="op">=</span> {<span class="st">"Logistic"</span>:LogisticRegression(),</span>
<span id="cb20-19"><a href="#cb20-19" aria-hidden="true" tabindex="-1"></a> <span class="st">"SVC"</span>:SVC(),</span>
<span id="cb20-20"><a href="#cb20-20" aria-hidden="true" tabindex="-1"></a> <span class="st">"KNN"</span>:KNeighborsClassifier()</span>
<span id="cb20-21"><a href="#cb20-21" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb20-22"><a href="#cb20-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-23"><a href="#cb20-23" aria-hidden="true" tabindex="-1"></a>preprocessor <span class="op">=</span> ColumnTransformer(transformers<span class="op">=</span>[</span>
<span id="cb20-24"><a href="#cb20-24" aria-hidden="true" tabindex="-1"></a> (<span class="st">"Imputer"</span>, SimpleImputer(strategy<span class="op">=</span><span class="st">'mean'</span>),[<span class="st">'weight'</span>]),</span>
<span id="cb20-25"><a href="#cb20-25" aria-hidden="true" tabindex="-1"></a> (<span class="st">"CatEncoder"</span>, onehot, [<span class="st">"category"</span>, <span class="st">"day_of_week"</span>]),</span>
<span id="cb20-26"><a href="#cb20-26" aria-hidden="true" tabindex="-1"></a> (<span class="st">"OrdEnc"</span>, ordEnc, [<span class="st">"time"</span>])],</span>
<span id="cb20-27"><a href="#cb20-27" aria-hidden="true" tabindex="-1"></a> remainder<span class="op">=</span> <span class="st">"passthrough"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
<ul>
<li>What I have done here is import all the necessary estimators and transformers that will be used for preprocessing and model training</li>
<li>I have created a Instance of Column transformer that will operate on the categorical and numeric columns seperately and output the cleaned features</li>
</ul>
</section>
<section id="models" class="level3">
<h3 class="anchored" data-anchor-id="models">6.1 Models</h3>
<ul>
<li>As can be seen I have created instances of three different models for training which are as following
<ol type="1">
<li><strong>Logistic Regression</strong> - A linear classifier that is simple to explain and train.</li>
<li><strong>Support vector machines</strong> - another similar model that can fit to both linear and non linear patterns in the data</li>
<li><strong>K-Neighbours Classifier</strong> - For a different approach this model classifies the data points based on the euclidean or manhattan distance in N-dimensional space. we will train these three models and see which one performs good enough or is this data even good enough to make predictions.</li>
</ol></li>
</ul>
<div class="cell" data-execution_count="18">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> utils <span class="im">import</span> outlier_rejection</span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> get_scores(model_dict, X, y, metric):</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a> <span class="co">'''</span></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a><span class="co"> This function returns training score and</span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a><span class="co"> cross validation score for various models</span></span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a><span class="co"> _____________________________________________________</span></span>
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a><span class="co"> params</span></span>
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a><span class="co"> model_dict: (dict) model name and model instance</span></span>
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a><span class="co"> X: X_train array</span></span>
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></a><span class="co"> y: y_train arrray</span></span>
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></a><span class="co"> '''</span></span>
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a> model_names <span class="op">=</span> []</span>
<span id="cb21-14"><a href="#cb21-14" aria-hidden="true" tabindex="-1"></a> training_score <span class="op">=</span> []</span>
<span id="cb21-15"><a href="#cb21-15" aria-hidden="true" tabindex="-1"></a> cross_validation_scores <span class="op">=</span> []</span>
<span id="cb21-16"><a href="#cb21-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-17"><a href="#cb21-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> name, model <span class="kw">in</span> model_dict.items():</span>
<span id="cb21-18"><a href="#cb21-18" aria-hidden="true" tabindex="-1"></a> pipeline <span class="op">=</span> Pipeline(steps<span class="op">=</span>[(<span class="st">"preprocesser"</span>, preprocessor),</span>
<span id="cb21-19"><a href="#cb21-19" aria-hidden="true" tabindex="-1"></a> (<span class="st">"outlier_rejection"</span>,</span>
<span id="cb21-20"><a href="#cb21-20" aria-hidden="true" tabindex="-1"></a> FunctionSampler(func<span class="op">=</span>outlier_rejection)),</span>
<span id="cb21-21"><a href="#cb21-21" aria-hidden="true" tabindex="-1"></a> (<span class="st">"scaler"</span>, StandardScaler()),</span>
<span id="cb21-22"><a href="#cb21-22" aria-hidden="true" tabindex="-1"></a> (<span class="st">"model"</span>, model)])</span>
<span id="cb21-23"><a href="#cb21-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-24"><a href="#cb21-24" aria-hidden="true" tabindex="-1"></a> pipeline.fit(X, y.values)</span>
<span id="cb21-25"><a href="#cb21-25" aria-hidden="true" tabindex="-1"></a> model_names.append(name)</span>
<span id="cb21-26"><a href="#cb21-26" aria-hidden="true" tabindex="-1"></a> training_score.append(pipeline.score(X, y.values))</span>
<span id="cb21-27"><a href="#cb21-27" aria-hidden="true" tabindex="-1"></a> avg_cross_val_score <span class="op">=</span> np.mean(cross_val_score(pipeline, X, y.values,</span>
<span id="cb21-28"><a href="#cb21-28" aria-hidden="true" tabindex="-1"></a> cv<span class="op">=</span><span class="dv">5</span>, scoring<span class="op">=</span>metric))</span>
<span id="cb21-29"><a href="#cb21-29" aria-hidden="true" tabindex="-1"></a> cross_validation_scores.append(avg_cross_val_score)</span>
<span id="cb21-30"><a href="#cb21-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-31"><a href="#cb21-31" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> pd.DataFrame({<span class="st">"training_score"</span>:training_score,</span>
<span id="cb21-32"><a href="#cb21-32" aria-hidden="true" tabindex="-1"></a> <span class="st">"cross_val_score"</span>:cross_validation_scores},</span>
<span id="cb21-33"><a href="#cb21-33" aria-hidden="true" tabindex="-1"></a> index<span class="op">=</span>model_names)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
</div>
</section>
<section id="pipeline" class="level3">
<h3 class="anchored" data-anchor-id="pipeline">6.2 Pipeline</h3>
<ul>
<li>In the above code cell, have basically defined a function that uses the pipeline we have created to train the models we have defined</li>
<li>It will calculate the training scores and the cross validation scores for us of each model.</li>
<li>The basic steps in this pipeline are
<ol type="1">
<li>Column transformation which includes imputation and categorical variable encoding</li>
<li>Next step is removing outliers from the training data. <strong>Note the outliers will only be removed from the training data and not from the testing data</strong></li>
<li>After this scaling and training takes place.</li>
</ol></li>
</ul>
</section>
<section id="model-evaluation" class="level3">
<h3 class="anchored" data-anchor-id="model-evaluation">6.3 Model evaluation</h3>
<div class="cell" data-execution_count="19">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>scores <span class="op">=</span> get_scores(model_dict <span class="op">=</span> models, X <span class="op">=</span> X_train, y <span class="op">=</span> y_train, metric<span class="op">=</span><span class="st">"accuracy"</span>)</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a>scores.plot.bar()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="181">
<pre><code><Axes: ></code></pre>
</div>
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="main_analysis_files/figure-html/cell-20-output-2.png" width="642" height="480" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<ul>
<li>Here we can see that Logistic regression has best cross validation score and it’s the simplest model</li>
<li>SVC is pretty similar in results and it doesn’t look like it’s overfitting the training data</li>
<li>The KNN model has the highest training accuracy but lowest cross validation accuracy which means it’s overfitting the training data.</li>
<li><strong>Not the thing to keep in mind when looking at these metrics is accuracy is not a good metric to judge a classification models, specially when the target variable is highly inbalanced.</strong></li>
<li>Let’s calculate ROC_AUC scores for each model using cross vaidation to see how they are really performing.</li>
</ul>
<div class="cell" data-execution_count="20">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>roc_auc <span class="op">=</span> get_scores(model_dict <span class="op">=</span> models, X <span class="op">=</span> X_train, y <span class="op">=</span> y_train, metric<span class="op">=</span><span class="st">"roc_auc"</span>)</span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>roc_auc[[<span class="st">"cross_val_score"</span>]]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="182">
<div>
<table class="dataframe table table-sm table-striped small" data-quarto-postprocess="true" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">cross_val_score</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td data-quarto-table-cell-role="th">Logistic</td>
<td>0.796712</td>
</tr>
<tr class="even">
<td data-quarto-table-cell-role="th">SVC</td>
<td>0.768351</td>
</tr>
<tr class="odd">
<td data-quarto-table-cell-role="th">KNN</td>
<td>0.690112</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<ul>
<li>Once again Logistic Regression has the best performance out of three models.</li>
<li>These ROC_AUC scores are pretty similar to what we say with accuracy.</li>
<li>But this does tell us that the model is not good just by a random chance. It has some good predictive ability.</li>
<li><strong>An even better metric for testing the models would be confusion_matrix</strong></li>
<li>Let’s see confusion matrix for each model on training data.</li>
</ul>
<div class="cell" data-execution_count="21">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> yellowbrick.classifier <span class="im">import</span> ConfusionMatrix</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Let's use a subset of above mentioned function to do this. </span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> confusion():</span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a> pipeline <span class="op">=</span> Pipeline(steps<span class="op">=</span>[(<span class="st">"preprocesser"</span>, preprocessor),</span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a> (<span class="st">"outlier_rejection"</span>,</span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a> FunctionSampler(func<span class="op">=</span>outlier_rejection)),</span>
<span id="cb25-10"><a href="#cb25-10" aria-hidden="true" tabindex="-1"></a> ])</span>
<span id="cb25-11"><a href="#cb25-11" aria-hidden="true" tabindex="-1"></a> x_train_c, y_train_c <span class="op">=</span> pipeline.fit_resample(X_train, y_train.values)</span>
<span id="cb25-12"><a href="#cb25-12" aria-hidden="true" tabindex="-1"></a> x_train_scale <span class="op">=</span> StandardScaler().fit_transform(x_train_c)</span>
<span id="cb25-13"><a href="#cb25-13" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb25-14"><a href="#cb25-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> model <span class="kw">in</span> models.values():</span>
<span id="cb25-15"><a href="#cb25-15" aria-hidden="true" tabindex="-1"></a> fig, ax <span class="op">=</span> plt.subplots(figsize<span class="op">=</span>(<span class="dv">4</span>, <span class="dv">4</span>))</span>
<span id="cb25-16"><a href="#cb25-16" aria-hidden="true" tabindex="-1"></a> cm <span class="op">=</span> ConfusionMatrix(model, encoder<span class="op">=</span>{<span class="dv">1</span>:<span class="st">"yes"</span>, <span class="dv">0</span>:<span class="st">"No"</span>}, ax<span class="op">=</span>ax)</span>
<span id="cb25-17"><a href="#cb25-17" aria-hidden="true" tabindex="-1"></a> cm.fit(x_train_scale, y_train_c)</span>