Dataset Exploration and Analysis #2
Multivariate Analysis final project 。

The cover image is generated by ChatGPT and it’s about dataset exploration and analysis. The prompt used was: “A digital illustration in a 16:9 aspect ratio, showing a young male data analyst working in a modern high-tech office, analyzing a large dataset on a widescreen monitor. The screen displays various data visualizations: bar charts, scatter plots, histograms, and data tables. The interface is sleek and futuristic, with turquoise and blue tones, glowing UI elements, and a clear emphasis on Dataset Exploration and Analysis” 。
Introduction
The dataset used in this project is sourced from Kaggle and was provided by Adil Shamim. It originates from the UCI Machine Learning Repository and was initially introduced by P. Cortez and A. Silva in their study titled Using Data Mining to Predict Secondary School Student Performance. This dataset is part of research presented at the 5th FUBUTEC 2008 conference held in Porto, Portugal. The variables G1, G2, G3, and absences were directly obtained from the school records, while the other variables were collected through questionnaires.
In this analysis, Python will be used as the programming language, and the main objective is to identify the variables that influence the final semester grade G3.
Dataset
Variable Description
The dataset contains 399 records and 33 variables. The variables are described as follows:
| Variable | Description | Type | Range |
|---|---|---|---|
school | Student’s school | Binary | GP - Gabriel Pereira MS - Mousinho da Silveira |
sex | Student’s gender | Binary | F - Female M - Male |
age | Student’s age | Numeric | 15 to 22 |
address | Home address type | Binary | U - Urban R - Rural |
famsize | Family size | Binary | LE3 - Less than or equal to 3 GT3 - Greater than 3 |
Pstatus | Parent’s cohabitation status | Binary | T - Living together A - Apart |
Medu | Mother’s education level | Numeric | 0 - None 1 - Primary (4th grade) 2 - 5th to 9th grade 3 - Secondary 4 - Higher education |
Fedu | Father’s education level | Numeric | 0 - None 1 - Primary (4th grade) 2 - 5th to 9th grade 3 - Secondary 4 - Higher education |
Mjob | Mother’s job | Nominal | teacher, health, services, at_home, other |
Fjob | Father’s job | Nominal | teacher, health, services, at_home, other |
reason | Reason for choosing the school | Nominal | home, reputation, course, other |
guardian | Student’s guardian | Nominal | mother, father, other |
traveltime | Travel time to school | Numeric | 1 - <15 min 2 - 15–30 min 3 - 30–60 min 4 - >60 min |
studytime | Weekly study time | Numeric | 1 - <2 hours 2 - 2 to 5 hours 3 - 5 to 10 hours 4 - >10 hours |
failures | Number of past class failures | Numeric | 0 to 3; 4 means more than 3 failures |
schoolsup | Extra educational support | Binary | yes / no |
famsup | Family educational support | Binary | yes / no |
paid | Extra paid classes | Binary | yes / no |
activities | Extracurricular activities | Binary | yes / no |
nursery | Attended nursery school | Binary | yes / no |
higher | Wants to take higher education | Binary | yes / no |
internet | Internet access at home | Binary | yes / no |
romantic | In a romantic relationship | Binary | yes / no |
famrel | Quality of family relationships | Numeric | From 1 (very bad) to 5 (very good) |
freetime | Free time after school | Numeric | From 1 (very low) to 5 (very high) |
goout | Going out with friends frequency | Numeric | From 1 (very low) to 5 (very high) |
Dalc | Workday alcohol consumption | Numeric | From 1 (very low) to 5 (very high) |
Walc | Weekend alcohol consumption | Numeric | From 1 (very low) to 5 (very high) |
health | Current health status | Numeric | From 1 (very bad) to 5 (very good) |
absences | Number of school absences | Numeric | 0 to 93 |
G1 | First period grade | Numeric | 0 to 20 |
G2 | Second period grade | Numeric | 0 to 20 |
G3 | Final grade | Numeric | 0 to 20 |
Dataset
The following analysis is conducted using Python as the programming language. The original contents of the dataset are as follows:
| index | school | sex | age | address | famsize | Pstatus | Medu | Fedu | Mjob | Fjob | famrel | freetime | goout | Dalc | Walc | health | absences | G1 | G2 | G3 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | GP | F | 18 | U | GT3 | A | 4 | 4 | at_home | teacher | 4 | 3 | 4 | 1 | 1 | 3 | 6 | 5 | 6 | 6 |
| 1 | GP | F | 17 | U | GT3 | T | 1 | 1 | at_home | other | 5 | 3 | 3 | 1 | 1 | 3 | 4 | 5 | 5 | 6 |
| 2 | GP | F | 15 | U | LE3 | T | 1 | 1 | at_home | other | 4 | 3 | 2 | 2 | 3 | 3 | 10 | 7 | 8 | 10 |
| 3 | GP | F | 15 | U | GT3 | T | 4 | 2 | health | services | 3 | 2 | 2 | 1 | 1 | 5 | 2 | 15 | 14 | 15 |
| 4 | GP | F | 16 | U | GT3 | T | 3 | 3 | other | other | 4 | 3 | 2 | 1 | 2 | 5 | 4 | 6 | 10 | 10 |
| … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | … | .. | .. | .. |
| 394 | MS | M | 19 | U | LE3 | T | 1 | 1 | other | at_home | 3 | 2 | 3 | 3 | 3 | 5 | 5 | 8 | 9 | 9 |
| 395 | MS | M | 18 | U | GT3 | T | 4 | 4 | teacher | services | 5 | 3 | 2 | 1 | 2 | 4 | 0 | 8 | 7 | 7 |
| 396 | MS | M | 17 | U | GT3 | T | 4 | 4 | teacher | services | 5 | 3 | 2 | 1 | 2 | 4 | 0 | 8 | 7 | 7 |
| 397 | MS | M | 19 | U | GT3 | T | 4 | 4 | teacher | other | 5 | 3 | 2 | 1 | 2 | 4 | 0 | 8 | 7 | 7 |
| 398 | MS | M | 18 | U | GT3 | T | 4 | 4 | teacher | at_home | 5 | 3 | 2 | 1 | 2 | 4 | 0 | 8 | 7 | 7 |
Original Data Bar Charts
To better prepare for data processing, we begin by plotting bar charts for various variables to gain a basic understanding of the dataset.
From the charts, we can observe the following:
- The
agedistribution shows slight variations, with most students aged between 15 and 18. This suggests that the dataset mainly consists of high school students, especially by Taiwanese standards. - There are students aged 20, 21, and 22, which may indicate that some are repeating a grade or have special circumstances.
- The lowest category of
studytimeis around 2 hours. - The distributions of
freetime,G1,G2, andG3are approximately normal. absences,Dalc, andWalcare right-skewed.famrelandhealthshow a left-skewed distribution.- Parents’ occupations are most commonly labeled as
otherandservices. - The gender ratio is roughly balanced.
Data Cleaning
To ensure a smooth analysis process, we begin by organizing the categorical variables, assigning them as either factors or using one-hot encoding. Additionally, we group related variables and specify G3 as the target variable for prediction.
| Groups | Variables |
|---|---|
support | schoolsup, famsup, paid |
family | address, famsize, Pstatus, guardian, traveltime, famrel |
parents | Medu, Fedu, Mjob, Fjob |
performance | failures, studytime, absences |
alcohol | Dalc, Walc, health |
after_class | activities, freetime, goout |
school_choice | reason, nursery, higher |
score | G1, G2, G3 |
| |
To facilitate data processing, we first organize the categorical variables. Those with an inherent order are converted into factors with levels arranged from weakest to strongest. Variables without a natural order are handled using one-hot encoding, and we adjust the order of categories within each variable for consistency, making future visual inspection more convenient.
From the previous bar charts, we observed that the school MS had significantly fewer data entries. To avoid biased analysis results caused by data imbalance between schools, we choose to exclude students from the MS school and focus our analysis solely on the GP school.
Additionally, G1, G2, and G3 all represent summative assessments—evaluations of students’ academic performance at different stages of the semester. We will later visualize the correlation among these three grades to demonstrate their strong interrelationship.
Since our goal is to identify the factors that influence final academic performance, we save a copy of the original dataset, remove G1 and G2, and retain G3 as the target variable for prediction.
Bar Charts After Data Cleaning
The following bar charts illustrate the dataset after the cleaning process. After removing data entries from the MS school, the overall structure remains largely the same, though some differences compared to the original dataset are noticeable.
Additionally, some variables have been converted from categorical to numerical form during preprocessing, which is reflected in the charts as numeric values.
These updated bar charts now provide a clearer and more focused view of the information specific to the GP school.
Correlation Matrix and Scatter Plot
We can examine the relationships between variables by calculating the correlation matrix, which quantifies pairwise correlations. A more advanced approach is to visualize this matrix using a correlation heatmap. In the plot below, deeper red colors indicate stronger positive correlations**, while deeper blue colors indicate stronger negative correlations.
As shown in the figure, there is a very strong correlation among G1, G2, and G3. This is precisely why we decided to remove G1 and G2 from the dataset. If they were kept, analysis results would overwhelmingly reflect their influence on G3, overshadowing the impact of other variables.

