Damage caused by Gorkha earthquake. Image by Prakash Mathema/AFP/Getty Images

by George Taniwaki

This is a continuation of my notes for a machine learning class offered by edX. Part 1 of this blog entry is posted in June 2018.

Step 4: Multivariate analysis

Pairwise scatterplots of the numerical variables after adjusting and normalizing are shown in Figure 4 below. The dependent variable (damage_grade) does not appear to be correlated with any of the numerical independent variables. Despite the lack of correlation, I included all the numeric variables when building the model. If I have time, I will convert these numerical variables into categorical ones.

Among the independent variables, covariance is highest between count_floors_pre_eq and height highlighted in green. This makes sense, taller buildings are likely to have more floors. If I have time, I will add a new variable height_per_floor (= height / count_floors_pre_eq).

Figure 4. Pairwise scatterplots of all numerical parameters. Correlations highlighted in green

There are 11 categorical and 23 binary parameters. I used the Chi-square test to compare distributions of these to the distribution of the dependent variable damage_grade, treated as categorical. The results are shown in Table 5 below.

All are statistically significant at 0.05 level, except for the 5 highlighted in red brackets. They will be excluded from the model. Two of the geo_level variables consume too many degrees of freedom given our sample size. (Even big datasets have limitations.) They are highlighted in orange and will be excluded from the model. If I have time, I will add a new custom geo_level variable with about 1000 degrees of freedom. The remaining 27 variables will be retained for use in the model.

Variable nameChi-squareDeg. of freedomP value
geo_level_1_id274660< 2.2e-16*
geo_level_2_id65922272< 2.2e-16*
geo_level_3_id1303910342< 2.2e-16*
land_surface_condition15.940.0032
foundation_type18578< 2.2e-16*
roof_type11224< 2.2e-16
ground_floor_type13478< 2.2e-16*
other_floor_type11176< 2.2e-16
position47.361.6e-08
plan_configuration46.5168.0e-05*
legal_ownership_status80.263.2e-15
has_superstructure_adobe_mud50.321.1e-11
has_superstructure_mud_mortar_stone10532< 2.2e-16
has_superstructure_stone_flag28.626.2e-07
has_superstructure_cement_mortar_stone53.822.1e-12
has_superstructure_mud_mortar_brick37.527.3e09
has_superstructure_cement_mortar_brick6322< 2.2e-16
has_superstructure_timber64.928.0e-15
has_superstructure_bamboo55.121.1e-12
has_superstructure_rc_non_engineered2962< 2.2e-16
has_superstructure_rc_engineered6032< 2.2e-16*
has_superstructure_other9.820.0072
has_secondary_use76.22< 2.2e-16
has_secondary_use_agriculture25.023.8e-06
has_secondary_use_hotel90.92< 2.2e-16
has_secondary_use_rental49.421.9e-11
has_secondary_use_institution10.820.0046*
has_secondary_use_school32.121.1e-07*
has_secondary_use_industry2.32[ 0.31 ]*
has_secondary_use_health_post1.52[ 0.46 ]*
has_secondary_use_gov_office4.22[ 0.12 ]*
has_secondary_use_use_police0.82[ 0.68 ]*
has_secondary_use_other15.320.00049*
has_missing_age3.12[ 0.21 ]*
Table 5. Chi-square results of categorical and Boolean values to damage_grade

*One or more enums have sample sizes too small to use Chi-square approximation
[ ] P value greater than 0.05 significance level

Step 5: Building the model

The dependent variable can take on the value 1, 2, or 3. I could use a classification method like multi-class logistic regression to create our model. However, there is a better way. I will use the ordinal regression algorithm available from Microsoft Azure Machine Learning (MAML).

Ordinal regression requires a binary classifier method. For this project, I will try three classifiers available in MAML, two-class logistic regression, two-class decision forest, and two-class support vector machine (SVM). I will use the default parameters for each classifier and submit all three models as entries in the contest. (This is sort of a cheat to improve the F1 score and my grade. In practice, you should only submit the results using the best model based on the training data.)

A simple model configuration using a static data split and without either a cross-validation step or a hyperparameter tuning step to optimize results is shown in Figure 5 below. I will add these steps later if the simple model does not meet the performance goals.

Figure 5. Layout of a simple MAML Studio experiment

I used a data split of 0.67, meaning 6,700 records were used to train the model and the remaining 3,300 were used to score and evaluate it. I used a random number seed of 123 to ensure every run of my experiment used the same split and produced replicable results.

Step 6: Measuring performance and testing results

