
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).

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 name | Chi-square | Deg. of freedom | P value |
| geo_level_1_id | 2746 | 60 | < 2.2e-16* |
| geo_level_2_id | 6592 | 2272 | < 2.2e-16* |
| geo_level_3_id | 13039 | 10342 | < 2.2e-16* |
| land_surface_condition | 15.9 | 4 | 0.0032 |
| foundation_type | 1857 | 8 | < 2.2e-16* |
| roof_type | 1122 | 4 | < 2.2e-16 |
| ground_floor_type | 1347 | 8 | < 2.2e-16* |
| other_floor_type | 1117 | 6 | < 2.2e-16 |
| position | 47.3 | 6 | 1.6e-08 |
| plan_configuration | 46.5 | 16 | 8.0e-05* |
| legal_ownership_status | 80.2 | 6 | 3.2e-15 |
| has_superstructure_adobe_mud | 50.3 | 2 | 1.1e-11 |
| has_superstructure_mud_mortar_stone | 1053 | 2 | < 2.2e-16 |
| has_superstructure_stone_flag | 28.6 | 2 | 6.2e-07 |
| has_superstructure_cement_mortar_stone | 53.8 | 2 | 2.1e-12 |
| has_superstructure_mud_mortar_brick | 37.5 | 2 | 7.3e09 |
| has_superstructure_cement_mortar_brick | 632 | 2 | < 2.2e-16 |
| has_superstructure_timber | 64.9 | 2 | 8.0e-15 |
| has_superstructure_bamboo | 55.1 | 2 | 1.1e-12 |
| has_superstructure_rc_non_engineered | 296 | 2 | < 2.2e-16 |
| has_superstructure_rc_engineered | 603 | 2 | < 2.2e-16* |
| has_superstructure_other | 9.8 | 2 | 0.0072 |
| has_secondary_use | 76.2 | 2 | < 2.2e-16 |
| has_secondary_use_agriculture | 25.0 | 2 | 3.8e-06 |
| has_secondary_use_hotel | 90.9 | 2 | < 2.2e-16 |
| has_secondary_use_rental | 49.4 | 2 | 1.9e-11 |
| has_secondary_use_institution | 10.8 | 2 | 0.0046* |
| has_secondary_use_school | 32.1 | 2 | 1.1e-07* |
| has_secondary_use_industry | 2.3 | 2 | [ 0.31 ]* |
| has_secondary_use_health_post | 1.5 | 2 | [ 0.46 ]* |
| has_secondary_use_gov_office | 4.2 | 2 | [ 0.12 ]* |
| has_secondary_use_use_police | 0.8 | 2 | [ 0.68 ]* |
| has_secondary_use_other | 15.3 | 2 | 0.00049* |
| has_missing_age | 3.1 | 2 | [ 0.21 ]* |
*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.

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 table | Is 1 | Is 2 | Is 3 | TOTAL |
| Predict 1 | TP1|TN2|TN3 | FP1|FN2|TN3 | FP1|TN2|FN3 | TP1+FP1 |
| Predict 2 | FN1|FP2|TN3 | TN1|TP2|TN3 | TN1|FP2|FN3 | TP2+FP2 |
| Predict 3 | FN1|TN2|FP3 | TN1|FN2|FP3 | TN1|TN2|TP3 | TP3+FP3 |
| TOTAL | TP1+FN1 | TP2+FN2 | TP3+FN3 | Pop |
| Performance | Calculation |
| 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 |
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 Score | Grade |
| < 0.60 | 1 out of 100 |
| 0.60 | 60/100 |
| 0.64 | 70/100 |
| 0.66 | 80/100 |
| 0.70 | 95/100 |
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 table | Is 1 | Is 2 | Is 3 | TOTAL |
| Predict 1 | 0 | 0 | 0 | 0 |
| Predict 2 | 310 | 1862 | 1128 | 3300 |
| Predict 3 | 0 | 0 | 0 | 0 |
| TOTAL | 310 | 1862 | 1128 | 3300 |
| Performance | Value |
| Avg Accuracy | 0.71 |
| F1 Score | 0.56 |
| F1 Score (test data) | Not submitted |
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 table | Is 1 | Is 2 | Is 3 | TOTAL |
| Predict 1 | 84 | 52 | 1 | 137 |
| Predict 2 | 223 | 1567 | 536 | 2326 |
| Predict 3 | 3 | 243 | 591 | 837 |
| TOTAL | 310 | 1862 | 1128 | 3300 |
| Performance | Value |
| Avg Accuracy | 0.79 |
| F1 Score | 0.68 |
| F1 Score (test data) | 0.5687 |
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 table | Is 1 | Is 2 | Is 3 | TOTAL |
| Predict 1 | 85 | 55 | 1 | 144 |
| Predict 2 | 218 | 1335 | 432 | 1985 |
| Predict 3 | 7 | 472 | 692 | 1171 |
| TOTAL | 310 | 1862 | 1128 | 3300 |
| Performance | Value |
| Avg Accuracy | 0.76 |
| F1 Score | 0.64 |
| F1 Score (test data) | Not submitted |
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 table | Is 1 | Is 2 | Is 3 | TOTAL |
| Predict 1 | 31 | 21 | 3 | 55 |
| Predict 2 | 273 | 1667 | 932 | 2872 |
| Predict 3 | 6 | 174 | 193 | 373 |
| TOTAL | 310 | 1862 | 1128 | 3300 |
| Performance | Value |
| Avg Accuracy | 0.72 |
| F1 Score | 0.57 |
| F1 Score (test data) | 0.5644 |
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.
Leave a Reply