Below is a 3D scatter plot of G1, G2, and G3, where we can observe a strong linear relationship among these three variables.
If you are unable to view the interactive 3D scatter plot or need to view it in full screen, please click here.
Next, we present the correlation plots within other variable groups. From the following figures, we can observe:
- Within the
parentsgroup, bothMedu,Fedu,MjobandFjobshow strong positive correlations, indicating that the parents group can be used to analyze the influence of parents on student academic performance. - Within the
alcoholgroup,DalcandWalcare highly positively correlated, but they show almost no correlation with the students’ self-assessedhealthstatus. - Within the
support group, there is a positive correlation betweenfamsupandpaid, which may suggest that family influences participation in extracurricular courses. - Within the
after_classgroup,freetimeandgooutare positively correlated, possibly indicating that students often spend their free time socializing with friends after school.

Interaction Bar Charts of Variables
Below, we select and plot some bar charts showing interactions between variables to observe their data patterns.
First, we plot the average minimum weekly study time (studytime) by age. The studytime variable is coded as follows:
1means less than 2 hours per week2means 2 to 5 hours per week3means 5 to 10 hours per week4means more than 10 hours per week
From this, we know the minimum weekly study time for each code is:
1at least 0 hours per week2at least 2 hours per week3at least 5 hours per week4at least 10 hours per week
Therefore, we can draw the following bar chart showing each age group’s average minimum weekly study time. From the chart, we find that students aged 15 to 19 study at least 2 to 3 hours per week on average, with 18-year-olds averaging even 3 hours or more. We speculate that students aged 18 to 19 might have more study pressure due to college entrance exams, hence the longer study times. Meanwhile, students aged 20 and above are likely less interested in studying or may have other career plans.