A generic truth table for an experiment with 3 outcomes is shown in Table 6a below. Using data from the truth table, four performance metrics can be calculated, average accuracy, micro-average precision , micro-average recall, and geometric average F1 score. The calculation of the performance metrics is shown in Table 6b. Note that since all combinations are measured, recall, precision, and F1 score are all equal. Perhaps the contest should have used the macro-average recall and precision to calculate the F1 score.

Truth tableIs 1Is 2Is 3TOTAL
Predict 1TP1|TN2|TN3FP1|FN2|TN3FP1|TN2|FN3TP1+FP1
Predict 2FN1|FP2|TN3TN1|TP2|TN3TN1|FP2|FN3TP2+FP2
Predict 3FN1|TN2|FP3TN1|FN2|FP3TN1|TN2|TP3TP3+FP3
TOTALTP1+FN1TP2+FN2TP3+FN3Pop
Table 6a. Generic truth table for case with 3 outcomes
PerformanceCalculation
Avg Accuracy∑((TP + TN) / Pop) / 3
Avg Precision (micro)P = ∑TP / ∑(TP + FP)) = ∑TP / Pop
Avg Recall (micro)R = ∑TP / ∑(TP + FN)) = ∑TP / Pop
F1 Score(2 * P * R) / (P + R) = ∑TP / Pop
Table 6b. Performance measures for case with 3 outcomes

Grading for the course is based on the F1 score for a hidden subset of the test dataset as shown in Table 7 below. F1 scores between these points will receive linearly proportional grades. For instance,  an F1 score of 0.65 would earn a grade of 75.

F1 ScoreGrade
< 0.601 out of 100
0.6060/100
0.6470/100
0.6680/100
0.7095/100
Table 7. Grading of project based on F1 score (test data)

Below are the results of my models.

Model 1 – Naïve guessing

The most common value for damage_grade is 2. Any prediction model should perform better than the naïve guess of predicting the damage is 2 for all buildings.

Truth tableIs 1Is 2Is 3TOTAL
Predict 10000
Predict 2310186211283300
Predict 30000
TOTAL310186211283300
Table 8a. Truth table for naive guess of damage_grade = 2
PerformanceValue
Avg Accuracy0.71
F1 Score0.56
F1 Score (test data)Not submitted
Table 8b. Performance measures for naive guess of damage_grade = 2

Model 2 – Ordinal regression model using 2-class logistic regression classifier

The default parameters for the 2-class logistic regression classifier are: Optimization tolerance = 1E-07, L1 regularization weight = 1, L2 regularization weight = 1. Notice in Table 9b the large gap between the F1 score using the training data and the test data. This indicates the model is overfitted.

Truth tableIs 1Is 2Is 3TOTAL
Predict 184521137
Predict 222315675362326
Predict 33243591837
TOTAL310186211283300
Table 9a. Truth table for ordinal regression model using 2-class logistic regression classifier
PerformanceValue
Avg Accuracy0.79
F1 Score0.68
F1 Score (test data)0.5687
Table 9b. Performance measures for ordinal regression model using 2-class logistic regression classifier

Model 3 – Ordinal regression model using 2-class decision forest classifier

The default parameter settings are: Resampling method = Bagging, Trainer mode = Single parameter, Number of decision trees = 8, Maximum depth of trees = 32, Number of random splits per need = 128, minimum number of samples per node = 1.

Truth tableIs 1Is 2Is 3TOTAL
Predict 185551144
Predict 221813354321985
Predict 374726921171
TOTAL310186211283300
Table 10a. Truth table for ordinal regression model using decision forest classifier
PerformanceValue
Avg Accuracy0.76
F1 Score0.64
F1 Score (test data)Not submitted
Table 10b. Performance measures for ordinal regression model using decision forest classifier

Model 4 – Ordinal regression model using 2-class support vector machine (SVM) classifier

The default parameter settings are: Number of iterations = 1, Lambda = 0.001.

Truth tableIs 1Is 2Is 3TOTAL
Predict 13121355
Predict 227316679322872
Predict 36174193373
TOTAL310186211283300
Table 11a. Truth table for ordinal regression model using SVM classifier
PerformanceValue
Avg Accuracy0.72
F1 Score0.57
F1 Score (test data)0.5644
Table 11b. Performance measures for ordinal regression model using SVM classifier

Summary of models in step 6

Unfortunately, none of my initial models performed well. The F1 score never meets the target of 0.70. In fact, in some cases my model don’t do much better than just guessing. (Note: You can see my scores on the contest leaderboard.) In the next section, we will add a cross-validation step and a hyperparameter tuning step to optimize the models and and see if that improves them.

This completes steps 4 to 6 of building a machine learning model. The remaining optimization step and the results are posted at How to create a machine learning model – Part 3.