Accessing Post Source
We are still working on getting this site set up, so source code for this post is not yet available. Check back soon and you’ll be able to find it linked here.
With exam period approaching fast, every student is wondering how to score the best possible grade. Some factors—like how much sleep you’re getting or how healthy you are—seem to have an obvious correlation with your final grade. What about your relationship status? How much should you be studying to achieve the grade you want? Does the subject you’re studying influence your final grade? In this article, we will use two datasets containing student math and Portuguese language performance in two different Portuguese schools and see which factors affected student performance the most.
Exploratory Data Analysis
The variables are the same for the two datasets:
|school||School||binary||GP—Gabriel Pereira; MS—Mousinho da Silveira|
|address||Address type||binary||U—Urban; R—Rural|
|famsize||Family Size||binary||LE3—less than or equal to 3; GE3—greater than 3|
|Pstatus||Parent’s cohabitation status||binary||T—living together; A—living apart|
|Medu||Mother’s Education||ordinal||0—none; 1—up to 4th grade; 2—5th–9th grade; 3—secondary; 4—higher|
|Fedu||Father’s Education||ordinal||0—none; 1—up to 4th grade; 2—5th–9th grade; 3—secondary; 4—higher|
|Mjob||Mother’s Job||nominal||teacher; health(-care related); (civil )services; at home; other|
|Fjob||Father’s Job||nominal||teacher; health(-care related); (civil )services; at home; other|
|reason||Reason for choosing school||nominal||(close to )home; (school )reputation; course(preference); other|
|guardian||Student’s guardian||nominal||mother; father; other|
|traveltime||Travel time to school||ordinal||1—<15 min.; 2—15–30 min.; 3—30 min.–1 hour; 4—>1 hour|
|studytime||Weekly study time||ordinal||1—<2 hours; 2—2–5 hours; 3—5–10 hours; 4—>10 hours|
|failures||Past class failures||numeric||0–3, else 4|
|schoolsup||Extra educational support||binary||yes; no|
|famsup||Family educational support||binary||yes; no|
|paid||Extra paid classes||binary||yes; no|
|activities||Extra-curricular activities||binary||yes; no|
|nursery||Attend nursery||binary||yes; no|
|higher||Wants to take higher education||binary||yes; no|
|internet||Home internet access||binary||yes; no|
|romantic||In a romantic relationship||binary||yes; no|
|famrel||Quality of family relationships||ordinal||1—very bad to 5—very good|
|freetime||Free time after school||ordinal||1—very low to 5—very high|
|goout||Going out with friends||ordinal||1—very low to 5—very high|
|Dalc||Workday alcohol consumption||ordinal||1—very low to 5—very high|
|Walc||Weekend alcohol consumption||ordinal||1—very low to 5—very high|
|health||Current health status||ordinal||1—very bad to 5—very good|
|G1||First Period Grade||numeric||0–20|
|G2||Second Period Grade||numeric||0–20|
I will be conducting a basic analysis of the dataset followed by visualizations of the correlations between different factors. Finally, I will build a linear regression model for each subject to predict the students’ final grades.
We will start by importing all the necessary packages and load the datasets into a pandas dataframe.
# Import necessary packages
We can start by taking a look at the first few rows of each dataset.
First 5 lines of the math performance dataset:
First 5 lines of the Portuguese performance dataset:
An important detail to note is that there are 395 high school students in the math dataset and 649 in the Portuguese dataset. The grades of the student are from 0 to 20. Furthermore, there are 16 numerical variables out of 33; the rest of the variables will need to be one-hot encoded when we will analyze correlations and build the regression model.
Now let’s visualize the final grades distributions for both subjects.
We can also calculate that the average final grades for math and Portuguese students are 10.42 and 11.91, respectively. This suggests that Portuguese students score higher on average than math students although this comparison could easily have been skewed by the large number of math students scoring zero.
Finding and Visualizing Correlations for Numerical Variables
We are now going to automatically find the variables with the strongest correlation to the final grades for both datasets. Finding correlations between non-numeric features and the outcome can get a bit messy, so we will focus on testing only the existing numerical values of the datasets at first. To better visualize the insights, we will also use correlation bar plots and heat maps for both datasets.
To interpret correlation bar plots and heat map:
- The darker the bar/square, the stronger the correlation is.
- Brown represents negative correlations, whereas purple represents positive correlations.
- For both datasets, the number of past classes
failureshas a strong negative correlation with
- Other common variables with a negative correlation are
age, frequency of going out with friends (
G2have very strong positive correlation coefficients for both datasets because student performance usually remains constant throughout the year; we will therefore ignore them.
- Other common variables with a positive correlation are:
studytime, education of parents (
Medu) and family relationship (
In order to get more insight from these datasets, we need to be able to use the categorical variables as well. An example of categorical variable is the
school variable (the student is either at Gabriel Pereira or Mousinho da Silveira) as there are multiple possible values with no intrinsic ordering. We will use a technique called one-hot encoding, which assigns binary value to each category level indicating whether or not that level was the value of the original predictor. Here is an example of how it would look like for the variable father’s job (
One-hot encoding is actually slightly more subtle than the description given above. The missing detail is that we often drop the first of the resulting binary columns. The reason we do this is that knowing the value of the other columns is enough to be certain of the value of the first. Indeed, if all of the other columns are zero, then the first column must be one, and vice-versa. Removing the first column is important as the algebra behind linear regression fails we duplicate predictor information.
Finding and Visualizing Correlations for Encoded Categorical Variables
We can now analyze the correlation coefficients for the final grades of all the variables for both datasets.
- Variables that impact negatively final grades in both datasets: in a romantic relationship (
romantic_yes), does not want to go to higher education (
higher_no), lives in a rural area (
address_R) and has no access to internet (
- Variables that impact positively final grades in both datasets: not in a romantic relationship (
romantic_no), wants to go to higher education (
higher_yes), lives in a urban area (
address_U), has access to internet (
- In Portuguese performance dataset, the
schoolvariable has a very high impact on the final grade (negatively impacted if goes to MS and positively impacted if goes to GP).
- Males seem to score higher in math whereas females score higher in portuguese.
Visualizing Key Trends
Some of the results are quite unexpected so let’s visualize them.
Effect of Address Type on Grades
- For math performance, there is not too much difference between urban and rural students. However, urban students tend to score slightly more.
- For portuguese performance, we can see that urban students score higher more often than rural students.
Effect of Relationship Status on Grades
Note, that of the 395 math students, 132 (33.4%) were in a relationship. Likewise 239 (36.8%) or of the 649 Portuguese students were in a relationship
- In both datasets, there are more single students than in a relationship (only 33% in math dataset and 36% in portuguese dataset). This might skew results as there is less data to analyze for students in a relationship. We can see that in the Portuguese dataset where there are more values to analyze, the scatter plot shapes tend to look more similar.
- Not enough data to say if relationship has true impact on math performance.
Effect of Sex on Grades
fig, axs = plt.subplots(2, 1, figsize=(12,10))
Effect of School Choice on Grades
#Analyzing impact of choice of school on Portuguese performance
- From the available data, MS students (
school_MS) tend to score less than GP students (
school_GP) in Portuguese. Maybe GP is specialized in Portuguese and students have access to higher-quality resources.
- However as for the relationship analysis, there are less students going to MS so it might affect results.
We are now going to build a multi-linear regression model for both datasets. To avoid the impact of correlated variables, we only use the top twelve most influential predictors. We start with the math scores.
def fit_regression_model(df, dummy_df):
r2_m, coeffs_m = fit_regression_model(data_m, dummy_dfm)
Model R^2: 0.20
Insights for math data set linear regression model:
- Our model explains explains 20% of the inputs into the final grade (
G3), however it could still be improve if the goal of this article would be pure accuracy.
- We can see that the willingness of the student to go into higher education (
higher_yes) is a variable with one of the largest absolute coefficients. If the student is willing to go into higher education, their score will increase, on average, by 1.44 points.
- There are other statistically significant coefficients such as
- For example,
failuresplays a decisive role in student performance: for each class the student has failed in the past, they can roughly except a decrease of 1.74 in their final score.
And now for the Portuguese scores.
r2_p, coeffs_p = fit_regression_model(data_p, dummy_dfp)
Model R^2: 0.305
Insights for the portuguese data set linear regression model:
- Our model explains explains 30.5% of the inputs into the final grade (
G3), better than the math model but still leaving room for improvement.
- We again see that the desire to go into higher education and the number of previous failures are highly influential when determining a student’s final grade.
- There are other statistically significant coefficients such as
- In fact, the influence of going to Mousinho da Silveira is strong, with an expected decrease in one mark in a student’s Portuguese grade
We have seen that many factors can influence your final grades, the strongest of which typically being socio-economic characteristics (address, parent’s education, family relationship, etc.) that cannot be changed. Those factors can also depend on the potential biases of the dataset. For example, maybe the mother’s unemployment status has a bigger cultural impact on Portuguese student than on UK students. However, some variables that are controllable by the student such as
studytime, going out (
goout), consumption of alcohol (
Walc) and potentially relationship status (
romantic) have been proved to have an impact on the final grade (
G3) of students in these datasets.
Although valuable insights have been gleaned from this dataset it is clear from our poorly fitting regression model that linear interactions alone are insufficient for capturing a system as complicated as a student’s school performance. If a purely performative model is what we desired, then moving towards a tree-based model or including carefully chosen interaction terms would be advised.