Next, we plot the bar charts of after-school free time (freetime) and frequency of going out with friends (goout). We observe that students aged 15 to 19 have average scores around 3 for both free time and going out frequency, while students aged 20 and above tend to have even higher averages than the younger group. Comparing with the previous chart, it’s clear that students aged 20 and above differ significantly from those aged 15 to 19, so we should pay close attention to the effect of the age variable in later analyses.

Next is the bar chart showing students’ willingness to pursue higher education (higher) and their average final grade G3. From this chart, we can see that students who intend to continue their studies tend to have higher average G3 scores than those who do not, indicating that the intention to pursue further education is one factor influencing academic performance.

The following chart is particularly interesting. According to Portuguese law, the legal age of adulthood and the legal age for purchasing and consuming alcohol is 18. In the original dataset questionnaire, the lowest score for drinking behavior is 1, which represents very little or no alcohol consumption. Thus, students who do not drink at all should mostly score 1.
In the chart below showing health status and average weekday and weekend alcohol consumption by age, we visualize students’ self-assessed health along with their average alcohol consumption on weekdays and weekends. To facilitate intuitive comparison between weekly drinking amounts and health status, we stacked weekday and weekend alcohol consumption, assigning 4 days for weekdays and 3 days for weekends. This reflects that students spend most daytime hours at school (weekdays) and tend to drink mostly in the evenings and weekends, with Friday night often regarded as the start of the weekend.
Notably, some students under 18 show average alcohol consumption scores above 1, implying that some minors in the dataset do consume alcohol.

We want to find the top 10 most frequent combinations of Medu, Fedu, Mjob, Fjob, and compute the average G3 for each combination.
In both plots below, the blue bars on the left show the number of times each combination appears, and the red bars on the right show the average G3 for each combination.

After we computed the average G3 for the top 10 most frequent combinations, we want to find the top 10 combinations with the highest average G3.

We can see that when both Medu and Fedu are higher, and both Mjob and Fjob are teachers, the students’ average G3 is 13.00; when Fjob is other, the average G3 is 9.00; and when Mjob is services, the average G3 is 15.20.
Therefore, we can see that, at the same education levels, different Mjob and Fjob combinations also affect G3.
Multidimensional Scaling
The original dataset contains 33 variables, most of which have very low correlations with each other (close to zero), making it difficult to directly identify which variables relate to the G3 final grade. To address the problem of having too many variables with diluted explanatory power, we first grouped the variables based on their attributes and meanings. Then, for each group, we applied Multidimensional Scaling (MDS) to perform dimensionality reduction.
During the MDS process, we compared the distances among student samples within each group, compressing the originally high-dimensional variables into two dimensions. This approach attempts to preserve the relative relationships among samples as much as possible, reducing the number of variables while improving each group’s explanatory power regarding G3. The two new variables generated after MDS for each group are prefixed with the group name and named _dim1 and _dim2 respectively.
Below are the visualizations of the MDS results for each group, with student samples colored according to their corresponding G3 scores, to visually present the potential relationships between the reduced dimensions and academic performance.
Although we do not analyze G1, G2, and G3 directly, we still plot the samples of these variables after MDS dimensionality reduction for comparison with the other groups above.

