Repository navigation
Expand file tree
/
Copy pathP01.html
More file actions
2493 lines (2412 loc) · 190 KB
/
Copy pathP01.html
File metadata and controls
2493 lines (2412 loc) · 190 KB
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.10.18">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="GROUP 01">
<meta name="dcterms.date" content="2026-08-21">
<title>DATA ANALYSIS REPORT</title>
<style>
/* Default styles provided by pandoc.
** See https://pandoc.org/MANUAL.html#variables-for-html for config info.
*/
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: 1.5em;}
div.column{flex: auto;}
@media screen {
div.columns{gap: min(4vw, 1.5em);}
div.column{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 */
html { -webkit-text-size-adjust: 100%; }
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; }
}
/* CSS for citations */
div.csl-bib-body { }
div.csl-entry {
clear: both;
margin-bottom: 0em;
}
.hanging-indent div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}</style>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" integrity="sha384-ZvpUoO/+PpLXR1lu4jmpXWu80pZlYUAfxl5NsBMWOEPSjUn/6Z/hRTt8+pR6L4N2" crossorigin="anonymous"></script><script src="P01_files/libs/clipboard/clipboard.min.js"></script>
<script src="P01_files/libs/quarto-html/quarto.js" type="module"></script>
<script src="P01_files/libs/quarto-html/tabsets/tabsets.js" type="module"></script>
<script src="P01_files/libs/quarto-html/popper.min.js"></script>
<script src="P01_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="P01_files/libs/quarto-html/anchor.min.js"></script>
<link href="P01_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="P01_files/libs/quarto-html/quarto-syntax-highlighting-64a204ab8560d761af7679ceaba22944.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="P01_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="P01_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="P01_files/libs/bootstrap/bootstrap-917079515d91bccb85b543faf8aa8bb6.min.css" rel="stylesheet" append-hash="true" id="quarto-bootstrap" data-mode="light">
<script src="https://cdn.jsdelivr.net/npm/requirejs@2.3.6/require.min.js" integrity="sha384-c9c+LnTbwQ3aujuU7ULEPVvgLs+Fn6fJUvIGTsuu1ZcCf11fiEubah0ttpca4ntM sha384-6V1/AdqZRWk1KAlWbKBlGhN7VG4iE/yAZcO6NZPMF8od0vukrvr0tg4qY6NSrItx" crossorigin="anonymous"></script>
<script type="application/javascript">define('jquery', [],function() {return window.jQuery;})</script>
<link rel="stylesheet" href="styles.css">
</head>
<body class="quarto-light">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<div class="quarto-alternate-formats"><h2>Other Formats</h2><ul><li><a href="P01.pdf"><i class="bi bi-file-pdf"></i>PDF</a></li></ul></div></div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">DATA ANALYSIS REPORT</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>GROUP 01 </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">August 21, 2026</p>
</div>
</div>
</div>
<div>
<div class="abstract">
<div class="block-title">Abstract</div>
<p>This study applies a complete data science pipeline to predict academic risk among university students. Using a dataset of 12,084 records, we investigate data quality issues, apply justified preprocessing techniques, and compare four machine learning models: Logistic Regression, Random Forest, Gradient Boosting, and XGBoost. Due to severe class imbalance (~85.6% Not At Risk), we prioritise recall and ROC-AUC over accuracy. Our findings indicate that while traditional models struggle to identify at-risk students, XGBoost with class balancing and Youden-index threshold tuning significantly improves recall to 63.5%. Feature importance analysis reveals that a newly engineered variable, <code>engagement_score</code> (combining attendance and assignment averages), is the strongest predictor of academic risk, followed by midterm scores.</p>
</div>
</div>
</header>
<section id="data-analysis-mini-project-student-academic-risk" class="level1">
<h1><strong>DATA ANALYSIS MINI PROJECT: STUDENT ACADEMIC RISK</strong></h1>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span><strong>Project Brief</strong>
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Domain:</strong> Higher Education / Student Performance <strong>Task:</strong> Binary Classification <strong>Target Variable:</strong> <code>academic_risk</code> (At Risk vs. Not At Risk) <strong>Primary Project Question:</strong> Predict whether a student is academically <em>At Risk</em> or <em>Not At Risk</em>?</p>
</div>
</div>
<section id="business-understanding" class="level2">
<h2 class="anchored" data-anchor-id="business-understanding"><strong>1. BUSINESS UNDERSTANDING</strong></h2>
<section id="domain-problem-context" class="level3">
<h3 class="anchored" data-anchor-id="domain-problem-context">Domain & Problem Context</h3>
<p>In higher education, identifying students at risk of academic failure before it is too late is a critical challenge. Late identification often leads to dropped courses, poor graduation rates, and reduced student morale. By leveraging historical student data, universities can build predictive models to flag “At Risk” students early in the semester, allowing academic advisors to intervene proactively rather than reactively.</p>
</section>
<section id="project-objective" class="level3">
<h3 class="anchored" data-anchor-id="project-objective">Project Objective</h3>
<p>The primary objective of this project is to build a predictive model that classifies whether a student is academically <strong>“At Risk”</strong> or <strong>“Not At Risk”</strong> based on their demographic, academic, and behavioural data.</p>
</section>
<section id="stakeholders-value" class="level3">
<h3 class="anchored" data-anchor-id="stakeholders-value">Stakeholders & Value</h3>
<ul>
<li><strong>Who could use the findings?</strong> Academic advisors, student affairs departments, and university retention teams.</li>
<li><strong>What value does the analysis provide?</strong> It enables a data-driven, early-warning system. By knowing <em>which</em> factors drive risk and <em>who</em> is most likely to struggle, advisors can allocate limited resources to the students who need help the most.</li>
</ul>
</section>
<section id="supporting-research-questions" class="level3">
<h3 class="anchored" data-anchor-id="supporting-research-questions">Supporting Research Questions</h3>
<p>To guide our analysis, we formulated three supporting questions:</p>
<ol type="1">
<li>Which features (attendance, midterm, GPA) are the strongest predictors of academic risk?</li>
<li>Does programme type influence academic risk?</li>
<li>How does study hours per week relate to being at risk?</li>
</ol>
</section>
</section>
<section id="phase-1-setup-load-the-data" class="level2">
<h2 class="anchored" data-anchor-id="phase-1-setup-load-the-data"><strong>PHASE 1: SETUP & LOAD THE DATA</strong></h2>
<section id="import-all-libaries" class="level3">
<h3 class="anchored" data-anchor-id="import-all-libaries"><strong>IMPORT ALL LIBARIES</strong></h3>
<div id="f29ced6c" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><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="im">import</span> pandas <span class="im">as</span> pd</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> matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> seaborn <span class="im">as</span> sns</span>
<span id="cb1-5"><a href="#cb1-5" 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="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.preprocessing <span class="im">import</span> StandardScaler, LabelEncoder</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.linear_model <span class="im">import</span> LogisticRegression</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.ensemble <span class="im">import</span> RandomForestClassifier</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.metrics <span class="im">import</span>(</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> accuracy_score, precision_score, recall_score, f1_score,</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> confusion_matrix, classification_report, roc_auc_score, roc_curve</span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> warnings</span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a>warnings.filterwarnings(<span class="st">'ignore'</span>)</span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a><span class="co"># we need to set the style</span></span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a>sns.set_style(<span class="st">'whitegrid'</span>)</span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a>RANDOM_STATE <span class="op">=</span> <span class="dv">42</span></span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a><span class="co"># load datasets</span></span>
<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> pd.read_csv(<span class="st">'data/group01_student_performance_classification.csv'</span>)</span>
<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a>df_raw <span class="op">=</span> df.copy()</span>
<span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Shape:"</span>, df.shape)</span>
<span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df.head())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Shape: (12084, 14)
student_id age gender programme attendance_pct \
0 ST07173 20 Male Software Engineering 72.2
1 ST01210 25 Female IT 75.4
2 ST11561 24 Female Computer Science 100.0
3 ST05074 24 Male Computer Science 72.4
4 ST05558 21 Male Computer Science 78.7
assignment_avg quiz_avg midterm_score study_hours_week previous_gpa \
0 49.0 71.2 81.6 11.1 3.48
1 66.0 76.4 56.2 3.6 3.49
2 53.6 94.6 68.5 5.5 2.98
3 100.0 65.5 78.0 6.5 3.05
4 78.5 72.4 89.0 6.3 2.76
absences internet_access extracurricular academic_risk
0 3.0 Yes Yes Not At Risk
1 NaN Yes No At Risk
2 2.0 Yes Yes Not At Risk
3 7.0 Yes Yes Not At Risk
4 4.0 Yes Yes Not At Risk </code></pre>
</div>
</div>
<section id="all-data-manipulation-is-performed-using-pandas-pandas-while-machine-learning-models-are-built-with-scikit-learn-sklearn.-visualisations-use-matplotlib-matplotlib-and-seaborn-seaborn." class="level5">
<h5 class="anchored" data-anchor-id="all-data-manipulation-is-performed-using-pandas-pandas-while-machine-learning-models-are-built-with-scikit-learn-sklearn.-visualisations-use-matplotlib-matplotlib-and-seaborn-seaborn.">All data manipulation is performed using pandas <span class="citation" data-cites="pandas">(<a href="#ref-pandas" role="doc-biblioref">McKinney, 2010</a>)</span>, while machine learning models are built with scikit-learn <span class="citation" data-cites="sklearn">(<a href="#ref-sklearn" role="doc-biblioref"><span class="nocase">Pedregosa et al.</span>, 2011</a>)</span>. Visualisations use matplotlib <span class="citation" data-cites="matplotlib">(<a href="#ref-matplotlib" role="doc-biblioref">Hunter, 2007</a>)</span> and seaborn <span class="citation" data-cites="seaborn">(<a href="#ref-seaborn" role="doc-biblioref">Waskom, 2021</a>)</span>.</h5>
</section>
</section>
<section id="save-copy-as-original" class="level3">
<h3 class="anchored" data-anchor-id="save-copy-as-original"><strong>SAVE COPY AS ORIGINAL</strong></h3>
<div id="e4909fca" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><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>df.to_csv(<span class="st">'P01_data_original.csv'</span>, index<span class="op">=</span><span class="va">False</span>)</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Original dataset saved"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Original dataset saved</code></pre>
</div>
</div>
</section>
</section>
<section id="phase-2-data-understanding" class="level2">
<h2 class="anchored" data-anchor-id="phase-2-data-understanding"><strong>PHASE 2: DATA UNDERSTANDING</strong></h2>
<section id="basic-structure-missing-values" class="level3">
<h3 class="anchored" data-anchor-id="basic-structure-missing-values"><strong>BASIC STRUCTURE & MISSING VALUES</strong></h3>
<div id="fbd3f3cd" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><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="bu">print</span>(df.dtypes)</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"</span><span class="ch">\n</span><span class="st">Missing Values:"</span>)</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df.isnull().<span class="bu">sum</span>())</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"</span><span class="ch">\n</span><span class="st">Missing Values (%):"</span>)</span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>((df.isnull().<span class="bu">sum</span>() <span class="op">/</span> <span class="bu">len</span>(df) <span class="op">*</span> <span class="dv">100</span>).<span class="bu">round</span>(<span class="dv">2</span>))</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"</span><span class="ch">\n</span><span class="st">Duplicate Rows:"</span>, df.duplicated().<span class="bu">sum</span>())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>student_id str
age int64
gender str
programme str
attendance_pct float64
assignment_avg float64
quiz_avg float64
midterm_score float64
study_hours_week float64
previous_gpa float64
absences float64
internet_access str
extracurricular str
academic_risk str
dtype: object
Missing Values:
student_id 0
age 0
gender 0
programme 182
attendance_pct 0
assignment_avg 0
quiz_avg 0
midterm_score 181
study_hours_week 0
previous_gpa 0
absences 181
internet_access 182
extracurricular 0
academic_risk 0
dtype: int64
Missing Values (%):
student_id 0.00
age 0.00
gender 0.00
programme 1.51
attendance_pct 0.00
assignment_avg 0.00
quiz_avg 0.00
midterm_score 1.50
study_hours_week 0.00
previous_gpa 0.00
absences 1.50
internet_access 1.51
extracurricular 0.00
academic_risk 0.00
dtype: float64
Duplicate Rows: 83</code></pre>
</div>
</div>
</section>
<section id="target-distribution" class="level3">
<h3 class="anchored" data-anchor-id="target-distribution"><strong>TARGET DISTRIBUTION</strong></h3>
<div id="0e052d52" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df[<span class="st">'academic_risk'</span>].value_counts())</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"</span><span class="ch">\n</span><span class="st">Percentages:"</span>)</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df[<span class="st">'academic_risk'</span>].value_counts(normalize<span class="op">=</span><span class="va">True</span>) <span class="op">*</span> <span class="dv">100</span>)</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="co"># normalize true here means ? </span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="co"># it means that : shows what fraction from 0 to 1 of students are each category</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>academic_risk
Not At Risk 10349
At Risk 1735
Name: count, dtype: int64
Percentages:
academic_risk
Not At Risk 85.642171
At Risk 14.357829
Name: proportion, dtype: float64</code></pre>
</div>
</div>
</section>
<section id="numerical-summary" class="level3">
<h3 class="anchored" data-anchor-id="numerical-summary"><strong>NUMERICAL SUMMARY</strong></h3>
<div id="67aa225d" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>numeric_cols <span class="op">=</span> df.select_dtypes(include<span class="op">=</span>[np.number]).columns</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df[numeric_cols].describe().<span class="bu">round</span>(<span class="dv">2</span>))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> age attendance_pct assignment_avg quiz_avg midterm_score \
count 12084.00 12084.00 12084.00 12084.00 11903.00
mean 21.46 81.62 68.11 65.78 63.82
std 2.29 11.32 14.82 15.65 16.73
min 18.00 39.20 14.90 6.60 0.00
25% 19.00 73.90 58.30 55.20 52.70
50% 21.00 81.90 68.20 65.90 64.00
75% 23.00 90.10 78.40 76.53 75.60
max 25.00 100.00 100.00 100.00 100.00
study_hours_week previous_gpa absences
count 12084.00 12084.00 11903.0
mean 6.02 2.89 4.0
std 3.48 0.62 2.0
min 0.20 0.34 0.0
25% 3.50 2.47 3.0
50% 5.30 2.89 4.0
75% 7.80 3.33 5.0
max 25.00 4.00 13.0 </code></pre>
</div>
</div>
</section>
<section id="categorical-summary" class="level3">
<h3 class="anchored" data-anchor-id="categorical-summary"><strong>CATEGORICAL SUMMARY</strong></h3>
<div id="aeefacad" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><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>cat_cols <span class="op">=</span> [<span class="st">'gender'</span>, <span class="st">'programme'</span>, <span class="st">'internet_access'</span>, <span class="st">'extracurricular'</span>]</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> col <span class="kw">in</span> cat_cols:</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="ss">-- </span><span class="sc">{</span>col<span class="sc">}</span><span class="ss"> ---"</span>)</span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(df[col].value_counts(dropna<span class="op">=</span><span class="va">False</span>).head(<span class="dv">10</span>))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
-- gender ---
gender
Male 6044
Female 5920
M 64
female 56
Name: count, dtype: int64
-- programme ---
programme
Computer Science 4055
IT 2986
Software Engineering 2829
Data Science 2032
NaN 182
Name: count, dtype: int64
-- internet_access ---
internet_access
Yes 11234
No 668
NaN 182
Name: count, dtype: int64
-- extracurricular ---
extracurricular
No 6835
Yes 5249
Name: count, dtype: int64</code></pre>
</div>
</div>
</section>
<section id="data-quality-issues" class="level3">
<h3 class="anchored" data-anchor-id="data-quality-issues"><strong>DATA QUALITY ISSUES</strong></h3>
<div id="3fbb4d97" class="cell" data-execution_count="7">
<div class="code-copy-outer-scaffold"><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><span class="bu">print</span>(<span class="st">"Gender unique values:"</span>, df[<span class="st">'gender'</span>].unique())</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"</span><span class="ch">\n</span><span class="st">Programme missing: "</span>, df[<span class="st">'programme'</span>].isnull().<span class="bu">sum</span>())</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Midterm missing:"</span>, df[<span class="st">'midterm_score'</span>].isnull().<span class="bu">sum</span>())</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Absence missing:"</span>, df[<span class="st">'absences'</span>].isnull().<span class="bu">sum</span>())</span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Internet access missig"</span>, df[<span class="st">'internet_access'</span>].isnull().<span class="bu">sum</span>())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Gender unique values: <StringArray>
['Male', 'Female', 'female', 'M']
Length: 4, dtype: str
Programme missing: 182
Midterm missing: 181
Absence missing: 181
Internet access missig 182</code></pre>
</div>
</div>
</section>
</section>
<section id="visualization" class="level2">
<h2 class="anchored" data-anchor-id="visualization"><strong>VISUALIZATION</strong></h2>
<section id="v1-target-distribution" class="level4">
<h4 class="anchored" data-anchor-id="v1-target-distribution">V1: TARGET DISTRIBUTION</h4>
<div id="74c8b070" class="cell" data-execution_count="8">
<div class="code-copy-outer-scaffold"><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>plt.figure(figsize<span class="op">=</span>(<span class="dv">6</span>, <span class="dv">4</span>))</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>sns.countplot(data<span class="op">=</span>df, x<span class="op">=</span><span class="st">'academic_risk'</span>, palette<span class="op">=</span>[<span class="st">'seagreen'</span>, <span class="st">'red'</span>])</span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">'Distribution of Academic Risk'</span>)</span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">'Academic Risk'</span>)</span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a>plt.ylabel(<span class="st">'Count'</span>)</span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="P01_files/figure-html/cell-9-output-1.png" width="531" height="375" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>“The dataset is imbalanced: approximately around <strong>85.6% ”Not At Risk”</strong> vs <strong>~14.4% ”At Risk”</strong>. This means accuracy alone is misleading. Hence we will use f1-Score, recall and ROC-AUC.”</p>
</blockquote>
</section>
<section id="v2-attendance-vs-risk" class="level4">
<h4 class="anchored" data-anchor-id="v2-attendance-vs-risk">V2: ATTENDANCE VS RISK</h4>
<div id="648be38b" class="cell" data-execution_count="9">
<div class="code-copy-outer-scaffold"><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>plt.figure(figsize<span class="op">=</span>(<span class="dv">8</span>,<span class="dv">5</span>))</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>sns.boxplot(data<span class="op">=</span>df, x<span class="op">=</span><span class="st">'academic_risk'</span>, y<span class="op">=</span><span class="st">'attendance_pct'</span>, palette<span class="op">=</span>[<span class="st">'seagreen'</span>, <span class="st">'red'</span>])</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">'Attendance % by Academic Risk'</span>)</span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="P01_files/figure-html/cell-10-output-1.png" width="664" height="449" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="v3-midterm-score-vs-risk" class="level4">
<h4 class="anchored" data-anchor-id="v3-midterm-score-vs-risk">V3: MIDTERM SCORE VS RISK</h4>
<div id="aabc17c7" class="cell" data-execution_count="10">
<div class="code-copy-outer-scaffold"><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>plt.figure(figsize<span class="op">=</span>(<span class="dv">8</span>,<span class="dv">5</span>))</span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>sns.boxplot(data<span class="op">=</span>df, x<span class="op">=</span><span class="st">'academic_risk'</span>, y<span class="op">=</span><span class="st">'midterm_score'</span>, palette<span class="op">=</span>[<span class="st">'seagreen'</span>, <span class="st">'red'</span>])</span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">'Midterm Score by Academic risk'</span>)</span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="P01_files/figure-html/cell-11-output-1.png" width="664" height="449" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="phase-3-data-preprocesing-cleaning" class="level2">
<h2 class="anchored" data-anchor-id="phase-3-data-preprocesing-cleaning"><strong>PHASE 3: DATA PREPROCESING (CLEANING)</strong></h2>
<section id="step-1-handle-duplicates" class="level3">
<h3 class="anchored" data-anchor-id="step-1-handle-duplicates">STEP 1: HANDLE DUPLICATES</h3>
<div id="6fb66974" class="cell" data-execution_count="11">
<div class="code-copy-outer-scaffold"><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><span class="co"># shows the original for we do the deduplication operation </span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Before Dedup Operations:"</span>, df.shape)</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> df.drop_duplicates()</span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"After Dedup Operations:"</span>, df.shape)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Before Dedup Operations: (12084, 14)
After Dedup Operations: (12001, 14)</code></pre>
</div>
</div>
</section>
<section id="step-2-fix-gender-inconsistencies" class="level3">
<h3 class="anchored" data-anchor-id="step-2-fix-gender-inconsistencies">STEP 2: FIX GENDER INCONSISTENCIES</h3>
<div id="ad39ff4b" class="cell" data-execution_count="12">
<div class="code-copy-outer-scaffold"><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="co"># standardize gender values</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>df[<span class="st">'gender'</span>] <span class="op">=</span> df[<span class="st">'gender'</span>].replace({</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="st">'female'</span>: <span class="st">'Female'</span>,</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> <span class="st">'M'</span>: <span class="st">'Male'</span>,</span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a> <span class="st">'Male'</span>: <span class="st">'Male'</span>,</span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a> <span class="st">'Female'</span>: <span class="st">'Female'</span></span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a>})</span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"</span><span class="ch">\n</span><span class="st">Gender values now:"</span>, df[<span class="st">'gender'</span>].unique())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Gender values now: <StringArray>
['Male', 'Female']
Length: 2, dtype: str</code></pre>
</div>
</div>
</section>
<section id="step-3-handle-missing-values" class="level3">
<h3 class="anchored" data-anchor-id="step-3-handle-missing-values">STEP 3: HANDLE MISSING VALUES</h3>
<blockquote class="blockquote">
<p>We actually got 2 options here which is <strong>drop</strong> or <strong>impute</strong> the missing values 😎</p>
</blockquote>
<section id="first-we-need-to-investigate-how-missing-values-are-distributed-are-they-random-or-systematic" class="level6">
<h6 class="anchored" data-anchor-id="first-we-need-to-investigate-how-missing-values-are-distributed-are-they-random-or-systematic">first we need to investigate, how missing values are distributed, are they random or systematic ?</h6>
<div id="d68db7fb" class="cell" data-execution_count="13">
<div class="code-copy-outer-scaffold"><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><span class="co"># check if missing values are they the same rows ??</span></span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a>missing_mask <span class="op">=</span> df[[<span class="st">'programme'</span>, <span class="st">'midterm_score'</span>, <span class="st">'absences'</span>, <span class="st">'internet_access'</span>]].isnull().<span class="bu">any</span>(axis<span class="op">=</span><span class="dv">1</span>)</span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Rows with ANY missing value:"</span>, missing_mask.<span class="bu">sum</span>())</span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a><span class="co"># ====== How many missing values does each affected row have?</span></span>
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a>missing_counts <span class="op">=</span> df[[<span class="st">'programme'</span>, <span class="st">'midterm_score'</span>, <span class="st">'absences'</span>, <span class="st">'internet_access'</span>]].isnull().<span class="bu">sum</span>(axis<span class="op">=</span><span class="dv">1</span>)</span>
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Distribution of missing values per row:"</span>)</span>
<span id="cb22-7"><a href="#cb22-7" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(missing_counts[missing_counts <span class="op">></span> <span class="dv">0</span>].value_counts().sort_index())</span>
<span id="cb22-8"><a href="#cb22-8" aria-hidden="true" tabindex="-1"></a><span class="co"># ====== check if missingness relates to the target ======</span></span>
<span id="cb22-9"><a href="#cb22-9" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"</span><span class="ch">\n</span><span class="st">Missing values by academic_risk:"</span>)</span>
<span id="cb22-10"><a href="#cb22-10" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df[missing_mask][<span class="st">'academic_risk'</span>].value_counts())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows with ANY missing value: 706
Distribution of missing values per row:
1 692
2 14
Name: count, dtype: int64
Missing values by academic_risk:
academic_risk
Not At Risk 593
At Risk 113
Name: count, dtype: int64</code></pre>
</div>
</div>
</section>
<section id="here-means-that-any-rows-that-have-at-least-one-missing-values-are-counted" class="level6">
<h6 class="anchored" data-anchor-id="here-means-that-any-rows-that-have-at-least-one-missing-values-are-counted"><em>706 here means that any rows that have at least one missing values are counted</em></h6>
</section>
<section id="missing-values-here-occur-mostly-in-isolation-692-rows-missing-1-value-14-rows-missing-2-values.-this-suggests-that-missing-data-is-scattered-and-largely-random-rather-than-caused-by-a-systematic-and-this-support-our-decision-of-using-imputation-to-preserve-all-records-rather-than-dropping-them" class="level6">
<h6 class="anchored" data-anchor-id="missing-values-here-occur-mostly-in-isolation-692-rows-missing-1-value-14-rows-missing-2-values.-this-suggests-that-missing-data-is-scattered-and-largely-random-rather-than-caused-by-a-systematic-and-this-support-our-decision-of-using-imputation-to-preserve-all-records-rather-than-dropping-them">“Missing values here occur mostly in isolation (692 rows missing 1 value, 14 rows missing 2 values). This suggests that missing data is scattered and largely random, rather than caused by a systematic, and this support our decision of using imputation to preserve all records rather than dropping them”</h6>
</section>
</section>
<section id="step-4-impute-the-missing-values" class="level3">
<h3 class="anchored" data-anchor-id="step-4-impute-the-missing-values">STEP 4: IMPUTE THE MISSING VALUES</h3>
<section id="check-the-skewness-to-justify-wether-to-use-median-or-mean" class="level4">
<h4 class="anchored" data-anchor-id="check-the-skewness-to-justify-wether-to-use-median-or-mean">4.01 CHECK THE SKEWNESS (TO JUSTIFY WETHER TO USE MEDIAN OR MEAN)</h4>
<div id="6da4bd5f" class="cell" data-execution_count="14">
<div class="code-copy-outer-scaffold"><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><span class="co"># check the skewness to justify median vs mean for numeric columns</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Skewness:"</span>)</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> col <span class="kw">in</span> [<span class="st">'midterm_score'</span>, <span class="st">'absences'</span>, <span class="st">'attendance_pct'</span>, <span class="st">'previous_gpa'</span>, <span class="st">'assignment_avg'</span>, <span class="st">'quiz_avg'</span>]:</span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" </span><span class="sc">{</span>col<span class="sc">}</span><span class="ss">: </span><span class="sc">{</span>df[col]<span class="sc">.</span>skew()<span class="sc">:.3f}</span><span class="ss">"</span>)</span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a><span class="co"># plotting the skewness using plotly and seaborn</span></span>
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a>sns.set_style(<span class="st">"whitegrid"</span>)</span>
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a><span class="co"># creating the 2 x 3 grid of subplots</span></span>
<span id="cb24-10"><a href="#cb24-10" aria-hidden="true" tabindex="-1"></a>fig, axes <span class="op">=</span> plt.subplots(<span class="dv">2</span>, <span class="dv">3</span>, figsize<span class="op">=</span>(<span class="dv">15</span>, <span class="dv">10</span>))</span>
<span id="cb24-11"><a href="#cb24-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-12"><a href="#cb24-12" aria-hidden="true" tabindex="-1"></a><span class="co"># flatten the axes for easy iteration</span></span>
<span id="cb24-13"><a href="#cb24-13" aria-hidden="true" tabindex="-1"></a>axes <span class="op">=</span> axes.flatten()</span>
<span id="cb24-14"><a href="#cb24-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-15"><a href="#cb24-15" aria-hidden="true" tabindex="-1"></a><span class="co"># plot each columns</span></span>
<span id="cb24-16"><a href="#cb24-16" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i, col <span class="kw">in</span> <span class="bu">enumerate</span>([<span class="st">'midterm_score'</span>, <span class="st">'absences'</span>, <span class="st">'attendance_pct'</span>, <span class="st">'previous_gpa'</span>, <span class="st">'assignment_avg'</span>, <span class="st">'quiz_avg'</span>]):</span>
<span id="cb24-17"><a href="#cb24-17" aria-hidden="true" tabindex="-1"></a> sns.histplot(df[col], kde<span class="op">=</span><span class="va">True</span>, ax<span class="op">=</span>axes[i], bins<span class="op">=</span><span class="dv">20</span>, color<span class="op">=</span><span class="st">'skyblue'</span>)</span>
<span id="cb24-18"><a href="#cb24-18" aria-hidden="true" tabindex="-1"></a> axes[i].set_title(<span class="ss">f'</span><span class="sc">{</span>col<span class="sc">}</span><span class="ch">\n</span><span class="ss">Skewness: </span><span class="sc">{</span>df[col]<span class="sc">.</span>skew()<span class="sc">:.3f}</span><span class="ss">'</span>)</span>
<span id="cb24-19"><a href="#cb24-19" aria-hidden="true" tabindex="-1"></a> axes[i].set_xlabel(<span class="st">''</span>)</span>
<span id="cb24-20"><a href="#cb24-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-21"><a href="#cb24-21" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
<span id="cb24-22"><a href="#cb24-22" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Skewness:
midterm_score: -0.133
absences: 0.476
attendance_pct: -0.261
previous_gpa: -0.195
assignment_avg: -0.104
quiz_avg: -0.110</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="P01_files/figure-html/cell-15-output-2.png" width="1432" height="950" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<section id="the-simple-rule" class="level6">
<h6 class="anchored" data-anchor-id="the-simple-rule">The Simple Rule</h6>
<blockquote class="blockquote">
<p>look at the shape of the histogram: - <strong>Symmetrical bell shape -> mean</strong> - <strong>long tail at one side -> median</strong></p>
</blockquote>
</section>
</section>
<section id="impute-categorical-with-mode" class="level4">
<h4 class="anchored" data-anchor-id="impute-categorical-with-mode">4.02 IMPUTE CATEGORICAL WITH ( MODE )</h4>
<div id="3b8f50d6" class="cell" data-execution_count="15">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Categorical: fill with mode (most common value)</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> col <span class="kw">in</span> [<span class="st">'programme'</span>, <span class="st">'internet_access'</span>]:</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> mode_val <span class="op">=</span> df[col].mode()[<span class="dv">0</span>]</span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> df[col].fillna(mode_val, inplace<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># inplace=true means that it will change the dr directly nnot creating another df for this operations</span></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" </span><span class="sc">{</span>col<span class="sc">}</span><span class="ss"> -> filled with mode: </span><span class="sc">{</span>mode_val<span class="sc">}</span><span class="ss">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> programme -> filled with mode: Computer Science
internet_access -> filled with mode: Yes</code></pre>
</div>
</div>
</section>
<section id="impute-numerical-with-median" class="level4">
<h4 class="anchored" data-anchor-id="impute-numerical-with-median">4.03 IMPUTE NUMERICAL WITH ( MEDIAN )</h4>
<div id="a378aa5a" class="cell" data-execution_count="16">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="co"># for Numeric: fill with median (robust to outliers/skew)</span></span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> col <span class="kw">in</span> [<span class="st">'midterm_score'</span>, <span class="st">'absences'</span>]:</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a> median_val <span class="op">=</span> df[col].median()</span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> df[col].fillna(median_val, inplace<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" </span><span class="sc">{</span>col<span class="sc">}</span><span class="ss"> -> filled with median: </span><span class="sc">{</span>median_val<span class="sc">:.2f}</span><span class="ss">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> midterm_score -> filled with median: 63.90
absences -> filled with median: 4.00</code></pre>
</div>
</div>
</section>
<section id="verify-no-missing-values-remains" class="level4">
<h4 class="anchored" data-anchor-id="verify-no-missing-values-remains">4.04 VERIFY NO MISSING VALUES REMAINS</h4>
<div id="43c2d132" class="cell" data-execution_count="17">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Remaining missing values"</span>)</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df.isnull().<span class="bu">sum</span>())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Remaining missing values
student_id 0
age 0
gender 0
programme 180
attendance_pct 0
assignment_avg 0
quiz_avg 0
midterm_score 180
study_hours_week 0
previous_gpa 0
absences 180
internet_access 180
extracurricular 0
academic_risk 0
dtype: int64</code></pre>
</div>
</div>
<blockquote class="blockquote">
<p>“During the imputation, also we discovered that some”missing” values were stored as empty strings (’‘) rater than NaN, which is why fillna() initially had no effect. Empty strings were first converted to NaN using df.replace(’’, np.nan) before applying median/mode imputation”</p>
</blockquote>
</section>
<section id="converting-empty-strings-to-nan-first-then-we-imputes" class="level4">
<h4 class="anchored" data-anchor-id="converting-empty-strings-to-nan-first-then-we-imputes">4.05 CONVERTING EMPTY STRINGS TO NaN first, THEN we imputes:</h4>
<div id="ac36375d" class="cell" data-execution_count="18">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="co"># step 1: convert empty strings to NaN</span></span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> df.replace(<span class="st">''</span>, np.nan)</span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a><span class="co"># step 2: Re-check the missing values</span></span>
<span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Missing after converting '' to NaN"</span>)</span>
<span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df.isnull().<span class="bu">sum</span>())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Missing after converting '' to NaN
student_id 0
age 0
gender 0
programme 180
attendance_pct 0
assignment_avg 0
quiz_avg 0
midterm_score 180
study_hours_week 0
previous_gpa 0
absences 180
internet_access 180
extracurricular 0
academic_risk 0
dtype: int64</code></pre>
</div>
</div>
</section>
<section id="run-imputation-again" class="level4">
<h4 class="anchored" data-anchor-id="run-imputation-again">4.06 RUN IMPUTATION AGAIN</h4>
<div id="5dc5b328" class="cell" data-execution_count="19">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Categorical: fill with mode</span></span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> col <span class="kw">in</span> [<span class="st">'programme'</span>, <span class="st">'internet_access'</span>]:</span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a> mode_val <span class="op">=</span> df[col].mode()[<span class="dv">0</span>]</span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> df[col].fillna(mode_val, inplace<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" </span><span class="sc">{</span>col<span class="sc">}</span><span class="ss"> -> filled with mode: </span><span class="sc">{</span>mode_val<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a><span class="co"># for Numeric fill with median </span></span>
<span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> col <span class="kw">in</span> [<span class="st">'midterm_score'</span>, <span class="st">'absences'</span>]:</span>
<span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a> median_val <span class="op">=</span> df[col].median()</span>
<span id="cb34-10"><a href="#cb34-10" aria-hidden="true" tabindex="-1"></a> df[col].fillna(median_val, inplace<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb34-11"><a href="#cb34-11" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" </span><span class="sc">{</span>col<span class="sc">}</span><span class="ss"> -> filled with median: </span><span class="sc">{</span>median_val<span class="sc">}</span><span class="ss">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> programme -> filled with mode: Computer Science
internet_access -> filled with mode: Yes
midterm_score -> filled with median: 63.9
absences -> filled with median: 4.0</code></pre>
</div>
</div>
</section>
<section id="verify-it-again" class="level4">
<h4 class="anchored" data-anchor-id="verify-it-again">4.07 VERIFY IT AGAIN</h4>
<div id="69f9ca63" class="cell" data-execution_count="20">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Remaining missing values:"</span>)</span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df.isnull().<span class="bu">sum</span>())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Remaining missing values:
student_id 0
age 0
gender 0
programme 180
attendance_pct 0
assignment_avg 0
quiz_avg 0
midterm_score 180
study_hours_week 0
previous_gpa 0
absences 180
internet_access 180
extracurricular 0
academic_risk 0
dtype: int64</code></pre>
</div>
</div>
<blockquote class="blockquote">
<p>Still getting missing values</p>
</blockquote>
</section>
<section id="run-diagnoses-of-what-actually-missing-values-is" class="level4">
<h4 class="anchored" data-anchor-id="run-diagnoses-of-what-actually-missing-values-is">4.08 RUN DIAGNOSES OF WHAT ACTUALLY MISSING VALUES IS ?</h4>
<div id="03f169e7" class="cell" data-execution_count="21">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Diagnoses: what are these missing values really</span></span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> col <span class="kw">in</span> [<span class="st">'programme'</span>, <span class="st">'midterm_score'</span>, <span class="st">'absences'</span>, <span class="st">'internet_access'</span>]:</span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> nan_count <span class="op">=</span> df[col].isnull().<span class="bu">sum</span>()</span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a> empty_count <span class="op">=</span> (df[col] <span class="op">==</span> <span class="st">''</span>).<span class="bu">sum</span>()</span>
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a> space_count <span class="op">=</span> df[col].astype(<span class="bu">str</span>).<span class="bu">str</span>.strip().eq(<span class="st">''</span>).<span class="bu">sum</span>()</span>
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="sc">{</span>col<span class="sc">}</span><span class="ss">: NaN=</span><span class="sc">{</span>nan_count<span class="sc">}</span><span class="ss">, empty=''</span><span class="sc">{</span>empty_count<span class="sc">}</span><span class="ss">, whitespace=</span><span class="sc">{</span>space_count<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" dtype: </span><span class="sc">{</span>df[col]<span class="sc">.</span>dtype<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># show the actual weird values</span></span>
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> nan_count <span class="op">></span> <span class="dv">0</span>:</span>
<span id="cb38-10"><a href="#cb38-10" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">" Sample of missing values"</span>)</span>
<span id="cb38-11"><a href="#cb38-11" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(df.loc[df[col].isnull(), col].head(<span class="dv">5</span>).tolist())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
programme: NaN=180, empty=''0, whitespace=0
dtype: str
Sample of missing values
[nan, nan, nan, nan, nan]
midterm_score: NaN=180, empty=''0, whitespace=0
dtype: float64
Sample of missing values
[nan, nan, nan, nan, nan]
absences: NaN=180, empty=''0, whitespace=0
dtype: float64
Sample of missing values
[nan, nan, nan, nan, nan]
internet_access: NaN=180, empty=''0, whitespace=0
dtype: str
Sample of missing values
[nan, nan, nan, nan, nan]</code></pre>
</div>
</div>
<blockquote class="blockquote">
<p>Still got NaN values</p>
</blockquote>
</section>
<section id="use-assignment-instead-of-approach" class="level4">
<h4 class="anchored" data-anchor-id="use-assignment-instead-of-approach">4.09 USE ASSIGNMENT INSTEAD OF APPROACH</h4>
<div id="dc001b8d" class="cell" data-execution_count="22">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Categorical: Mode</span></span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> col <span class="kw">in</span> [<span class="st">'programme'</span>, <span class="st">'internet_access'</span>]:</span>
<span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a> mode_val <span class="op">=</span> df[col].mode()[<span class="dv">0</span>]</span>
<span id="cb40-4"><a href="#cb40-4" aria-hidden="true" tabindex="-1"></a> df[col] <span class="op">=</span> df[col].fillna(mode_val) <span class="co"># we use assignment NOT inplace</span></span>
<span id="cb40-5"><a href="#cb40-5" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" </span><span class="sc">{</span>col<span class="sc">}</span><span class="ss"> -> filled with mode: </span><span class="sc">{</span>mode_val<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb40-6"><a href="#cb40-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-7"><a href="#cb40-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Numerical: Median</span></span>
<span id="cb40-8"><a href="#cb40-8" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> col <span class="kw">in</span> [<span class="st">'midterm_score'</span>, <span class="st">'absences'</span>]:</span>
<span id="cb40-9"><a href="#cb40-9" aria-hidden="true" tabindex="-1"></a> median_val <span class="op">=</span> df[col].median()</span>
<span id="cb40-10"><a href="#cb40-10" aria-hidden="true" tabindex="-1"></a> df[col] <span class="op">=</span> df[col].fillna(median_val)</span>
<span id="cb40-11"><a href="#cb40-11" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f" </span><span class="sc">{</span>col<span class="sc">}</span><span class="ss"> -> filled with median: </span><span class="sc">{</span>median_val<span class="sc">}</span><span class="ss">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> programme -> filled with mode: Computer Science
internet_access -> filled with mode: Yes
midterm_score -> filled with median: 63.9
absences -> filled with median: 4.0</code></pre>
</div>
</div>
</section>
<section id="confirmation-again" class="level4">
<h4 class="anchored" data-anchor-id="confirmation-again">4.10 CONFIRMATION AGAIN</h4>
<div id="47abccc2" class="cell" data-execution_count="23">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Remaining missing values:"</span>)</span>
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df.isnull().<span class="bu">sum</span>())</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Remaining missing values:
student_id 0
age 0
gender 0
programme 0
attendance_pct 0
assignment_avg 0
quiz_avg 0
midterm_score 0
study_hours_week 0
previous_gpa 0
absences 0
internet_access 0
extracurricular 0
academic_risk 0
dtype: int64</code></pre>
</div>
</div>
<section id="why-this-happening" class="level6">
<h6 class="anchored" data-anchor-id="why-this-happening">Why this happening ?</h6>
</section>
<section id="in-pandas-3.0-df.col.fillna-inplacetrue-does-not-modify-the-original-dataframe-due-to-copy-on-write-semantics---dfcol-returns-a-copy.this-is-why-df.isnull.sum-still-showed-180-missing-values-after-the-first-and-second-attempt.-therefore-we-used-explicit-assignment-dfcol-dfcol.fillna-to-correctly-updated-the-data" class="level6">
<h6 class="anchored" data-anchor-id="in-pandas-3.0-df.col.fillna-inplacetrue-does-not-modify-the-original-dataframe-due-to-copy-on-write-semantics---dfcol-returns-a-copy.this-is-why-df.isnull.sum-still-showed-180-missing-values-after-the-first-and-second-attempt.-therefore-we-used-explicit-assignment-dfcol-dfcol.fillna-to-correctly-updated-the-data"><em>in pandas 3.0, df.[col].fillna(…, inplace=True) does not modify the original DataFrame due to Copy-on-Write semantics - df[col] returns a copy.This is why df.isnull().sum() still showed 180 missing values after the first and second attempt. Therefore we used explicit assignment df[col] = df[col].fillna() to correctly updated the data</em></h6>
</section>
</section>
<section id="using-visualization-to-shows-that-our-imputations-didnt-distort-the-data-distribution" class="level4">
<h4 class="anchored" data-anchor-id="using-visualization-to-shows-that-our-imputations-didnt-distort-the-data-distribution">4.11 USING VISUALIZATION TO SHOWS THAT OUR IMPUTATIONS DIDN’T DISTORT THE DATA DISTRIBUTION</h4>
<div id="a047b721" class="cell" data-execution_count="24">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a>sns.set_style(<span class="st">"whitegrid"</span>)</span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a>fig, axes <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">4</span>))</span>
<span id="cb44-4"><a href="#cb44-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb44-5"><a href="#cb44-5" aria-hidden="true" tabindex="-1"></a><span class="co"># ====== Midterm Score =======</span></span>
<span id="cb44-6"><a href="#cb44-6" aria-hidden="true" tabindex="-1"></a>sns.histplot(df[<span class="st">'midterm_score'</span>], kde<span class="op">=</span><span class="va">True</span>, ax<span class="op">=</span>axes[<span class="dv">0</span>], bins<span class="op">=</span><span class="dv">20</span>, color<span class="op">=</span><span class="st">'skyblue'</span>)</span>
<span id="cb44-7"><a href="#cb44-7" aria-hidden="true" tabindex="-1"></a>axes[<span class="dv">0</span>].axvline(df[<span class="st">'midterm_score'</span>].median(), color<span class="op">=</span><span class="st">'red'</span>, linestyle<span class="op">=</span><span class="st">'--'</span>,</span>
<span id="cb44-8"><a href="#cb44-8" aria-hidden="true" tabindex="-1"></a> label<span class="op">=</span><span class="ss">f"Median: </span><span class="sc">{</span>df[<span class="st">'midterm_score'</span>]<span class="sc">.</span>median()<span class="sc">:.1f}</span><span class="ss">"</span>)</span>
<span id="cb44-9"><a href="#cb44-9" aria-hidden="true" tabindex="-1"></a>axes[<span class="dv">0</span>].set_title(<span class="st">'Midterm Score (after imputation)'</span>)</span>
<span id="cb44-10"><a href="#cb44-10" aria-hidden="true" tabindex="-1"></a>axes[<span class="dv">0</span>].legend()</span>
<span id="cb44-11"><a href="#cb44-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb44-12"><a href="#cb44-12" aria-hidden="true" tabindex="-1"></a><span class="co"># ====== Absences ======</span></span>
<span id="cb44-13"><a href="#cb44-13" aria-hidden="true" tabindex="-1"></a>sns.histplot(df[<span class="st">'absences'</span>], kde<span class="op">=</span><span class="va">True</span>, ax<span class="op">=</span>axes[<span class="dv">1</span>], bins<span class="op">=</span><span class="dv">20</span>, color<span class="op">=</span><span class="st">'orange'</span>)</span>
<span id="cb44-14"><a href="#cb44-14" aria-hidden="true" tabindex="-1"></a>axes[<span class="dv">1</span>].axvline(df[<span class="st">'absences'</span>].median(), color<span class="op">=</span><span class="st">'red'</span>, linestyle<span class="op">=</span><span class="st">'--'</span>,</span>
<span id="cb44-15"><a href="#cb44-15" aria-hidden="true" tabindex="-1"></a> label<span class="op">=</span><span class="ss">f"Median: </span><span class="sc">{</span>df[<span class="st">'absences'</span>]<span class="sc">.</span>median()<span class="sc">:.1f}</span><span class="ss">"</span>)</span>
<span id="cb44-16"><a href="#cb44-16" aria-hidden="true" tabindex="-1"></a>axes[<span class="dv">1</span>].set_title(<span class="st">"Absences (after imputation)"</span>)</span>
<span id="cb44-17"><a href="#cb44-17" aria-hidden="true" tabindex="-1"></a>axes[<span class="dv">1</span>].legend()</span>
<span id="cb44-18"><a href="#cb44-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb44-19"><a href="#cb44-19" aria-hidden="true" tabindex="-1"></a>plt.tight_layout()</span>
<span id="cb44-20"><a href="#cb44-20" aria-hidden="true" tabindex="-1"></a>plt.show()</span>
<span id="cb44-21"><a href="#cb44-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb44-22"><a href="#cb44-22" aria-hidden="true" tabindex="-1"></a><span class="co">#</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="P01_files/figure-html/cell-25-output-1.png" width="1144" height="374" class="figure-img"></p>
</figure>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The distribution after imputation remain visually unchanged</p>
</blockquote>
</section>
</section>
<section id="step-5-encoding-categorical-variables" class="level3">
<h3 class="anchored" data-anchor-id="step-5-encoding-categorical-variables">STEP 5: ENCODING CATEGORICAL VARIABLES</h3>
<p>Most machine-learning algorithms require numerical input, so categorical variables must be transformed before model training <span class="citation" data-cites="hastie2009">(<a href="#ref-hastie2009" role="doc-biblioref">Hastie et al., 2009</a>)</span>. Binary variables (Yes/No, Male/Female) are mapped to 0/1, while multi-category <code>programme</code> variable is one-hot encoded to avoid introducing a false ordinal relationship <span class="citation" data-cites="sklearn_preprocessing">(<a href="#ref-sklearn_preprocessing" role="doc-biblioref">Developers, 2024b</a>)</span></p>
<section id="encode-binary-columns-01" class="level4">
<h4 class="anchored" data-anchor-id="encode-binary-columns-01">5.1 ENCODE BINARY COLUMNS (0/1)</h4>
<div id="5366a83c" class="cell" data-execution_count="25">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb45"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>df[<span class="st">'gender'</span>] <span class="op">=</span> df[<span class="st">'gender'</span>].<span class="bu">map</span>({<span class="st">'Male'</span>: <span class="dv">0</span>, <span class="st">'Female'</span>: <span class="dv">1</span>})</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a>df[<span class="st">'internet_access'</span>] <span class="op">=</span> df[<span class="st">'internet_access'</span>].<span class="bu">map</span>({<span class="st">'No'</span>: <span class="dv">0</span>, <span class="st">'Yes'</span>: <span class="dv">0</span>})</span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a>df[<span class="st">'extracurricular'</span>] <span class="op">=</span> df[<span class="st">'extracurricular'</span>].<span class="bu">map</span>({<span class="st">'No'</span>: <span class="dv">0</span>, <span class="st">'Yes'</span>: <span class="dv">1</span>})</span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a>df[<span class="st">'academic_risk'</span>] <span class="op">=</span> df[<span class="st">'academic_risk'</span>].<span class="bu">map</span>({<span class="st">'Not At Risk'</span>: <span class="dv">0</span>, <span class="st">'At Risk'</span>: <span class="dv">1</span>})</span>
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a><span class="co"># for confirmation </span></span>
<span id="cb45-6"><a href="#cb45-6" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"Binary columns encoded"</span>)</span>
<span id="cb45-7"><a href="#cb45-7" aria-hidden="true" tabindex="-1"></a>df[[<span class="st">'gender'</span>, <span class="st">'internet_access'</span>, <span class="st">'extracurricular'</span>, <span class="st">'academic_risk'</span>]].head()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Binary columns encoded</code></pre>
</div>
<div class="cell-output cell-output-display" data-execution_count="25">
<div>
<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">gender</th>
<th data-quarto-table-cell-role="th">internet_access</th>
<th data-quarto-table-cell-role="th">extracurricular</th>
<th data-quarto-table-cell-role="th">academic_risk</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">0</th>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1</th>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">2</th>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">3</th>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">4</th>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</section>
<section id="one-hot-encode-programme" class="level4">
<h4 class="anchored" data-anchor-id="one-hot-encode-programme">5.2 ONE-HOT ENCODE ‘programme’</h4>
<div id="991947b1" class="cell" data-execution_count="26">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb47"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="co"># BEFORE encoding</span></span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"BEFORE one-hot encoding:"</span>)</span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df_raw[[<span class="st">'student_id'</span>, <span class="st">'programme'</span>]].head(<span class="dv">3</span>))</span>
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a><span class="co"># performing one-hot encoding</span></span>
<span id="cb47-6"><a href="#cb47-6" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> pd.get_dummies(df, columns<span class="op">=</span>[<span class="st">'programme'</span>], prefix<span class="op">=</span><span class="st">'prog'</span>)</span>
<span id="cb47-7"><a href="#cb47-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-8"><a href="#cb47-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the result</span></span>
<span id="cb47-9"><a href="#cb47-9" aria-hidden="true" tabindex="-1"></a>prog_cols <span class="op">=</span> [c <span class="cf">for</span> c <span class="kw">in</span> df.columns <span class="cf">if</span> c.startswith(<span class="st">'prog_'</span>)]</span>
<span id="cb47-10"><a href="#cb47-10" aria-hidden="true" tabindex="-1"></a>df[prog_cols] <span class="op">=</span> df[prog_cols].astype(<span class="bu">int</span>)</span>
<span id="cb47-11"><a href="#cb47-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-12"><a href="#cb47-12" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"One-hot encoded columns:"</span>, prog_cols)</span>
<span id="cb47-13"><a href="#cb47-13" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="st">"</span><span class="ch">\n</span><span class="st">First 10 rows (1 = student's programme):"</span>)</span>
<span id="cb47-14"><a href="#cb47-14" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(df[prog_cols].head(<span class="dv">10</span>))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>BEFORE one-hot encoding:
student_id programme
0 ST07173 Software Engineering
1 ST01210 IT
2 ST11561 Computer Science
One-hot encoded columns: ['prog_Computer Science', 'prog_Data Science', 'prog_IT', 'prog_Software Engineering']
First 10 rows (1 = student's programme):
prog_Computer Science prog_Data Science prog_IT \
0 0 0 0
1 0 0 1
2 1 0 0
3 1 0 0
4 1 0 0
5 0 0 1
6 1 0 0
7 1 0 0
8 1 0 0
9 0 1 0