mirror of
https://github.com/andwati/student-retention.git
synced 2026-10-18 08:52:53 +00:00
4.0 MiB
4.0 MiB
In [1]:
# expand border width
from IPython.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
# Needed libraries
# Data manipulation
import pandas as pd
import numpy as np
# Data Visualization
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# hide warning messages
import warnings
warnings.filterwarnings('ignore')
pd.set_option('display.max_columns', None) # Show all columns
pd.set_option('display.width', 150) # Adjust the width as neededIn [2]:
#from google.colab import drive
#drive.mount('/content/drive')
dataFileNamePath = "./data/data.csv"In [3]:
masterRawData = pd.read_csv(dataFileNamePath)
print("The dataframe shape is: ", masterRawData.shape, ": ", masterRawData.shape[0], "- Rows, ", masterRawData.shape[1], "- Columns\n")
masterRawData.head()
Out [3]:
The dataframe shape is: (4424, 37) : 4424 - Rows, 37 - Columns
| Marital status | Application mode | Application order | Course | Daytime/evening attendance\t | Previous qualification | Previous qualification (grade) | Nacionality | Mother's qualification | Father's qualification | Mother's occupation | Father's occupation | Admission grade | Displaced | Educational special needs | Debtor | Tuition fees up to date | Gender | Scholarship holder | Age at enrollment | International | Curricular units 1st sem (credited) | Curricular units 1st sem (enrolled) | Curricular units 1st sem (evaluations) | Curricular units 1st sem (approved) | Curricular units 1st sem (grade) | Curricular units 1st sem (without evaluations) | Curricular units 2nd sem (credited) | Curricular units 2nd sem (enrolled) | Curricular units 2nd sem (evaluations) | Curricular units 2nd sem (approved) | Curricular units 2nd sem (grade) | Curricular units 2nd sem (without evaluations) | Unemployment rate | Inflation rate | GDP | Target | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 17 | 5 | 171 | 1 | 1 | 122.0 | 1 | 19 | 12 | 5 | 9 | 127.3 | 1 | 0 | 0 | 1 | 1 | 0 | 20 | 0 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 10.8 | 1.4 | 1.74 | Dropout |
| 1 | 1 | 15 | 1 | 9254 | 1 | 1 | 160.0 | 1 | 1 | 3 | 3 | 3 | 142.5 | 1 | 0 | 0 | 0 | 1 | 0 | 19 | 0 | 0 | 6 | 6 | 6 | 14.000000 | 0 | 0 | 6 | 6 | 6 | 13.666667 | 0 | 13.9 | -0.3 | 0.79 | Graduate |
| 2 | 1 | 1 | 5 | 9070 | 1 | 1 | 122.0 | 1 | 37 | 37 | 9 | 9 | 124.8 | 1 | 0 | 0 | 0 | 1 | 0 | 19 | 0 | 0 | 6 | 0 | 0 | 0.000000 | 0 | 0 | 6 | 0 | 0 | 0.000000 | 0 | 10.8 | 1.4 | 1.74 | Dropout |
| 3 | 1 | 17 | 2 | 9773 | 1 | 1 | 122.0 | 1 | 38 | 37 | 5 | 3 | 119.6 | 1 | 0 | 0 | 1 | 0 | 0 | 20 | 0 | 0 | 6 | 8 | 6 | 13.428571 | 0 | 0 | 6 | 10 | 5 | 12.400000 | 0 | 9.4 | -0.8 | -3.12 | Graduate |
| 4 | 2 | 39 | 1 | 8014 | 0 | 1 | 100.0 | 1 | 37 | 38 | 9 | 9 | 141.5 | 0 | 0 | 0 | 1 | 0 | 0 | 45 | 0 | 0 | 6 | 9 | 5 | 12.333333 | 0 | 0 | 6 | 6 | 6 | 13.000000 | 0 | 13.9 | -0.3 | 0.79 | Graduate |
In [4]:
data = masterRawData.copy()In [5]:
print("Head\n", data.head()) # prefer to use print here with display width adjusted to force wrapping text to view all columns at once and avoid having to scroll left to right
print("\nTail\n", data.tail())Head
Marital status Application mode Application order Course Daytime/evening attendance\t Previous qualification \
0 1 17 5 171 1 1
1 1 15 1 9254 1 1
2 1 1 5 9070 1 1
3 1 17 2 9773 1 1
4 2 39 1 8014 0 1
Previous qualification (grade) Nacionality Mother's qualification Father's qualification Mother's occupation Father's occupation \
0 122.0 1 19 12 5 9
1 160.0 1 1 3 3 3
2 122.0 1 37 37 9 9
3 122.0 1 38 37 5 3
4 100.0 1 37 38 9 9
Admission grade Displaced Educational special needs Debtor Tuition fees up to date Gender Scholarship holder Age at enrollment \
0 127.3 1 0 0 1 1 0 20
1 142.5 1 0 0 0 1 0 19
2 124.8 1 0 0 0 1 0 19
3 119.6 1 0 0 1 0 0 20
4 141.5 0 0 0 1 0 0 45
International Curricular units 1st sem (credited) Curricular units 1st sem (enrolled) Curricular units 1st sem (evaluations) \
0 0 0 0 0
1 0 0 6 6
2 0 0 6 0
3 0 0 6 8
4 0 0 6 9
Curricular units 1st sem (approved) Curricular units 1st sem (grade) Curricular units 1st sem (without evaluations) \
0 0 0.000000 0
1 6 14.000000 0
2 0 0.000000 0
3 6 13.428571 0
4 5 12.333333 0
Curricular units 2nd sem (credited) Curricular units 2nd sem (enrolled) Curricular units 2nd sem (evaluations) \
0 0 0 0
1 0 6 6
2 0 6 0
3 0 6 10
4 0 6 6
Curricular units 2nd sem (approved) Curricular units 2nd sem (grade) Curricular units 2nd sem (without evaluations) Unemployment rate \
0 0 0.000000 0 10.8
1 6 13.666667 0 13.9
2 0 0.000000 0 10.8
3 5 12.400000 0 9.4
4 6 13.000000 0 13.9
Inflation rate GDP Target
0 1.4 1.74 Dropout
1 -0.3 0.79 Graduate
2 1.4 1.74 Dropout
3 -0.8 -3.12 Graduate
4 -0.3 0.79 Graduate
Tail
Marital status Application mode Application order Course Daytime/evening attendance\t Previous qualification \
4419 1 1 6 9773 1 1
4420 1 1 2 9773 1 1
4421 1 1 1 9500 1 1
4422 1 1 1 9147 1 1
4423 1 10 1 9773 1 1
Previous qualification (grade) Nacionality Mother's qualification Father's qualification Mother's occupation Father's occupation \
4419 125.0 1 1 1 5 4
4420 120.0 105 1 1 9 9
4421 154.0 1 37 37 9 9
4422 180.0 1 37 37 7 4
4423 152.0 22 38 37 5 9
Admission grade Displaced Educational special needs Debtor Tuition fees up to date Gender Scholarship holder Age at enrollment \
4419 122.2 0 0 0 1 1 0 19
4420 119.0 1 0 1 0 0 0 18
4421 149.5 1 0 0 1 0 1 30
4422 153.8 1 0 0 1 0 1 20
4423 152.0 1 0 0 1 0 0 22
International Curricular units 1st sem (credited) Curricular units 1st sem (enrolled) Curricular units 1st sem (evaluations) \
4419 0 0 6 7
4420 1 0 6 6
4421 0 0 7 8
4422 0 0 5 5
4423 1 0 6 8
Curricular units 1st sem (approved) Curricular units 1st sem (grade) Curricular units 1st sem (without evaluations) \
4419 5 13.600000 0
4420 6 12.000000 0
4421 7 14.912500 0
4422 5 13.800000 0
4423 6 11.666667 0
Curricular units 2nd sem (credited) Curricular units 2nd sem (enrolled) Curricular units 2nd sem (evaluations) \
4419 0 6 8
4420 0 6 6
4421 0 8 9
4422 0 5 6
4423 0 6 6
Curricular units 2nd sem (approved) Curricular units 2nd sem (grade) Curricular units 2nd sem (without evaluations) Unemployment rate \
4419 5 12.666667 0 15.5
4420 2 11.000000 0 11.1
4421 1 13.500000 0 13.9
4422 5 12.000000 0 9.4
4423 6 13.000000 0 12.7
Inflation rate GDP Target
4419 2.8 -4.06 Graduate
4420 0.6 2.02 Dropout
4421 -0.3 0.79 Dropout
4422 -0.8 -3.12 Graduate
4423 3.7 -1.70 Graduate
Marital status Application mode Application order Course Daytime/evening attendance\t Previous qualification \
4419 1 1 6 9773 1 1
4420 1 1 2 9773 1 1
4421 1 1 1 9500 1 1
4422 1 1 1 9147 1 1
4423 1 10 1 9773 1 1
Previous qualification (grade) Nacionality Mother's qualification Father's qualification Mother's occupation Father's occupation \
4419 125.0 1 1 1 5 4
4420 120.0 105 1 1 9 9
4421 154.0 1 37 37 9 9
4422 180.0 1 37 37 7 4
4423 152.0 22 38 37 5 9
Admission grade Displaced Educational special needs Debtor Tuition fees up to date Gender Scholarship holder Age at enrollment \
4419 122.2 0 0 0 1 1 0 19
4420 119.0 1 0 1 0 0 0 18
4421 149.5 1 0 0 1 0 1 30
4422 153.8 1 0 0 1 0 1 20
4423 152.0 1 0 0 1 0 0 22
International Curricular units 1st sem (credited) Curricular units 1st sem (enrolled) Curricular units 1st sem (evaluations) \
4419 0 0 6 7
4420 1 0 6 6
4421 0 0 7 8
4422 0 0 5 5
4423 1 0 6 8
Curricular units 1st sem (approved) Curricular units 1st sem (grade) Curricular units 1st sem (without evaluations) \
4419 5 13.600000 0
4420 6 12.000000 0
4421 7 14.912500 0
4422 5 13.800000 0
4423 6 11.666667 0
Curricular units 2nd sem (credited) Curricular units 2nd sem (enrolled) Curricular units 2nd sem (evaluations) \
4419 0 6 8
4420 0 6 6
4421 0 8 9
4422 0 5 6
4423 0 6 6
Curricular units 2nd sem (approved) Curricular units 2nd sem (grade) Curricular units 2nd sem (without evaluations) Unemployment rate \
4419 5 12.666667 0 15.5
4420 2 11.000000 0 11.1
4421 1 13.500000 0 13.9
4422 5 12.000000 0 9.4
4423 6 13.000000 0 12.7
Inflation rate GDP Target
4419 2.8 -4.06 Graduate
4420 0.6 2.02 Dropout
4421 -0.3 0.79 Dropout
4422 -0.8 -3.12 Graduate
4423 3.7 -1.70 Graduate
In [6]:
# fix Nacionality column name typo to Nationality
data.rename(columns={'Nacionality': 'Nationality'}, inplace=True)
print("Sample\n", data['Nationality'].sample())Sample 1634 1 Name: Nationality, dtype: int64
In [7]:
# check for nulls in the columns
data.info()<class 'pandas.core.frame.DataFrame'> RangeIndex: 4424 entries, 0 to 4423 Data columns (total 37 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Marital status 4424 non-null int64 1 Application mode 4424 non-null int64 2 Application order 4424 non-null int64 3 Course 4424 non-null int64 4 Daytime/evening attendance 4424 non-null int64 5 Previous qualification 4424 non-null int64 6 Previous qualification (grade) 4424 non-null float64 7 Nationality 4424 non-null int64 8 Mother's qualification 4424 non-null int64 9 Father's qualification 4424 non-null int64 10 Mother's occupation 4424 non-null int64 11 Father's occupation 4424 non-null int64 12 Admission grade 4424 non-null float64 13 Displaced 4424 non-null int64 14 Educational special needs 4424 non-null int64 15 Debtor 4424 non-null int64 16 Tuition fees up to date 4424 non-null int64 17 Gender 4424 non-null int64 18 Scholarship holder 4424 non-null int64 19 Age at enrollment 4424 non-null int64 20 International 4424 non-null int64 21 Curricular units 1st sem (credited) 4424 non-null int64 22 Curricular units 1st sem (enrolled) 4424 non-null int64 23 Curricular units 1st sem (evaluations) 4424 non-null int64 24 Curricular units 1st sem (approved) 4424 non-null int64 25 Curricular units 1st sem (grade) 4424 non-null float64 26 Curricular units 1st sem (without evaluations) 4424 non-null int64 27 Curricular units 2nd sem (credited) 4424 non-null int64 28 Curricular units 2nd sem (enrolled) 4424 non-null int64 29 Curricular units 2nd sem (evaluations) 4424 non-null int64 30 Curricular units 2nd sem (approved) 4424 non-null int64 31 Curricular units 2nd sem (grade) 4424 non-null float64 32 Curricular units 2nd sem (without evaluations) 4424 non-null int64 33 Unemployment rate 4424 non-null float64 34 Inflation rate 4424 non-null float64 35 GDP 4424 non-null float64 36 Target 4424 non-null object dtypes: float64(7), int64(29), object(1) memory usage: 1.2+ MB
In [8]:
print("Total Null Values: ", data.isna().sum().sum())Total Null Values: 0
In [9]:
print(f"Duplicated rows: {data.duplicated().sum()}")Duplicated rows: 0
In [10]:
for column_name in data.columns:
# Check if the column name contains an apostrophe
if "'" in column_name:
# Remove the apostrophe and update the column name
new_column_name = column_name.replace("'", "")
data.rename(columns={column_name: new_column_name}, inplace=True)In [11]:
# check if the apostrophes were removed
print(data.sample()) Marital status Application mode Application order Course Daytime/evening attendance\t Previous qualification \
38 4 39 1 9991 0 1
Previous qualification (grade) Nationality Mothers qualification Fathers qualification Mothers occupation Fathers occupation \
38 120.0 1 19 19 9 3
Admission grade Displaced Educational special needs Debtor Tuition fees up to date Gender Scholarship holder Age at enrollment \
38 113.9 1 0 0 1 0 0 39
International Curricular units 1st sem (credited) Curricular units 1st sem (enrolled) Curricular units 1st sem (evaluations) \
38 0 0 5 8
Curricular units 1st sem (approved) Curricular units 1st sem (grade) Curricular units 1st sem (without evaluations) \
38 5 11.0 0
Curricular units 2nd sem (credited) Curricular units 2nd sem (enrolled) Curricular units 2nd sem (evaluations) \
38 0 5 5
Curricular units 2nd sem (approved) Curricular units 2nd sem (grade) Curricular units 2nd sem (without evaluations) Unemployment rate \
38 5 11.6 0 7.6
Inflation rate GDP Target
38 2.6 0.32 Graduate
In [12]:
# Number of unique values per column in ascending order
data.nunique().sort_values()Out [12]:
Daytime/evening attendance\t 2 Displaced 2 Debtor 2 Educational special needs 2 International 2 Scholarship holder 2 Gender 2 Tuition fees up to date 2 Target 3 Marital status 6 Application order 8 Inflation rate 9 Curricular units 2nd sem (without evaluations) 10 Unemployment rate 10 GDP 10 Curricular units 1st sem (without evaluations) 11 Course 17 Previous qualification 17 Application mode 18 Curricular units 2nd sem (credited) 19 Curricular units 2nd sem (approved) 20 Nationality 21 Curricular units 1st sem (credited) 21 Curricular units 2nd sem (enrolled) 22 Curricular units 1st sem (enrolled) 23 Curricular units 1st sem (approved) 23 Mothers qualification 29 Curricular units 2nd sem (evaluations) 30 Mothers occupation 32 Fathers qualification 34 Curricular units 1st sem (evaluations) 35 Fathers occupation 46 Age at enrollment 46 Previous qualification (grade) 101 Admission grade 620 Curricular units 2nd sem (grade) 782 Curricular units 1st sem (grade) 797 dtype: int64
In [13]:
data.filter(like='Curricular').sample(10)Out [13]:
| Curricular units 1st sem (credited) | Curricular units 1st sem (enrolled) | Curricular units 1st sem (evaluations) | Curricular units 1st sem (approved) | Curricular units 1st sem (grade) | Curricular units 1st sem (without evaluations) | Curricular units 2nd sem (credited) | Curricular units 2nd sem (enrolled) | Curricular units 2nd sem (evaluations) | Curricular units 2nd sem (approved) | Curricular units 2nd sem (grade) | Curricular units 2nd sem (without evaluations) | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4081 | 0 | 5 | 9 | 2 | 11.500000 | 0 | 0 | 5 | 13 | 4 | 11.00 | 1 |
| 1630 | 0 | 5 | 9 | 4 | 12.250000 | 2 | 0 | 5 | 8 | 4 | 11.75 | 0 |
| 957 | 0 | 6 | 6 | 6 | 12.500000 | 0 | 0 | 6 | 7 | 5 | 13.00 | 0 |
| 2876 | 0 | 7 | 7 | 0 | 0.000000 | 0 | 0 | 5 | 10 | 0 | 0.00 | 0 |
| 1089 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0.00 | 0 |
| 1596 | 0 | 7 | 9 | 6 | 12.833333 | 0 | 0 | 8 | 12 | 6 | 12.50 | 0 |
| 1549 | 0 | 8 | 8 | 0 | 0.000000 | 1 | 0 | 8 | 8 | 0 | 0.00 | 1 |
| 3560 | 0 | 7 | 7 | 5 | 13.060000 | 0 | 0 | 7 | 9 | 0 | 0.00 | 0 |
| 3824 | 0 | 5 | 5 | 0 | 0.000000 | 0 | 0 | 5 | 6 | 1 | 15.00 | 0 |
| 2156 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0.00 | 0 |
In [14]:
data.describe().TOut [14]:
| count | mean | std | min | 25% | 50% | 75% | max | |
|---|---|---|---|---|---|---|---|---|
| Marital status | 4424.0 | 1.178571 | 0.605747 | 1.00 | 1.00 | 1.000000 | 1.000000 | 6.000000 |
| Application mode | 4424.0 | 18.669078 | 17.484682 | 1.00 | 1.00 | 17.000000 | 39.000000 | 57.000000 |
| Application order | 4424.0 | 1.727848 | 1.313793 | 0.00 | 1.00 | 1.000000 | 2.000000 | 9.000000 |
| Course | 4424.0 | 8856.642631 | 2063.566416 | 33.00 | 9085.00 | 9238.000000 | 9556.000000 | 9991.000000 |
| Daytime/evening attendance\t | 4424.0 | 0.890823 | 0.311897 | 0.00 | 1.00 | 1.000000 | 1.000000 | 1.000000 |
| Previous qualification | 4424.0 | 4.577758 | 10.216592 | 1.00 | 1.00 | 1.000000 | 1.000000 | 43.000000 |
| Previous qualification (grade) | 4424.0 | 132.613314 | 13.188332 | 95.00 | 125.00 | 133.100000 | 140.000000 | 190.000000 |
| Nationality | 4424.0 | 1.873192 | 6.914514 | 1.00 | 1.00 | 1.000000 | 1.000000 | 109.000000 |
| Mothers qualification | 4424.0 | 19.561935 | 15.603186 | 1.00 | 2.00 | 19.000000 | 37.000000 | 44.000000 |
| Fathers qualification | 4424.0 | 22.275316 | 15.343108 | 1.00 | 3.00 | 19.000000 | 37.000000 | 44.000000 |
| Mothers occupation | 4424.0 | 10.960895 | 26.418253 | 0.00 | 4.00 | 5.000000 | 9.000000 | 194.000000 |
| Fathers occupation | 4424.0 | 11.032324 | 25.263040 | 0.00 | 4.00 | 7.000000 | 9.000000 | 195.000000 |
| Admission grade | 4424.0 | 126.978119 | 14.482001 | 95.00 | 117.90 | 126.100000 | 134.800000 | 190.000000 |
| Displaced | 4424.0 | 0.548373 | 0.497711 | 0.00 | 0.00 | 1.000000 | 1.000000 | 1.000000 |
| Educational special needs | 4424.0 | 0.011528 | 0.106760 | 0.00 | 0.00 | 0.000000 | 0.000000 | 1.000000 |
| Debtor | 4424.0 | 0.113698 | 0.317480 | 0.00 | 0.00 | 0.000000 | 0.000000 | 1.000000 |
| Tuition fees up to date | 4424.0 | 0.880651 | 0.324235 | 0.00 | 1.00 | 1.000000 | 1.000000 | 1.000000 |
| Gender | 4424.0 | 0.351718 | 0.477560 | 0.00 | 0.00 | 0.000000 | 1.000000 | 1.000000 |
| Scholarship holder | 4424.0 | 0.248418 | 0.432144 | 0.00 | 0.00 | 0.000000 | 0.000000 | 1.000000 |
| Age at enrollment | 4424.0 | 23.265145 | 7.587816 | 17.00 | 19.00 | 20.000000 | 25.000000 | 70.000000 |
| International | 4424.0 | 0.024864 | 0.155729 | 0.00 | 0.00 | 0.000000 | 0.000000 | 1.000000 |
| Curricular units 1st sem (credited) | 4424.0 | 0.709991 | 2.360507 | 0.00 | 0.00 | 0.000000 | 0.000000 | 20.000000 |
| Curricular units 1st sem (enrolled) | 4424.0 | 6.270570 | 2.480178 | 0.00 | 5.00 | 6.000000 | 7.000000 | 26.000000 |
| Curricular units 1st sem (evaluations) | 4424.0 | 8.299051 | 4.179106 | 0.00 | 6.00 | 8.000000 | 10.000000 | 45.000000 |
| Curricular units 1st sem (approved) | 4424.0 | 4.706600 | 3.094238 | 0.00 | 3.00 | 5.000000 | 6.000000 | 26.000000 |
| Curricular units 1st sem (grade) | 4424.0 | 10.640822 | 4.843663 | 0.00 | 11.00 | 12.285714 | 13.400000 | 18.875000 |
| Curricular units 1st sem (without evaluations) | 4424.0 | 0.137658 | 0.690880 | 0.00 | 0.00 | 0.000000 | 0.000000 | 12.000000 |
| Curricular units 2nd sem (credited) | 4424.0 | 0.541817 | 1.918546 | 0.00 | 0.00 | 0.000000 | 0.000000 | 19.000000 |
| Curricular units 2nd sem (enrolled) | 4424.0 | 6.232143 | 2.195951 | 0.00 | 5.00 | 6.000000 | 7.000000 | 23.000000 |
| Curricular units 2nd sem (evaluations) | 4424.0 | 8.063291 | 3.947951 | 0.00 | 6.00 | 8.000000 | 10.000000 | 33.000000 |
| Curricular units 2nd sem (approved) | 4424.0 | 4.435805 | 3.014764 | 0.00 | 2.00 | 5.000000 | 6.000000 | 20.000000 |
| Curricular units 2nd sem (grade) | 4424.0 | 10.230206 | 5.210808 | 0.00 | 10.75 | 12.200000 | 13.333333 | 18.571429 |
| Curricular units 2nd sem (without evaluations) | 4424.0 | 0.150316 | 0.753774 | 0.00 | 0.00 | 0.000000 | 0.000000 | 12.000000 |
| Unemployment rate | 4424.0 | 11.566139 | 2.663850 | 7.60 | 9.40 | 11.100000 | 13.900000 | 16.200000 |
| Inflation rate | 4424.0 | 1.228029 | 1.382711 | -0.80 | 0.30 | 1.400000 | 2.600000 | 3.700000 |
| GDP | 4424.0 | 0.001969 | 2.269935 | -4.06 | -1.70 | 0.320000 | 1.790000 | 3.510000 |
In [15]:
# plot out the numeric columns to view the distribution
numericColumns = data.loc[:, data.select_dtypes(include=['number']).columns]
numericColumns.shapeOut [15]:
(4424, 36)
In [16]:
# plot layout of numeric columns
plt.subplots(12, 3, figsize=(20, 40)) # using a 4 x 7 grid of graphs since we are working with 28 columns; each graph will be 20x20 in size
plt.suptitle("Numeric Column Distributions", size=20)
for i , column in enumerate(numericColumns.columns, 1):
plt.subplot(12, 3 , i)
sns.histplot(numericColumns[column])
plt.xticks(rotation=90)
# titles and labels
plt.title(column)
plt.xlabel(column)
plt.ylabel("Frequency")
# include median and mean lines
plt.axvline(data[column].mean(), c='blue', label='Mean')
plt.axvline(data[column].median(), c='green', label='Median')
plt.legend()
plt.tight_layout()
plt.show()In [17]:
unique_Country_Combinations = data[['Unemployment rate', 'Inflation rate', 'GDP']].drop_duplicates().sort_index()
print('Total Unique Rows:', unique_Country_Combinations.shape[0])
print(unique_Country_Combinations)Total Unique Rows: 10
Unemployment rate Inflation rate GDP
0 10.8 1.4 1.74
1 13.9 -0.3 0.79
3 9.4 -0.8 -3.12
5 16.2 0.3 -0.92
6 15.5 2.8 -4.06
9 8.9 1.4 3.51
11 12.7 3.7 -1.70
20 11.1 0.6 2.02
25 7.6 2.6 0.32
33 12.4 0.5 1.79
In [18]:
data.describe(include='object')Out [18]:
| Target | |
|---|---|
| count | 4424 |
| unique | 3 |
| top | Graduate |
| freq | 2209 |
In [19]:
sns.countplot(x='Target', data=data)
plt.title('Distribution of Target Classes')
# show percentage of each Target value on plot
total = len(data)
for p in plt.gca().patches:
height = p.get_height()
plt.text(p.get_x() + p.get_width() / 2.,
height + 0.1,
'{:.1f}%'.format((height / total) * 100),
ha="center")
plt.show()In [20]:
from sklearn.preprocessing import LabelEncoder
class_mapping = {'Graduate': 2, 'Enrolled': 3, 'Dropout': 1}
data['TargetEncoded'] = data['Target'].map(class_mapping)
print('Sample rows of Target variable: ', data['TargetEncoded'].sample(10))Sample rows of Target variable: 3377 2 1297 1 3467 2 1444 2 4036 2 826 1 2124 2 1891 2 4012 1 1928 3 Name: TargetEncoded, dtype: int64
In [21]:
sns.countplot(x='TargetEncoded', data=data)
plt.title('Distribution of Target Classes')
# show percentage of each Target value on plot
total = len(data)
for p in plt.gca().patches:
height = p.get_height()
plt.text(p.get_x() + p.get_width() / 2.,
height + 0.1,
'{:.1f}%'.format((height / total) * 100),
ha="center")
plt.show()In [22]:
data.drop(columns=['Target'], inplace=True)In [23]:
# Check that the original Target columns is removed
print(data.sample()) Marital status Application mode Application order Course Daytime/evening attendance\t Previous qualification \
4134 1 1 1 9238 1 1
Previous qualification (grade) Nationality Mothers qualification Fathers qualification Mothers occupation Fathers occupation \
4134 142.0 1 34 38 4 7
Admission grade Displaced Educational special needs Debtor Tuition fees up to date Gender Scholarship holder Age at enrollment \
4134 127.3 0 0 0 1 0 1 18
International Curricular units 1st sem (credited) Curricular units 1st sem (enrolled) Curricular units 1st sem (evaluations) \
4134 0 0 6 12
Curricular units 1st sem (approved) Curricular units 1st sem (grade) Curricular units 1st sem (without evaluations) \
4134 4 11.0 0
Curricular units 2nd sem (credited) Curricular units 2nd sem (enrolled) Curricular units 2nd sem (evaluations) \
4134 0 6 9
Curricular units 2nd sem (approved) Curricular units 2nd sem (grade) Curricular units 2nd sem (without evaluations) Unemployment rate \
4134 6 11.5 0 9.4
Inflation rate GDP TargetEncoded
4134 -0.8 -3.12 2
In [24]:
# Update values 2, 3 to 0
data.loc[data['TargetEncoded'].isin([2, 3]), 'TargetEncoded'] = 0
# check and print count of all unique values
print('\nCount Breakdown of Target:\n', data['TargetEncoded'].value_counts())
print('\nPercentage Breakdown of Target:\n', data['TargetEncoded'].value_counts(normalize=True) * 100)
data.head(10)Out [24]:
Count Breakdown of Target: TargetEncoded 0 3003 1 1421 Name: count, dtype: int64 Percentage Breakdown of Target: TargetEncoded 0 67.879747 1 32.120253 Name: proportion, dtype: float64
| Marital status | Application mode | Application order | Course | Daytime/evening attendance\t | Previous qualification | Previous qualification (grade) | Nationality | Mothers qualification | Fathers qualification | Mothers occupation | Fathers occupation | Admission grade | Displaced | Educational special needs | Debtor | Tuition fees up to date | Gender | Scholarship holder | Age at enrollment | International | Curricular units 1st sem (credited) | Curricular units 1st sem (enrolled) | Curricular units 1st sem (evaluations) | Curricular units 1st sem (approved) | Curricular units 1st sem (grade) | Curricular units 1st sem (without evaluations) | Curricular units 2nd sem (credited) | Curricular units 2nd sem (enrolled) | Curricular units 2nd sem (evaluations) | Curricular units 2nd sem (approved) | Curricular units 2nd sem (grade) | Curricular units 2nd sem (without evaluations) | Unemployment rate | Inflation rate | GDP | TargetEncoded | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 17 | 5 | 171 | 1 | 1 | 122.0 | 1 | 19 | 12 | 5 | 9 | 127.3 | 1 | 0 | 0 | 1 | 1 | 0 | 20 | 0 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 10.8 | 1.4 | 1.74 | 1 |
| 1 | 1 | 15 | 1 | 9254 | 1 | 1 | 160.0 | 1 | 1 | 3 | 3 | 3 | 142.5 | 1 | 0 | 0 | 0 | 1 | 0 | 19 | 0 | 0 | 6 | 6 | 6 | 14.000000 | 0 | 0 | 6 | 6 | 6 | 13.666667 | 0 | 13.9 | -0.3 | 0.79 | 0 |
| 2 | 1 | 1 | 5 | 9070 | 1 | 1 | 122.0 | 1 | 37 | 37 | 9 | 9 | 124.8 | 1 | 0 | 0 | 0 | 1 | 0 | 19 | 0 | 0 | 6 | 0 | 0 | 0.000000 | 0 | 0 | 6 | 0 | 0 | 0.000000 | 0 | 10.8 | 1.4 | 1.74 | 1 |
| 3 | 1 | 17 | 2 | 9773 | 1 | 1 | 122.0 | 1 | 38 | 37 | 5 | 3 | 119.6 | 1 | 0 | 0 | 1 | 0 | 0 | 20 | 0 | 0 | 6 | 8 | 6 | 13.428571 | 0 | 0 | 6 | 10 | 5 | 12.400000 | 0 | 9.4 | -0.8 | -3.12 | 0 |
| 4 | 2 | 39 | 1 | 8014 | 0 | 1 | 100.0 | 1 | 37 | 38 | 9 | 9 | 141.5 | 0 | 0 | 0 | 1 | 0 | 0 | 45 | 0 | 0 | 6 | 9 | 5 | 12.333333 | 0 | 0 | 6 | 6 | 6 | 13.000000 | 0 | 13.9 | -0.3 | 0.79 | 0 |
| 5 | 2 | 39 | 1 | 9991 | 0 | 19 | 133.1 | 1 | 37 | 37 | 9 | 7 | 114.8 | 0 | 0 | 1 | 1 | 1 | 0 | 50 | 0 | 0 | 5 | 10 | 5 | 11.857143 | 0 | 0 | 5 | 17 | 5 | 11.500000 | 5 | 16.2 | 0.3 | -0.92 | 0 |
| 6 | 1 | 1 | 1 | 9500 | 1 | 1 | 142.0 | 1 | 19 | 38 | 7 | 10 | 128.4 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 7 | 9 | 7 | 13.300000 | 0 | 0 | 8 | 8 | 8 | 14.345000 | 0 | 15.5 | 2.8 | -4.06 | 0 |
| 7 | 1 | 18 | 4 | 9254 | 1 | 1 | 119.0 | 1 | 37 | 37 | 9 | 9 | 113.1 | 1 | 0 | 0 | 0 | 1 | 0 | 22 | 0 | 0 | 5 | 5 | 0 | 0.000000 | 0 | 0 | 5 | 5 | 0 | 0.000000 | 0 | 15.5 | 2.8 | -4.06 | 1 |
| 8 | 1 | 1 | 3 | 9238 | 1 | 1 | 137.0 | 62 | 1 | 1 | 9 | 9 | 129.3 | 0 | 0 | 0 | 1 | 0 | 1 | 21 | 1 | 0 | 6 | 8 | 6 | 13.875000 | 0 | 0 | 6 | 7 | 6 | 14.142857 | 0 | 16.2 | 0.3 | -0.92 | 0 |
| 9 | 1 | 1 | 1 | 9238 | 1 | 1 | 138.0 | 1 | 1 | 19 | 4 | 7 | 123.0 | 1 | 0 | 1 | 0 | 0 | 0 | 18 | 0 | 0 | 6 | 9 | 5 | 11.400000 | 0 | 0 | 6 | 14 | 2 | 13.500000 | 0 | 8.9 | 1.4 | 3.51 | 1 |
In [25]:
from matplotlib.lines import Line2D
sns.countplot(x='TargetEncoded', data=data)
plt.title('Distribution of Updated Target Classes')
# show percentage of each Target value on plot
total = len(data)
for p in plt.gca().patches:
height = p.get_height()
plt.text(p.get_x() + p.get_width() / 2.,
height + 0.1,
'{:.1f}%'.format((height / total) * 100),
ha="center")
# Add custom legend labels
plt.show()In [26]:
i = 1
for col in data.drop(columns=['TargetEncoded']):
g = sns.catplot(x = col, kind='count', col = 'TargetEncoded', data=data, sharey=True)
g.figure.suptitle(i)
g.set_xticklabels()
i += 1In [27]:
i = 1
for col in data.drop(columns=['TargetEncoded']):
# Calculating the percentage of dropouts by col
result = data.groupby(col)['TargetEncoded'].value_counts(normalize=True).mul(100).unstack()
result = result.fillna(0)
plotit = result[result[1] > 65]
# print(plotit.sort_values(by=1, ascending=False))
if plotit.empty == False:
# Plotting
plotit.plot(kind='bar', figsize=(5, 3))
plt.title('Percentage of Target 0 and Target 1 by ' + column_name)
plt.xlabel(col)
plt.ylabel('Percentage')
plt.xticks(rotation=0)
plt.grid(True)
plt.legend(title='Target')
# Show the plot
plt.show()
In [28]:
from sklearn.utils import resample
# Separate majority and minority classes
majority_class = data[data['TargetEncoded'] == 0]
minority_class = data[data['TargetEncoded'] == 1]In [29]:
display(majority_class.head())
print('Shape of Majority Class:', majority_class.shape)
print('Shape of Minority Class:', minority_class.shape)| Marital status | Application mode | Application order | Course | Daytime/evening attendance\t | Previous qualification | Previous qualification (grade) | Nationality | Mothers qualification | Fathers qualification | Mothers occupation | Fathers occupation | Admission grade | Displaced | Educational special needs | Debtor | Tuition fees up to date | Gender | Scholarship holder | Age at enrollment | International | Curricular units 1st sem (credited) | Curricular units 1st sem (enrolled) | Curricular units 1st sem (evaluations) | Curricular units 1st sem (approved) | Curricular units 1st sem (grade) | Curricular units 1st sem (without evaluations) | Curricular units 2nd sem (credited) | Curricular units 2nd sem (enrolled) | Curricular units 2nd sem (evaluations) | Curricular units 2nd sem (approved) | Curricular units 2nd sem (grade) | Curricular units 2nd sem (without evaluations) | Unemployment rate | Inflation rate | GDP | TargetEncoded | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 15 | 1 | 9254 | 1 | 1 | 160.0 | 1 | 1 | 3 | 3 | 3 | 142.5 | 1 | 0 | 0 | 0 | 1 | 0 | 19 | 0 | 0 | 6 | 6 | 6 | 14.000000 | 0 | 0 | 6 | 6 | 6 | 13.666667 | 0 | 13.9 | -0.3 | 0.79 | 0 |
| 3 | 1 | 17 | 2 | 9773 | 1 | 1 | 122.0 | 1 | 38 | 37 | 5 | 3 | 119.6 | 1 | 0 | 0 | 1 | 0 | 0 | 20 | 0 | 0 | 6 | 8 | 6 | 13.428571 | 0 | 0 | 6 | 10 | 5 | 12.400000 | 0 | 9.4 | -0.8 | -3.12 | 0 |
| 4 | 2 | 39 | 1 | 8014 | 0 | 1 | 100.0 | 1 | 37 | 38 | 9 | 9 | 141.5 | 0 | 0 | 0 | 1 | 0 | 0 | 45 | 0 | 0 | 6 | 9 | 5 | 12.333333 | 0 | 0 | 6 | 6 | 6 | 13.000000 | 0 | 13.9 | -0.3 | 0.79 | 0 |
| 5 | 2 | 39 | 1 | 9991 | 0 | 19 | 133.1 | 1 | 37 | 37 | 9 | 7 | 114.8 | 0 | 0 | 1 | 1 | 1 | 0 | 50 | 0 | 0 | 5 | 10 | 5 | 11.857143 | 0 | 0 | 5 | 17 | 5 | 11.500000 | 5 | 16.2 | 0.3 | -0.92 | 0 |
| 6 | 1 | 1 | 1 | 9500 | 1 | 1 | 142.0 | 1 | 19 | 38 | 7 | 10 | 128.4 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 7 | 9 | 7 | 13.300000 | 0 | 0 | 8 | 8 | 8 | 14.345000 | 0 | 15.5 | 2.8 | -4.06 | 0 |
Shape of Majority Class: (3003, 37) Shape of Minority Class: (1421, 37)
In [30]:
# Upsample minority class
minority_upsampled = resample(minority_class,
replace=True, # Sample with replacement
n_samples=len(majority_class), # Match number of majority class
random_state=42) # Reproducible results
display(minority_upsampled.head())
print('Minority Upsampled:', minority_upsampled.shape)| Marital status | Application mode | Application order | Course | Daytime/evening attendance\t | Previous qualification | Previous qualification (grade) | Nationality | Mothers qualification | Fathers qualification | Mothers occupation | Fathers occupation | Admission grade | Displaced | Educational special needs | Debtor | Tuition fees up to date | Gender | Scholarship holder | Age at enrollment | International | Curricular units 1st sem (credited) | Curricular units 1st sem (enrolled) | Curricular units 1st sem (evaluations) | Curricular units 1st sem (approved) | Curricular units 1st sem (grade) | Curricular units 1st sem (without evaluations) | Curricular units 2nd sem (credited) | Curricular units 2nd sem (enrolled) | Curricular units 2nd sem (evaluations) | Curricular units 2nd sem (approved) | Curricular units 2nd sem (grade) | Curricular units 2nd sem (without evaluations) | Unemployment rate | Inflation rate | GDP | TargetEncoded | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 3564 | 1 | 44 | 1 | 9119 | 1 | 39 | 130.0 | 1 | 19 | 3 | 3 | 4 | 130.0 | 0 | 0 | 1 | 0 | 1 | 0 | 23 | 0 | 0 | 5 | 5 | 0 | 0.000000 | 0 | 0 | 5 | 5 | 0 | 0.00000 | 0 | 15.5 | 2.8 | -4.06 | 1 |
| 2760 | 1 | 39 | 1 | 9500 | 1 | 4 | 150.0 | 1 | 3 | 38 | 3 | 7 | 127.0 | 1 | 0 | 0 | 1 | 1 | 0 | 26 | 0 | 2 | 7 | 8 | 6 | 13.046667 | 0 | 2 | 8 | 10 | 7 | 13.30625 | 0 | 11.1 | 0.6 | 2.02 | 1 |
| 4015 | 4 | 39 | 1 | 9991 | 0 | 1 | 133.1 | 1 | 37 | 37 | 5 | 5 | 138.7 | 0 | 0 | 0 | 1 | 0 | 0 | 31 | 0 | 0 | 5 | 7 | 3 | 10.333333 | 0 | 0 | 5 | 6 | 3 | 12.00000 | 0 | 10.8 | 1.4 | 1.74 | 1 |
| 3577 | 1 | 42 | 1 | 9500 | 1 | 1 | 100.0 | 1 | 19 | 19 | 9 | 9 | 100.0 | 0 | 0 | 1 | 1 | 1 | 0 | 24 | 0 | 0 | 8 | 16 | 2 | 10.250000 | 1 | 0 | 8 | 16 | 2 | 10.25000 | 1 | 12.7 | 3.7 | -1.70 | 1 |
| 3501 | 1 | 39 | 1 | 9070 | 1 | 1 | 140.0 | 1 | 37 | 37 | 9 | 10 | 118.0 | 0 | 0 | 1 | 1 | 1 | 1 | 45 | 0 | 0 | 6 | 6 | 6 | 13.500000 | 0 | 0 | 6 | 6 | 0 | 0.00000 | 0 | 16.2 | 0.3 | -0.92 | 1 |
Minority Upsampled: (3003, 37)
In [31]:
data['TargetEncoded'].value_counts()Out [31]:
TargetEncoded 0 3003 1 1421 Name: count, dtype: int64
In [32]:
# Combine majority class with upsampled minority class
balanced_data = pd.concat([majority_class, minority_upsampled])
# Now 'balanced_data' contains an upsampled distribution of the minority classIn [33]:
display(balanced_data.head())
print('Shape of unbalanced_data:', balanced_data.shape)
print('Distribution of unbalanced_data:', balanced_data['TargetEncoded'].value_counts())| Marital status | Application mode | Application order | Course | Daytime/evening attendance\t | Previous qualification | Previous qualification (grade) | Nationality | Mothers qualification | Fathers qualification | Mothers occupation | Fathers occupation | Admission grade | Displaced | Educational special needs | Debtor | Tuition fees up to date | Gender | Scholarship holder | Age at enrollment | International | Curricular units 1st sem (credited) | Curricular units 1st sem (enrolled) | Curricular units 1st sem (evaluations) | Curricular units 1st sem (approved) | Curricular units 1st sem (grade) | Curricular units 1st sem (without evaluations) | Curricular units 2nd sem (credited) | Curricular units 2nd sem (enrolled) | Curricular units 2nd sem (evaluations) | Curricular units 2nd sem (approved) | Curricular units 2nd sem (grade) | Curricular units 2nd sem (without evaluations) | Unemployment rate | Inflation rate | GDP | TargetEncoded | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 15 | 1 | 9254 | 1 | 1 | 160.0 | 1 | 1 | 3 | 3 | 3 | 142.5 | 1 | 0 | 0 | 0 | 1 | 0 | 19 | 0 | 0 | 6 | 6 | 6 | 14.000000 | 0 | 0 | 6 | 6 | 6 | 13.666667 | 0 | 13.9 | -0.3 | 0.79 | 0 |
| 3 | 1 | 17 | 2 | 9773 | 1 | 1 | 122.0 | 1 | 38 | 37 | 5 | 3 | 119.6 | 1 | 0 | 0 | 1 | 0 | 0 | 20 | 0 | 0 | 6 | 8 | 6 | 13.428571 | 0 | 0 | 6 | 10 | 5 | 12.400000 | 0 | 9.4 | -0.8 | -3.12 | 0 |
| 4 | 2 | 39 | 1 | 8014 | 0 | 1 | 100.0 | 1 | 37 | 38 | 9 | 9 | 141.5 | 0 | 0 | 0 | 1 | 0 | 0 | 45 | 0 | 0 | 6 | 9 | 5 | 12.333333 | 0 | 0 | 6 | 6 | 6 | 13.000000 | 0 | 13.9 | -0.3 | 0.79 | 0 |
| 5 | 2 | 39 | 1 | 9991 | 0 | 19 | 133.1 | 1 | 37 | 37 | 9 | 7 | 114.8 | 0 | 0 | 1 | 1 | 1 | 0 | 50 | 0 | 0 | 5 | 10 | 5 | 11.857143 | 0 | 0 | 5 | 17 | 5 | 11.500000 | 5 | 16.2 | 0.3 | -0.92 | 0 |
| 6 | 1 | 1 | 1 | 9500 | 1 | 1 | 142.0 | 1 | 19 | 38 | 7 | 10 | 128.4 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 7 | 9 | 7 | 13.300000 | 0 | 0 | 8 | 8 | 8 | 14.345000 | 0 | 15.5 | 2.8 | -4.06 | 0 |
Shape of unbalanced_data: (6006, 37) Distribution of unbalanced_data: TargetEncoded 0 3003 1 3003 Name: count, dtype: int64
In [34]:
sns.countplot(x='TargetEncoded', data=balanced_data)
plt.title('Distribution of Updated Target Classes')
# show percentage of each Target value on plot
total = len(data)
for p in plt.gca().patches:
height = p.get_height()
plt.text(p.get_x() + p.get_width() / 2.,
height + 0.1,
'{:.1f}%'.format((height / total) * 100),
ha="center")
# Add custom legend labels
plt.show()In [35]:
data = balanced_dataIn [36]:
sns.countplot(x='TargetEncoded', data=data)
plt.title('Distribution of Updated Target Classes')
# show percentage of each Target value on plot
total = len(data)
for p in plt.gca().patches:
height = p.get_height()
plt.text(p.get_x() + p.get_width() / 2.,
height + 0.1,
'{:.1f}%'.format((height / total) * 100),
ha="center")
# Add custom legend labels
plt.show()In [37]:
qualification_bin_mapping = {
1:7,
2:9,
3:9,
4:9,
5:9,
6:9,
9:6,
10:6,
11:5,
12:7,
13:7,
14:7,
15:6,
18:9,
19:6,
20:7,
22:8,
25:6,
26:5,
27:6,
29:6,
30:4,
31:9,
33:8,
34:1,
35:2,
36:2,
37:3,
38:5,
39:8,
40:7,
41:9,
42:9,
43:9,
44:9
}
display(qualification_bin_mapping){1: 7,
2: 9,
3: 9,
4: 9,
5: 9,
6: 9,
9: 6,
10: 6,
11: 5,
12: 7,
13: 7,
14: 7,
15: 6,
18: 9,
19: 6,
20: 7,
22: 8,
25: 6,
26: 5,
27: 6,
29: 6,
30: 4,
31: 9,
33: 8,
34: 1,
35: 2,
36: 2,
37: 3,
38: 5,
39: 8,
40: 7,
41: 9,
42: 9,
43: 9,
44: 9}In [38]:
# Apply the mapping to the appropriate columns in the DataFrame
data['Previous qualification Bin'] = data['Previous qualification'].map(qualification_bin_mapping)
data['Mothers qualification Bin'] = data['Mothers qualification'].map(qualification_bin_mapping)
data['Fathers qualification Bin'] = data['Fathers qualification'].map(qualification_bin_mapping)In [39]:
data.info()<class 'pandas.core.frame.DataFrame'> Index: 6006 entries, 1 to 2393 Data columns (total 40 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Marital status 6006 non-null int64 1 Application mode 6006 non-null int64 2 Application order 6006 non-null int64 3 Course 6006 non-null int64 4 Daytime/evening attendance 6006 non-null int64 5 Previous qualification 6006 non-null int64 6 Previous qualification (grade) 6006 non-null float64 7 Nationality 6006 non-null int64 8 Mothers qualification 6006 non-null int64 9 Fathers qualification 6006 non-null int64 10 Mothers occupation 6006 non-null int64 11 Fathers occupation 6006 non-null int64 12 Admission grade 6006 non-null float64 13 Displaced 6006 non-null int64 14 Educational special needs 6006 non-null int64 15 Debtor 6006 non-null int64 16 Tuition fees up to date 6006 non-null int64 17 Gender 6006 non-null int64 18 Scholarship holder 6006 non-null int64 19 Age at enrollment 6006 non-null int64 20 International 6006 non-null int64 21 Curricular units 1st sem (credited) 6006 non-null int64 22 Curricular units 1st sem (enrolled) 6006 non-null int64 23 Curricular units 1st sem (evaluations) 6006 non-null int64 24 Curricular units 1st sem (approved) 6006 non-null int64 25 Curricular units 1st sem (grade) 6006 non-null float64 26 Curricular units 1st sem (without evaluations) 6006 non-null int64 27 Curricular units 2nd sem (credited) 6006 non-null int64 28 Curricular units 2nd sem (enrolled) 6006 non-null int64 29 Curricular units 2nd sem (evaluations) 6006 non-null int64 30 Curricular units 2nd sem (approved) 6006 non-null int64 31 Curricular units 2nd sem (grade) 6006 non-null float64 32 Curricular units 2nd sem (without evaluations) 6006 non-null int64 33 Unemployment rate 6006 non-null float64 34 Inflation rate 6006 non-null float64 35 GDP 6006 non-null float64 36 TargetEncoded 6006 non-null int64 37 Previous qualification Bin 6006 non-null int64 38 Mothers qualification Bin 6006 non-null int64 39 Fathers qualification Bin 6006 non-null int64 dtypes: float64(7), int64(33) memory usage: 1.9 MB
In [40]:
data.drop('Previous qualification', axis=1, inplace=True)
data.drop('Mothers qualification', axis=1, inplace=True)
data.drop('Fathers qualification', axis=1, inplace=True)In [41]:
data.head(15)Out [41]:
| Marital status | Application mode | Application order | Course | Daytime/evening attendance\t | Previous qualification (grade) | Nationality | Mothers occupation | Fathers occupation | Admission grade | Displaced | Educational special needs | Debtor | Tuition fees up to date | Gender | Scholarship holder | Age at enrollment | International | Curricular units 1st sem (credited) | Curricular units 1st sem (enrolled) | Curricular units 1st sem (evaluations) | Curricular units 1st sem (approved) | Curricular units 1st sem (grade) | Curricular units 1st sem (without evaluations) | Curricular units 2nd sem (credited) | Curricular units 2nd sem (enrolled) | Curricular units 2nd sem (evaluations) | Curricular units 2nd sem (approved) | Curricular units 2nd sem (grade) | Curricular units 2nd sem (without evaluations) | Unemployment rate | Inflation rate | GDP | TargetEncoded | Previous qualification Bin | Mothers qualification Bin | Fathers qualification Bin | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 15 | 1 | 9254 | 1 | 160.0 | 1 | 3 | 3 | 142.5 | 1 | 0 | 0 | 0 | 1 | 0 | 19 | 0 | 0 | 6 | 6 | 6 | 14.000000 | 0 | 0 | 6 | 6 | 6 | 13.666667 | 0 | 13.9 | -0.3 | 0.79 | 0 | 7 | 7 | 9 |
| 3 | 1 | 17 | 2 | 9773 | 1 | 122.0 | 1 | 5 | 3 | 119.6 | 1 | 0 | 0 | 1 | 0 | 0 | 20 | 0 | 0 | 6 | 8 | 6 | 13.428571 | 0 | 0 | 6 | 10 | 5 | 12.400000 | 0 | 9.4 | -0.8 | -3.12 | 0 | 7 | 5 | 3 |
| 4 | 2 | 39 | 1 | 8014 | 0 | 100.0 | 1 | 9 | 9 | 141.5 | 0 | 0 | 0 | 1 | 0 | 0 | 45 | 0 | 0 | 6 | 9 | 5 | 12.333333 | 0 | 0 | 6 | 6 | 6 | 13.000000 | 0 | 13.9 | -0.3 | 0.79 | 0 | 7 | 3 | 5 |
| 5 | 2 | 39 | 1 | 9991 | 0 | 133.1 | 1 | 9 | 7 | 114.8 | 0 | 0 | 1 | 1 | 1 | 0 | 50 | 0 | 0 | 5 | 10 | 5 | 11.857143 | 0 | 0 | 5 | 17 | 5 | 11.500000 | 5 | 16.2 | 0.3 | -0.92 | 0 | 6 | 3 | 3 |
| 6 | 1 | 1 | 1 | 9500 | 1 | 142.0 | 1 | 7 | 10 | 128.4 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 7 | 9 | 7 | 13.300000 | 0 | 0 | 8 | 8 | 8 | 14.345000 | 0 | 15.5 | 2.8 | -4.06 | 0 | 7 | 6 | 5 |
| 8 | 1 | 1 | 3 | 9238 | 1 | 137.0 | 62 | 9 | 9 | 129.3 | 0 | 0 | 0 | 1 | 0 | 1 | 21 | 1 | 0 | 6 | 8 | 6 | 13.875000 | 0 | 0 | 6 | 7 | 6 | 14.142857 | 0 | 16.2 | 0.3 | -0.92 | 0 | 7 | 7 | 7 |
| 10 | 1 | 1 | 1 | 9670 | 1 | 139.0 | 1 | 5 | 7 | 130.6 | 1 | 0 | 0 | 1 | 0 | 0 | 18 | 0 | 0 | 6 | 6 | 6 | 12.333333 | 0 | 0 | 6 | 7 | 5 | 14.200000 | 0 | 13.9 | -0.3 | 0.79 | 0 | 7 | 5 | 6 |
| 11 | 1 | 1 | 1 | 9500 | 1 | 136.0 | 1 | 9 | 9 | 119.3 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 8 | 8 | 7 | 13.214286 | 0 | 0 | 8 | 8 | 7 | 13.214286 | 0 | 12.7 | 3.7 | -1.70 | 0 | 7 | 6 | 5 |
| 13 | 1 | 53 | 1 | 9254 | 1 | 110.0 | 1 | 4 | 7 | 111.8 | 1 | 0 | 0 | 1 | 0 | 1 | 21 | 0 | 0 | 6 | 7 | 6 | 10.571429 | 0 | 0 | 6 | 8 | 5 | 11.000000 | 0 | 8.9 | 1.4 | 3.51 | 0 | 9 | 7 | 7 |
| 14 | 1 | 1 | 1 | 9085 | 1 | 149.0 | 1 | 5 | 5 | 137.1 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 5 | 7 | 4 | 13.250000 | 0 | 0 | 5 | 5 | 5 | 12.000000 | 0 | 10.8 | 1.4 | 1.74 | 0 | 7 | 5 | 3 |
| 16 | 1 | 18 | 1 | 9238 | 1 | 137.0 | 1 | 5 | 8 | 137.4 | 1 | 0 | 0 | 1 | 0 | 0 | 18 | 0 | 0 | 6 | 10 | 1 | 12.000000 | 0 | 0 | 6 | 14 | 2 | 11.000000 | 0 | 10.8 | 1.4 | 1.74 | 0 | 7 | 6 | 5 |
| 17 | 1 | 17 | 2 | 9500 | 1 | 135.0 | 1 | 5 | 4 | 127.3 | 1 | 0 | 0 | 1 | 0 | 0 | 18 | 0 | 0 | 7 | 8 | 7 | 13.306250 | 0 | 0 | 8 | 8 | 8 | 14.545000 | 0 | 15.5 | 2.8 | -4.06 | 0 | 7 | 6 | 7 |
| 18 | 1 | 1 | 1 | 9130 | 1 | 137.0 | 1 | 3 | 5 | 136.3 | 1 | 0 | 0 | 1 | 0 | 0 | 20 | 0 | 0 | 5 | 8 | 4 | 12.500000 | 1 | 0 | 5 | 8 | 4 | 12.250000 | 2 | 10.8 | 1.4 | 1.74 | 0 | 7 | 9 | 6 |
| 19 | 1 | 1 | 1 | 9853 | 1 | 140.0 | 1 | 7 | 7 | 124.6 | 1 | 0 | 0 | 1 | 0 | 0 | 18 | 0 | 0 | 7 | 7 | 6 | 11.666667 | 0 | 0 | 7 | 8 | 6 | 13.500000 | 0 | 16.2 | 0.3 | -0.92 | 0 | 7 | 6 | 6 |
| 20 | 1 | 1 | 3 | 171 | 1 | 122.0 | 1 | 9 | 8 | 120.3 | 0 | 0 | 0 | 1 | 0 | 1 | 21 | 0 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 11.1 | 0.6 | 2.02 | 0 | 7 | 7 | 7 |
In [42]:
data['Marital status'].value_counts(normalize=True, ascending=False).mul(100).round(2)Out [42]:
Marital status 1 87.16 2 9.49 4 2.35 5 0.72 6 0.22 3 0.07 Name: proportion, dtype: float64
In [43]:
marital_bin_mapping = {
1:1,
2:2,
3:7,
4:4,
5:7,
6:7
}
display(marital_bin_mapping){1: 1, 2: 2, 3: 7, 4: 4, 5: 7, 6: 7}In [44]:
# Apply the mapping to the appropriate column
data['Marital Status Bin'] = data['Marital status'].map(marital_bin_mapping)In [45]:
print(data['Marital Status Bin'].value_counts(normalize=True, ascending=False).mul(100).round(2))
display(data.head())Marital Status Bin 1 87.16 2 9.49 4 2.35 7 1.00 Name: proportion, dtype: float64
| Marital status | Application mode | Application order | Course | Daytime/evening attendance\t | Previous qualification (grade) | Nationality | Mothers occupation | Fathers occupation | Admission grade | Displaced | Educational special needs | Debtor | Tuition fees up to date | Gender | Scholarship holder | Age at enrollment | International | Curricular units 1st sem (credited) | Curricular units 1st sem (enrolled) | Curricular units 1st sem (evaluations) | Curricular units 1st sem (approved) | Curricular units 1st sem (grade) | Curricular units 1st sem (without evaluations) | Curricular units 2nd sem (credited) | Curricular units 2nd sem (enrolled) | Curricular units 2nd sem (evaluations) | Curricular units 2nd sem (approved) | Curricular units 2nd sem (grade) | Curricular units 2nd sem (without evaluations) | Unemployment rate | Inflation rate | GDP | TargetEncoded | Previous qualification Bin | Mothers qualification Bin | Fathers qualification Bin | Marital Status Bin | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 15 | 1 | 9254 | 1 | 160.0 | 1 | 3 | 3 | 142.5 | 1 | 0 | 0 | 0 | 1 | 0 | 19 | 0 | 0 | 6 | 6 | 6 | 14.000000 | 0 | 0 | 6 | 6 | 6 | 13.666667 | 0 | 13.9 | -0.3 | 0.79 | 0 | 7 | 7 | 9 | 1 |
| 3 | 1 | 17 | 2 | 9773 | 1 | 122.0 | 1 | 5 | 3 | 119.6 | 1 | 0 | 0 | 1 | 0 | 0 | 20 | 0 | 0 | 6 | 8 | 6 | 13.428571 | 0 | 0 | 6 | 10 | 5 | 12.400000 | 0 | 9.4 | -0.8 | -3.12 | 0 | 7 | 5 | 3 | 1 |
| 4 | 2 | 39 | 1 | 8014 | 0 | 100.0 | 1 | 9 | 9 | 141.5 | 0 | 0 | 0 | 1 | 0 | 0 | 45 | 0 | 0 | 6 | 9 | 5 | 12.333333 | 0 | 0 | 6 | 6 | 6 | 13.000000 | 0 | 13.9 | -0.3 | 0.79 | 0 | 7 | 3 | 5 | 2 |
| 5 | 2 | 39 | 1 | 9991 | 0 | 133.1 | 1 | 9 | 7 | 114.8 | 0 | 0 | 1 | 1 | 1 | 0 | 50 | 0 | 0 | 5 | 10 | 5 | 11.857143 | 0 | 0 | 5 | 17 | 5 | 11.500000 | 5 | 16.2 | 0.3 | -0.92 | 0 | 6 | 3 | 3 | 2 |
| 6 | 1 | 1 | 1 | 9500 | 1 | 142.0 | 1 | 7 | 10 | 128.4 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 7 | 9 | 7 | 13.300000 | 0 | 0 | 8 | 8 | 8 | 14.345000 | 0 | 15.5 | 2.8 | -4.06 | 0 | 7 | 6 | 5 | 1 |
In [46]:
data.drop('Marital status', axis=1, inplace=True)In [47]:
display(data.head(15))| Application mode | Application order | Course | Daytime/evening attendance\t | Previous qualification (grade) | Nationality | Mothers occupation | Fathers occupation | Admission grade | Displaced | Educational special needs | Debtor | Tuition fees up to date | Gender | Scholarship holder | Age at enrollment | International | Curricular units 1st sem (credited) | Curricular units 1st sem (enrolled) | Curricular units 1st sem (evaluations) | Curricular units 1st sem (approved) | Curricular units 1st sem (grade) | Curricular units 1st sem (without evaluations) | Curricular units 2nd sem (credited) | Curricular units 2nd sem (enrolled) | Curricular units 2nd sem (evaluations) | Curricular units 2nd sem (approved) | Curricular units 2nd sem (grade) | Curricular units 2nd sem (without evaluations) | Unemployment rate | Inflation rate | GDP | TargetEncoded | Previous qualification Bin | Mothers qualification Bin | Fathers qualification Bin | Marital Status Bin | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 15 | 1 | 9254 | 1 | 160.0 | 1 | 3 | 3 | 142.5 | 1 | 0 | 0 | 0 | 1 | 0 | 19 | 0 | 0 | 6 | 6 | 6 | 14.000000 | 0 | 0 | 6 | 6 | 6 | 13.666667 | 0 | 13.9 | -0.3 | 0.79 | 0 | 7 | 7 | 9 | 1 |
| 3 | 17 | 2 | 9773 | 1 | 122.0 | 1 | 5 | 3 | 119.6 | 1 | 0 | 0 | 1 | 0 | 0 | 20 | 0 | 0 | 6 | 8 | 6 | 13.428571 | 0 | 0 | 6 | 10 | 5 | 12.400000 | 0 | 9.4 | -0.8 | -3.12 | 0 | 7 | 5 | 3 | 1 |
| 4 | 39 | 1 | 8014 | 0 | 100.0 | 1 | 9 | 9 | 141.5 | 0 | 0 | 0 | 1 | 0 | 0 | 45 | 0 | 0 | 6 | 9 | 5 | 12.333333 | 0 | 0 | 6 | 6 | 6 | 13.000000 | 0 | 13.9 | -0.3 | 0.79 | 0 | 7 | 3 | 5 | 2 |
| 5 | 39 | 1 | 9991 | 0 | 133.1 | 1 | 9 | 7 | 114.8 | 0 | 0 | 1 | 1 | 1 | 0 | 50 | 0 | 0 | 5 | 10 | 5 | 11.857143 | 0 | 0 | 5 | 17 | 5 | 11.500000 | 5 | 16.2 | 0.3 | -0.92 | 0 | 6 | 3 | 3 | 2 |
| 6 | 1 | 1 | 9500 | 1 | 142.0 | 1 | 7 | 10 | 128.4 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 7 | 9 | 7 | 13.300000 | 0 | 0 | 8 | 8 | 8 | 14.345000 | 0 | 15.5 | 2.8 | -4.06 | 0 | 7 | 6 | 5 | 1 |
| 8 | 1 | 3 | 9238 | 1 | 137.0 | 62 | 9 | 9 | 129.3 | 0 | 0 | 0 | 1 | 0 | 1 | 21 | 1 | 0 | 6 | 8 | 6 | 13.875000 | 0 | 0 | 6 | 7 | 6 | 14.142857 | 0 | 16.2 | 0.3 | -0.92 | 0 | 7 | 7 | 7 | 1 |
| 10 | 1 | 1 | 9670 | 1 | 139.0 | 1 | 5 | 7 | 130.6 | 1 | 0 | 0 | 1 | 0 | 0 | 18 | 0 | 0 | 6 | 6 | 6 | 12.333333 | 0 | 0 | 6 | 7 | 5 | 14.200000 | 0 | 13.9 | -0.3 | 0.79 | 0 | 7 | 5 | 6 | 1 |
| 11 | 1 | 1 | 9500 | 1 | 136.0 | 1 | 9 | 9 | 119.3 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 8 | 8 | 7 | 13.214286 | 0 | 0 | 8 | 8 | 7 | 13.214286 | 0 | 12.7 | 3.7 | -1.70 | 0 | 7 | 6 | 5 | 1 |
| 13 | 53 | 1 | 9254 | 1 | 110.0 | 1 | 4 | 7 | 111.8 | 1 | 0 | 0 | 1 | 0 | 1 | 21 | 0 | 0 | 6 | 7 | 6 | 10.571429 | 0 | 0 | 6 | 8 | 5 | 11.000000 | 0 | 8.9 | 1.4 | 3.51 | 0 | 9 | 7 | 7 | 1 |
| 14 | 1 | 1 | 9085 | 1 | 149.0 | 1 | 5 | 5 | 137.1 | 1 | 0 | 0 | 1 | 0 | 1 | 18 | 0 | 0 | 5 | 7 | 4 | 13.250000 | 0 | 0 | 5 | 5 | 5 | 12.000000 | 0 | 10.8 | 1.4 | 1.74 | 0 | 7 | 5 | 3 | 1 |
| 16 | 18 | 1 | 9238 | 1 | 137.0 | 1 | 5 | 8 | 137.4 | 1 | 0 | 0 | 1 | 0 | 0 | 18 | 0 | 0 | 6 | 10 | 1 | 12.000000 | 0 | 0 | 6 | 14 | 2 | 11.000000 | 0 | 10.8 | 1.4 | 1.74 | 0 | 7 | 6 | 5 | 1 |
| 17 | 17 | 2 | 9500 | 1 | 135.0 | 1 | 5 | 4 | 127.3 | 1 | 0 | 0 | 1 | 0 | 0 | 18 | 0 | 0 | 7 | 8 | 7 | 13.306250 | 0 | 0 | 8 | 8 | 8 | 14.545000 | 0 | 15.5 | 2.8 | -4.06 | 0 | 7 | 6 | 7 | 1 |
| 18 | 1 | 1 | 9130 | 1 | 137.0 | 1 | 3 | 5 | 136.3 | 1 | 0 | 0 | 1 | 0 | 0 | 20 | 0 | 0 | 5 | 8 | 4 | 12.500000 | 1 | 0 | 5 | 8 | 4 | 12.250000 | 2 | 10.8 | 1.4 | 1.74 | 0 | 7 | 9 | 6 | 1 |
| 19 | 1 | 1 | 9853 | 1 | 140.0 | 1 | 7 | 7 | 124.6 | 1 | 0 | 0 | 1 | 0 | 0 | 18 | 0 | 0 | 7 | 7 | 6 | 11.666667 | 0 | 0 | 7 | 8 | 6 | 13.500000 | 0 | 16.2 | 0.3 | -0.92 | 0 | 7 | 6 | 6 | 1 |
| 20 | 1 | 3 | 171 | 1 | 122.0 | 1 | 9 | 8 | 120.3 | 0 | 0 | 0 | 1 | 0 | 1 | 21 | 0 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0.000000 | 0 | 11.1 | 0.6 | 2.02 | 0 | 7 | 7 | 7 | 1 |
Warning:
Output truncated. This notebook contains too many cells to display efficiently.