After applying MDS, we then plot the correlation matrix to examine the relationships between the reduced variables.
If you are unable to view the interactive correlation matrix visualization or need to view it in full screen, please click here.
Pair Plot
Below is a pair plot to observe if the variables after MDS show stronger correlations.
Principal Component Analysis
Before performing Principal Component Analysis (PCA), we first standardize the variables. After standardizing all variables except the target variable G3, the data appears as follows:
| index | age | support_dim1 | support_dim2 | family_dim1 | family_dim2 | … | school_choice_dim1 | school_choice_dim2 | internet | romantic | sex_M |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.217015 | 1.087782 | -2.045391 | -0.911204 | -0.570128 | … | 0.238088 | -0.047286 | -2.38988 | -0.69196 | -0.952 |
| 1 | 0.393879 | -0.207043 | -0.836019 | 0.060769 | 1.546435 | … | 0.817325 | 0.615202 | 0.41843 | -0.69196 | -0.952 |
| 2 | -1.25239 | -2.043003 | -0.695022 | 0.514605 | 0.288704 | … | -1.813855 | 1.842271 | 0.41843 | -0.69196 | -0.952 |
| 3 | -1.25239 | -0.697640 | 1.009037 | 0.877950 | -0.557109 | … | -0.814591 | 0.867946 | 0.41843 | 1.44516 | -0.952 |
| 4 | -0.42925 | -0.697640 | 1.009037 | 0.700091 | 0.735734 | … | -0.814591 | 0.867946 | -2.38988 | -0.69196 | -0.952 |
| … | … | … | … | … | … | … | … | … | … | … | … |
| 344 | 1.217015 | -0.207043 | -0.836019 | 0.027745 | 0.240671 | … | 0.238088 | -0.047286 | 0.41843 | -0.69196 | -0.952 |
| 345 | 1.217015 | 1.386098 | -0.426182 | -0.600978 | 0.964057 | … | -1.803388 | 1.853657 | 0.41843 | 1.44516 | -0.952 |
| 346 | 1.217015 | 1.386098 | -0.426182 | -1.135662 | 0.929772 | … | 0.238088 | -0.047286 | 0.41843 | 1.44516 | 1.049 |
| 347 | 1.217015 | -0.697640 | 1.009037 | -0.600978 | 0.964057 | … | 0.238088 | -0.047286 | 0.41843 | 1.44516 | 1.049 |
| 348 | 0.393879 | -0.697640 | 1.009037 | 0.027745 | 0.240671 | … | 1.254272 | -1.000397 | 0.41843 | 1.44516 | -0.952 |
The most important feature is selected as the variable with the largest absolute loading value within each principal component.
| age | support_dim1 | support_dim2 | family_dim1 | family_dim2 | parents_dim1 | parents_dim2 | performance_dim1 | performance_dim2 | alcohol_dim1 | … | after_class_dim1 | after_class_dim2 | school_choice_dim1 | school_choice_dim2 | internet | romantic | sex_M | Explained Variance | Cumulative Explained Variance | Most Important Feature | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| PC1 | -0.31428 | -0.01492 | -0.09142 | 0.02471 | 0.07027 | 0.00225 | -0.01428 | 0.56291 | 0.56998 | -0.13261 | … | 0.12635 | 0.18285 | 0.12075 | -0.14428 | -0.12218 | -0.24775 | 0.05817 | 0.123739 | 0.123739 | performance_dim2 |
| PC2 | 0.05854 | -0.15301 | -0.05277 | -0.01508 | -0.08101 | 0.11834 | 0.12734 | -0.20177 | -0.15329 | -0.33308 | … | 0.27279 | -0.08147 | 0.55584 | -0.46000 | -0.06047 | 0.01056 | -0.35919 | 0.104542 | 0.228281 | school_choice_dim1 |
| PC3 | -0.25162 | -0.33288 | 0.12403 | 0.25912 | 0.04080 | -0.21617 | -0.19674 | -0.10099 | -0.08990 | -0.25778 | … | 0.42266 | 0.06376 | -0.24370 | 0.41533 | 0.16350 | 0.03533 | -0.27323 | 0.089062 | 0.317343 | after_class_dim1 |
| PC4 | 0.18089 | 0.23004 | -0.21277 | -0.12089 | -0.06424 | 0.55154 | 0.12192 | 0.08621 | 0.02896 | -0.15634 | … | 0.18814 | -0.14339 | -0.29892 | 0.29881 | -0.42365 | 0.07793 | -0.25812 | 0.082954 | 0.400297 | parents_dim1 |
| PC5 | 0.09728 | 0.23079 | -0.27532 | -0.35041 | 0.51664 | -0.11613 | -0.15667 | -0.16518 | -0.18069 | -0.06037 | … | 0.03337 | 0.38923 | 0.02474 | 0.00286 | 0.01517 | 0.10256 | 0.14521 | 0.076042 | 0.476339 | family_dim2 |
| PC6 | 0.16847 | -0.20377 | 0.52457 | -0.37602 | 0.41667 | 0.08991 | 0.33865 | 0.10907 | 0.18200 | -0.12810 | … | -0.16270 | -0.13117 | -0.06696 | 0.05962 | 0.24155 | 0.02600 | -0.20855 | 0.066698 | 0.543037 | support_dim2 |
| PC7 | -0.13403 | 0.16553 | 0.03740 | 0.34388 | -0.02806 | 0.25739 | 0.60921 | -0.03758 | -0.04892 | -0.08187 | … | 0.13169 | 0.40514 | -0.01908 | 0.00350 | 0.28333 | 0.25359 | 0.24462 | 0.060366 | 0.603402 | parents_dim2 |
| PC8 | 0.37215 | 0.32579 | 0.02033 | 0.26236 | 0.03516 | -0.07732 | -0.35951 | 0.23284 | 0.23434 | -0.24334 | … | -0.00137 | -0.07107 | 0.03513 | -0.09724 | 0.27742 | 0.51081 | -0.13665 | 0.058127 | 0.661529 | romantic |
| PC9 | -0.04030 | -0.39534 | 0.04481 | -0.07430 | -0.22586 | 0.09811 | -0.06035 | 0.07540 | 0.07128 | 0.42277 | … | -0.20239 | 0.26401 | 0.05025 | -0.02830 | -0.17433 | 0.56876 | -0.17478 | 0.054081 | 0.715610 | romantic |
| PC10 | 0.32727 | 0.17252 | 0.61629 | 0.01171 | -0.15288 | -0.13744 | -0.04751 | 0.01093 | -0.02539 | 0.12011 | … | 0.40903 | 0.11550 | 0.07008 | -0.01209 | -0.35258 | -0.04926 | 0.29073 | 0.049724 | 0.765334 | support_dim2 |
| PC11 | 0.22969 | -0.26312 | -0.00117 | -0.06563 | -0.09572 | -0.07595 | -0.05341 | -0.03554 | 0.03893 | -0.45823 | … | -0.19930 | 0.58174 | -0.09997 | 0.01158 | -0.27012 | -0.07807 | -0.03096 | 0.046235 | 0.811570 | after_class_dim2 |
| PC12 | 0.25752 | -0.17788 | 0.00275 | 0.60756 | 0.52036 | 0.23676 | -0.08113 | -0.06917 | 0.01976 | 0.27676 | … | -0.08760 | 0.07145 | 0.02855 | -0.07412 | -0.17312 | -0.23242 | -0.12077 | 0.041150 | 0.852720 | family_dim1 |
| PC13 | 0.18436 | 0.12262 | 0.09962 | -0.08477 | -0.35041 | 0.33747 | -0.24372 | 0.01910 | -0.05377 | 0.13521 | … | -0.07200 | 0.31544 | -0.03596 | -0.00568 | 0.48422 | -0.43045 | -0.22621 | 0.035231 | 0.887951 | internet |
| PC14 | -0.23559 | -0.18653 | 0.09688 | -0.21986 | 0.19514 | 0.50116 | -0.39481 | -0.01050 | -0.01521 | 0.05864 | … | 0.36308 | 0.02747 | 0.04392 | -0.10603 | 0.10683 | 0.14587 | 0.26635 | 0.034410 | 0.922361 | parents_dim1 |
| PC15 | 0.51158 | -0.48800 | -0.35236 | -0.03468 | -0.11947 | -0.00802 | 0.11331 | 0.09324 | 0.08548 | -0.02816 | … | 0.20338 | -0.17840 | -0.11567 | -0.01638 | 0.21865 | -0.03666 | 0.41258 | 0.032990 | 0.955352 | age |
| PC16 | 0.12207 | 0.07406 | -0.20073 | -0.16652 | 0.07581 | -0.29201 | 0.21268 | 0.08410 | 0.09351 | 0.42624 | … | 0.46395 | 0.18854 | -0.14068 | -0.13191 | 0.07381 | -0.02104 | -0.39930 | 0.024505 | 0.979856 | after_class_dim1 |
| PC17 | 0.11616 | -0.00761 | -0.09411 | -0.03805 | 0.02841 | -0.00119 | 0.03888 | 0.17998 | -0.03532 | 0.09370 | … | 0.02228 | 0.05660 | 0.67791 | 0.66363 | 0.04566 | -0.01960 | -0.00163 | 0.013824 | 0.993680 | school_choice_dim1 |
| PC18 | 0.00832 | 0.06218 | -0.03136 | -0.03381 | -0.06435 | 0.02330 | -0.01424 | -0.68519 | 0.70113 | 0.04366 | … | 0.02955 | 0.00129 | 0.08253 | 0.12981 | 0.02290 | -0.00060 | 0.02883 | 0.006320 | 1.000000 | performance_dim2 |
Plotting the variance helps us better analyze the dataset. The black line represents the explained variance of each principal component, the blue line shows the remaining unexplained variance, and the red line indicates the cumulative explained variance.

Of course, we can also calculate and present all variables transformed by PCA as shown below.
| Index | PC1 | PC2 | PC3 | PC4 | PC5 | PC6 | PC7 | … | PC15 | PC16 | PC17 | PC18 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | -5.510839 | 1.127293 | -4.117657 | 3.107669 | 2.864756 | 1.596422 | -5.251726 | … | 9.138033 | 0.807222 | 1.855121 | 0.349057 |
| 1 | -3.477549 | 1.540630 | -4.339357 | 4.639965 | 1.925990 | 4.441370 | -1.301991 | … | 9.372359 | 0.938431 | 2.687517 | 0.665990 |
| 2 | -7.996224 | 0.857119 | -2.236678 | 4.270145 | 1.455193 | 2.076697 | -0.357673 | … | 8.280973 | 1.682065 | 2.160862 | -3.14570 |
| 3 | -1.713179 | -0.138464 | -1.645506 | 2.585313 | 0.984078 | 2.730721 | -2.543466 | … | 8.935648 | 2.044699 | 1.432726 | 2.032150 |
| 4 | -3.250539 | 0.438469 | -3.461779 | 4.108225 | 1.205912 | 4.030496 | -0.474944 | … | 8.956492 | 2.556946 | 1.910259 | 0.608520 |
| … | … | … | … | … | … | … | … | … | … | … | … | … |
| 344 | -4.299600 | 1.316663 | -4.763758 | 3.739167 | 1.177631 | 4.059481 | -1.511099 | … | 10.022404 | 2.260172 | 2.207069 | 1.525158 |
| 345 | -7.601288 | -0.275631 | -3.954244 | 4.220185 | 2.496979 | 3.148704 | -2.224165 | … | 8.215032 | 1.755102 | 2.104987 | 0.928338 |
| 346 | -8.095515 | 1.264441 | -3.196450 | 1.115171 | 4.848870 | 1.962490 | -3.098767 | … | 9.138925 | 2.697971 | 1.810029 | 0.542200 |
| 347 | -1.969261 | -1.791564 | -6.257189 | 0.902714 | 0.909935 | 5.697392 | -2.812854 | … | 10.234261 | 3.180343 | 2.534447 | 2.330287 |
| 348 | -1.054689 | 0.131606 | -5.522433 | 1.743504 | 0.374565 | 5.017756 | -1.248375 | … | 9.365234 | 3.594221 | 2.535474 | 2.333115 |
The following shows a 3D scatter plot of the loadings for each variable on PC1, PC2, and PC3, along with their cumulative explained variance.
If you are unable to view the interactive PC1 vs. PC2 vs. PC3 3D scatter plot or need to view it in full screen, please click here.
Regression Model
Python offers many modules for regression modeling, such as sklearn and statsmodels. Among them:
sklearnis more oriented towards machine learning, emphasizing prediction and generalization ability, but many statistical metrics need to be coded manually.statsmodelsfocuses more on statistical modeling and inference, making it more suitable for academic or statistical analysis, and it provides convenient model summaries.
Below, we use the statsmodels module for analysis and plotting.
Original Dataset
We first run regression on the cleaned original dataset and observe:
- The original model has an $R^2$ of 0.269, indicating relatively low explanatory power. The adjusted $R^2$ is even lower, suggesting some variables may be redundant.
- The overall model P-value = $1.80 \times 10^{-9}$, indicating that some variables have a significant effect on
G3.failureshas a P-value of 0.000 and a negative coefficient, meaning the number of failures has a significant negative effect onG3; more failures lead to lower grades.goouthas a P-value of 0.001 and a negative coefficient, indicating that frequency of going out has a significant negative effect onG3.schoolsuphas a P-value of 0.000 and a negative coefficient, showing that students receiving extra school support tend to have lower grades, possibly because they need help due to learning difficulties.romantichas a P-value of 0.019 and a negative coefficient, indicating that having romantic experience has a significant negative effect onG3.sex_Mhas a P-value of 0.015 and a positive coefficient, meaning male students tend to have significantly higher grades than females.address_0has a P-value of 0.005 and a positive coefficient, indicating students living in urban areas score higher.famsize_1has a P-value of 0.004 and a positive coefficient, suggesting students from larger families perform significantly better.Pstatus_1has a P-value of 0.020 and a positive coefficient, indicating students with separated parents have significantly higher grades.Mjob_2(mother is a healthcare worker) has a P-value of 0.047 and a positive coefficient, showing this occupation positively affects student grades.Mjob_3(mother is a civil servant) has a P-value of 0.012 and a positive coefficient, indicating a significant positive effect on grades from this occupation.
| |
Dataset After MDS Transformation
- The overall model P-value = $4.23 \times 10^{-8}$, indicating the model’s prediction of
G3is statistically significant.agehas a P-value of 0.011 with a negative coefficient, meaning that as age increases, the grades tend to decrease.support_dim1has a P-value of 0.009 with a positive coefficient, indicating that the first dimension of support-related features (such as school and family support) is significantly positively correlated withG3.support_dim2has a P-value of 0.005 with a positive coefficient, showing that the second dimension of support also has a significant positive effect.family_dim2has a P-value of 0.009 with a positive coefficient, meaning the second dimension of family-related factors is significantly positively associated withG3.parents_dim1has a P-value of 0.046 with a negative coefficient, suggesting some parental background factors may have a significant negative impact onG3.performance_dim1has a P-value of 0.030 with a negative coefficient, possibly indicating some academic performance background factors negatively relate toG3.after_class_dim1has a P-value of 0.014 with a positive coefficient, indicating the first dimension of after-school activities has a significant positive influence onG3.after_class_dim2has a P-value of 0.034 with a positive coefficient, showing the second dimension of after-school activities also has a significant positive effect.romantichas a P-value of 0.008 with a negative coefficient, indicating that having a romantic relationship significantly negatively affectsG3.
| |
Dataset After MDS and PCA Transformation
All data points were transformed using PCA, and then regression analysis was performed. From the regression model, we observed:
- The MSE of the PCA-transformed data is the same as that of the previous model.
- When performing regression using the principal components, we found that PC1, PC4, PC9, PC10, and PC15 have a more significant impact on
G3.
From the earlier analysis of important variables within each principal component, we found that the variables influencing this model are:
| PC1 | PC4 | PC9 | PC10 | PC15 | |
|---|---|---|---|---|---|
| Import Variable | performance_dim2 | parents_dim1 | romantic | support_dim2 | age |
| Impact | + | - | - | + | - |
| |
Dataset After MDS and PCA Transformation Using 80% Explained Variance
We conducted regression analysis using only the principal components that collectively explain 80% of the total variance. The results show:
- The $R^2$ of the model using the top 80% explained variance PCs is 0.137, indicating a decrease in explanatory power compared to the original dataset model.
- From the regression results, we found that PC1, PC4, PC5, and PC9 have a more significant impact on
G3.
| PC1 | PC4 | PC5 | PC9 | |
|---|---|---|---|---|
| Import Variable | performance_dim2 | family_dim2 | romantic | |
| Impact | + | - | + | + |
Since this dataset can still be directly fed into a regression model for training, there is no need to apply PCA for dimensionality reduction beforehand. Doing so would actually reduce the model’s explanatory power and increase the mean squared error (MSE).
| |
QQ plot
The following figure shows the QQ plots of the four regression models described above. From the QQ plots of the four models, we can assess whether the residuals approximate a normal distribution:
- Original data (excluding
MS,G1, andG2) + regression: best overall fit, but the right tail deviates significantly, indicating large prediction errors for extremely high scores. - MDS + regression: a simpler model, but the tails of the residuals deviate more, suggesting the influence of outliers.
- MDS + PCA + regression: slight improvement in tail deviation, but the middle-to-tail range still does not align with the ideal line; residuals are still not ideal.
- MDS + PCA (80% explained variance) + regression: the most noticeable deviation from normality, especially in the right tail, likely due to excessive information compression in PCA.
None of the four models fully satisfy the assumption of normally distributed residuals, but the model using the original dataset comes closest.
Stepwise Variable Selection
Do you remember our goal? We aim to identify which variables influence G3. Therefore, we will gradually add variables into the regression model and evaluate whether each added variable significantly improves the model’s ability to fit G3.
Sound familiar? That’s right, we plan to implement a procedure similar to the step() function in R. The following section introduces an implementation of bidirectional elimination stepwise regression, which will be used to select influential variables.
| |
Stepwise regression consists of three approaches:
Forward selection: The regression model starts with no variables. Then, variables are added one by one based on their statistical significance, forming the final set of predictors.
Backward elimination: All potential predictors are included at the beginning. Then, variables are removed one by one based on their lack of contribution to the model.
Bidirectional elimination: Similar to forward selection, variables are added step by step. However, any variable that becomes insignificant during the process will also be removed.
By setting a threshold of p-value = 0.05 for variable selection, the resulting variables from each model are as follows:
| Method | Selected Variables |
|---|---|
Original data (excluding MS, G1, G2) + regression | failures, goout, Mjob_1, Mjob_0, sex_M |
| MDS + regression | parents_dim1, after_class_dim1, family_dim2, romantic, support_dim2, after_class_dim2, support_dim1, age, performance_dim1, const |
| MDS + PCA + regression | PC9, PC1, PC15, PC10, PC8, PC18 |
| MDS + PCA (80% explained variance) + regression | PC4, PC9, PC1, PC8, PC10 |
PCA Again
Here, we perform PCA once more, this time using only the variables selected from the MDS + stepwise regression process. The results are as follows:
| PC | parents_dim1 | after_class_dim1 | family_dim2 | romantic | support_dim2 | after_class_dim2 | support_dim1 | age | performance_dim1 | const | Explained Variance | Cumulative Explained Variance | Most Important Feature |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| PC1 | 0.213121 | -0.205577 | -0.060527 | 0.434094 | 0.056771 | -0.286477 | 0.223825 | 0.623287 | -0.443102 | 0.0 | 0.166152 | 0.166152 | age |
| PC2 | 0.448179 | -0.083659 | 0.074404 | -0.061783 | -0.540231 | 0.133070 | 0.588515 | 0.021220 | 0.355564 | 0.0 | 0.143947 | 0.310098 | support_dim1 |
| PC3 | -0.406978 | -0.415160 | 0.623954 | 0.116911 | 0.022111 | 0.475853 | 0.128447 | 0.110293 | -0.058623 | -0.0 | 0.129111 | 0.439210 | family_dim2 |
| PC4 | 0.081492 | 0.512700 | -0.044706 | 0.497163 | -0.318840 | 0.496922 | -0.143583 | -0.122033 | -0.311816 | -0.0 | 0.113825 | 0.553034 | after_class_dim1 |
| PC5 | 0.258220 | 0.589895 | 0.613767 | -0.218113 | 0.319698 | -0.105588 | 0.107590 | 0.189307 | -0.017143 | -0.0 | 0.104848 | 0.657882 | family_dim2 |
| PC6 | 0.248523 | -0.047805 | -0.180789 | 0.402815 | 0.601967 | 0.323911 | 0.032664 | 0.052785 | 0.519491 | 0.0 | 0.100250 | 0.758132 | support_dim2 |
| PC7 | -0.600307 | 0.359132 | -0.276512 | 0.029457 | 0.122379 | -0.030260 | 0.643747 | 0.043791 | 0.033295 | -0.0 | 0.091065 | 0.849197 | support_dim1 |
| PC8 | -0.128970 | 0.015051 | 0.337069 | 0.578870 | -0.116411 | -0.555072 | -0.001069 | -0.386453 | 0.251855 | 0.0 | 0.083395 | 0.932592 | romantic |
| PC9 | -0.275680 | 0.189882 | -0.003829 | 0.033395 | -0.331180 | -0.042056 | -0.373340 | 0.627789 | 0.491798 | 0.0 | 0.067408 | 1.000000 | age |
| PC10 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 1.0 | 0.000000 | 1.000000 | const |
From the previous table, it’s not easy to clearly understand the relationships between variables. Therefore, we again performed PCA on the selected variables and plotted the explained variance. It can be observed that at least PC7 is needed to explain over 80% of the cumulative variance.

Next, we plotted the sample scatterplots of PC1 vs. PC2 using both the original dataset and the PCA after variable selection, with G3 scores used for color mapping. To better highlight the contrast between high and low scores:
- Scores from 15 to 20 are labeled as high and colored red.
- Scores from 0 to 5 are labeled as low and colored blue.

In the figure above:
- The left plot shows the result of PCA on the cleaned original dataset.
- The right plot shows PCA results after stepwise variable selection (SW PCA).
PCA: PC1 vs. PC2
- High-, mid-, and low-score samples are mixed and do not form clear clusters or boundaries.
- Although PC1 and PC2 capture the most total variance, they may not be directly related to
G3. - This suggests that PCA on all original variables may not provide strong discriminative power for
G3.
SW PCA: PC1 vs. PC2
- High-score samples are mainly concentrated in the positive PC1 region.
- Low-score samples are mainly concentrated in the negative PC1 region.
This indicates that performing PCA after variable selection enables principal components, especially PC1, to better distinguish between different performance levels.
QQ Plot
After selecting variables, we conducted regression analysis and plotted the QQ plots for four models, as shown below:

Model Comparison
It is well known that when applying PCA, selecting only the top n% of components inevitably leads to information loss compared to using all principal components. Therefore, we exclude such models from further discussion here.
The following section compares the six models previously introduced.
| Model | $R^2$ | Adj. $R^2$ | MSE | P-value | AIC |
|---|---|---|---|---|---|
Original dataset (excluding MS, G1, and G2) + Regression | 0.268987 | 0.194960 | 62.583249 | 0.915722 | 2015.098094 |
| MDS + Regression | 0.187554 | 0.143239 | 77.576792 | 0.749395 | 2023.958578 |
| MDS + PCA + Regression | 0.187554 | 0.143239 | 77.576792 | 0.614297 | 2023.958578 |
Original dataset (excluding MS, G1, and G2) + Stepwise Regression | 0.197138 | 0.185435 | 293.547188 | 0.036654 | 1993.817204 |
| MDS + Stepwise Regression | 0.169384 | 0.147332 | 140.122389 | 0.020078 | 2013.677881 |
| MDS + PCA + Stepwise Regression* | 0.169384 | 0.147332 | 140.122389 | 0.511294 | 2013.677881 |
From the table above, we observe that the model using the original dataset (excluding MS, G1, and G2) + regression demonstrates the best fit for predicting G3. It achieves the highest explanatory power, as indicated by both $R^2$ and adjusted $R^2$, and also yields the lowest mean squared error (MSE), suggesting that it captures the data variability well and offers more accurate predictions.
On the other hand, the original dataset (excluding MS, G1, and G2) + stepwise regression and MDS + PCA + stepwise regression models have relatively low P-values. This indicates that the variables selected in these models have statistically significant effects on G3, meaning the models are effective at identifying predictors with strong explanatory power.
When comparing AIC values, the original dataset + stepwise regression model has the lowest AIC, suggesting it offers the best overall performance by balancing explanatory strength and model complexity.
The following table shows the top three influential variables for each model.
| Model | Top 1 | Top 2 | Top 3 |
|---|---|---|---|
Original data (excluding MS, G1, and G2) + Regression | failures | schoolsup | paid |
| MDS + Regression | romantic | support_dim2 | support_dim1 |
| MDS + PCA + Regression | PC9 | PC10 | PC15 |
Original data (excluding MS, G1, and G2) + Stepwise Regression | Mjob_1 | failures | Mjob_0 |
| MDS + Stepwise Regression | romantic | support_dim2 | support_dim1 |
| MDS + PCA + Stepwise Regression | PC9 | PC7 | PC5 |
In summary, we select the Original data (excluding MS, G1, and G2) + Stepwise Regression model as the model for evaluating G3. From this model, we find that the variables influencing students are Mjob and failures, indicating that family background and student performance in school courses are key factors affecting the final grades.
Conclusion
Through this collaborative data analysis, we have learned many analytical methods that we had not previously encountered. Through brainstorming, we were able to uncover hidden yet significant information within the data. Since the dataset mainly consists of categorical variables, a key challenge in this project was how to properly transform these into numerical variables for further analysis. Improper transformation may lead to information loss; therefore, selecting appropriate processing methods was crucial and represents an important data analysis skill we have acquired.
We would like to express our gratitude to Wang, Xuan-Chun and Sin, Wen-Lee for their assistance in the analysis, which allowed this project to proceed smoothly.
Further Learning
- The ipynb file used in this article.
- The Github repository for this article.
References
Adil Shamim. (April 2025). Math-Students Performance Data. Kaggle. Retrieved on May 1, 2025, from https://www.kaggle.com/datasets/adilshamim8/math-students
Cortez, P. (2008). Student Performance [Dataset]. UCI Machine Learning Repository. from https://doi.org/10.24432/C5TG7T
Cortez, P., & Silva, A. M. (2008). Using data mining to predict secondary school student performance.




![[Thought] Historical Earthquake Locations Around Taiwan](https://Josh-test-lab.github.io/posts/Historical%20Earthquake%20Locations%20Around%20Taiwan/cover%20image.webp)






