Artificial Intelligence Applications in Engineering (MUH-920), week 8 of 14

Classification and Evaluation: Defects, Failures and Grades

Prof. Dr. Utku Kose, Süleyman Demirel University

Classification problems in engineering

A classifier assigns an input to one of several classes. Quality inspection separates good from defective parts, condition monitoring distinguishes fault modes, food engineering grades products, and geology assigns facies to depth intervals of a well. A classifier divides the input space into regions separated by decision boundaries. Most modern classifiers do more than name a class: They estimate a probability or a score for each class, and a decision rule, usually a threshold, turns the score into an action. Separating the model from the decision is essential in engineering, because the costs of different mistakes are rarely equal.

Formulas: Scores, probabilities and decisions

A probabilistic classifier estimates for an input xx the probability p^(x)=P^(y=1∣x)\hat{p}(x) = \hat{P}(y = 1 \mid x) of the positive class. A threshold tt turns it into a decision:

y^=1 if p^(x)≥t,    y^=0 otherwise\hat{y} = 1 \text{ if } \hat{p}(x) \ge t, \;\; \hat{y} = 0 \text{ otherwise}

The decision boundary is the set of inputs at which the decision changes, {x:p^(x)=t}\{x : \hat{p}(x) = t\}. With KK classes, the classifier estimates p^k(x)\hat{p}_k(x) for every class kk, and the usual rule picks the class with the largest probability, y^=arg⁡max⁡kp^k(x)\hat{y} = \arg\max_k \hat{p}_k(x). The model supplies p^\hat{p}; the threshold belongs to the decision and is set from the costs of the errors, as the section on curves and costs shows.

Models for classification

Logistic regression models the log-odds of a class as a linear function of the inputs, so its decision boundary is a straight line or a plane, and its coefficients show the direction in which each input pushes the decision [1]. The k-nearest-neighbour classifier takes a vote among the most similar training examples [2]. Support vector machines find the boundary with the largest margin between classes and use kernels to draw curved boundaries in the original space [3]. Decision trees ask a sequence of threshold questions and are easy to read [4, 5]; random forests and gradient boosting combine many trees and are among the strongest methods for tabular engineering data [6, 7].

Animation: Decision boundaries of three classifiers

The same two-class data are separated by logistic regression, k nearest neighbours and a decision tree. Change k and the depth and watch the boundary move between too rigid and too wiggly.

Logistic regression
k nearest neighbours
Decision tree

Check your understanding. Which classifier draws a linear decision boundary in the original input space?

Formulas: Logistic regression, impurity and the margin of a support vector machine

Logistic regression passes a linear score through the logistic function, which maps any real number to a probability [1]:

z=β0+β1x1+⋯+βpxp,    p^=σ(z)=11+e−zz = \beta_0 + \beta_1 x_1 + \cdots + \beta_p x_p, \;\; \hat{p} = \sigma(z) = \frac{1}{1 + e^{-z}}

Solving for zz shows that the score is the logarithm of the odds, z=ln⁡(p^/(1−p^))z = \ln(\hat{p}/(1 - \hat{p})), so a coefficient βj\beta_j multiplies the odds by eβje^{\beta_j} for every unit of xjx_j. The coefficients minimise the log-loss, also called the cross-entropy, of the training examples, whose gradient has a simple form:

L=−[ yln⁡p^+(1−y)ln⁡(1−p^) ],    ∂L∂βj=(p^−y) xjL = -[\,y \ln \hat{p} + (1 - y) \ln(1 - \hat{p})\,], \;\; \frac{\partial L}{\partial \beta_j} = (\hat{p} - y)\, x_j

with x0=1x_0 = 1 for the intercept. Gradient descent repeats βj←βj−η ∂L/∂βj\beta_j \leftarrow \beta_j - \eta\, \partial L/\partial \beta_j with a learning rate η\eta.

Decision trees choose the split that makes the children purest. With the class shares pkp_k in a node, the Gini impurity and the entropy are [4, 5]

G=1−∑kpk2,    H=−∑kpklog⁡2pkG = 1 - \sum_{k} p_k^2, \;\; H = -\sum_{k} p_k \log_2 p_k

and a split into children with nLn_L and nRn_R of the nn examples is scored by the weighted impurity of the children; for the entropy, the decrease is the information gain:

IG=Hparent−nLnHL−nRnHR\text{IG} = H_{\text{parent}} - \frac{n_L}{n} H_L - \frac{n_R}{n} H_R

Support vector machines separate two classes, coded yi=±1y_i = \pm 1, by the hyperplane w⋅x+b=0w \cdot x + b = 0 with the widest margin. Requiring yi(w⋅xi+b)≥1y_i (w \cdot x_i + b) \ge 1 for every training example, the margin between the two classes is 2/∥w∥2/\lVert w \rVert, so the machine minimises ∥w∥2/2\lVert w \rVert^2/2 under these constraints [3]. Kernels such as the radial basis function K(x,x′)=e−γ∥x−x′∥2K(x, x') = e^{-\gamma \lVert x - x' \rVert^2} replace dot products and give curved boundaries. The nearest-neighbour classifier of Week 7 takes a majority vote among the kk closest training examples [2].

Worked example: A logistic model of machine failure

A failure model uses two standardised inputs, torque x1x_1 and tool wear x2x_2, with β0=−3\beta_0 = -3, β1=1.5\beta_1 = 1.5 and β2=1\beta_2 = 1. A machine has x1=1.2x_1 = 1.2 and x2=0.8x_2 = 0.8.

Step 1. Score and probability.

z=−3+1.5×1.2+1×0.8=−3+1.8+0.8=−0.4z = -3 + 1.5 \times 1.2 + 1 \times 0.8 = -3 + 1.8 + 0.8 = −0.4
p^=11+e0.4=11+1.491825=0.401312\hat{p} = \frac{1}{1 + e^{0.4}} = \frac{1}{1 + 1.491825} = 0.401312

Step 2. Odds. One standard deviation more torque multiplies the odds of failure by e1.5=4.4817e^{1.5} = 4.4817.

Step 3. Loss. If the machine failed (y=1y = 1), L=−ln⁡0.401312=0.913016L = -\ln 0.401312 = 0.913016; if it did not (y=0y = 0), L=−ln⁡(1−0.401312)=0.513015L = -\ln(1 - 0.401312) = 0.513015.

Step 4. One gradient step. The machine did fail. With p^−y=0.401312−1=−0.598688\hat{p} - y = 0.401312 - 1 = −0.598688 the gradient is

−0.598688×(1,1.2,0.8)=(−0.598688,−0.718426,−0.47895)−0.598688 \times (1, 1.2, 0.8) = (−0.598688, −0.718426, −0.47895)

and with η=0.5\eta = 0.5:

β0=−3−0.5×(−0.598688)=−2.700656\beta_0 = -3 - 0.5 \times (−0.598688) = −2.700656
β1=1.5−0.5×(−0.718426)=1.859213,    β2=1−0.5×(−0.47895)=1.239475\beta_1 = 1.5 - 0.5 \times (−0.718426) = 1.859213, \;\; \beta_2 = 1 - 0.5 \times (−0.47895) = 1.239475

Step 5. The effect. The new score is −2.700656+1.859213×1.2+1.239475×0.8=−2.700656+2.231056+0.99158=0.52198−2.700656 + 1.859213 \times 1.2 + 1.239475 \times 0.8 = −2.700656 + 2.231056 + 0.99158 = 0.52198, so p^=1/(1+e−0.52198)=0.62761\hat{p} = 1/(1 + e^{−0.52198}) = 0.62761 and the loss falls from 0.913016 to −ln⁡0.62761=0.465836-\ln 0.62761 = 0.465836. Part A of the interactive lab repeats such steps over a whole data set.

Worked example: Gini impurity and information gain of a split

A node holds 40 machines, 10 of which failed. A split at a torque of 50 Nm sends 22 machines with 2 failures to the left and 18 machines with 8 failures to the right.

Step 1. The parent. With p=10/40=0.25p = 10/40 = 0.25:

G=1−(0.252+0.752)=1−(0.0625+0.5625)=0.375G = 1 - (0.25^2 + 0.75^2) = 1 - (0.0625 + 0.5625) = 0.375
H=−(0.25log⁡20.25+0.75log⁡20.75)=−(0.25×(−2)+0.75×(−0.415037))=0.811278H = -(0.25 \log_2 0.25 + 0.75 \log_2 0.75) = -(0.25 \times (−2) + 0.75 \times (−0.415037)) = 0.811278

Step 2. The children. Left, with p=2/22=0.090909p = 2/22 = 0.090909 and 1−p=0.9090911 - p = 0.909091; right, with p=8/18=0.444444p = 8/18 = 0.444444 and 1−p=0.5555561 - p = 0.555556:

GL=1−(0.0909092+0.9090912)=0.165289,    GR=1−(0.4444442+0.5555562)=0.493827G_L = 1 - (0.090909^2 + 0.909091^2) = 0.165289, \;\; G_R = 1 - (0.444444^2 + 0.555556^2) = 0.493827
HL=−(0.090909×(−3.459432)+0.909091×(−0.137504))=0.439497H_L = -(0.090909 \times (−3.459432) + 0.909091 \times (−0.137504)) = 0.439497
HR=−(0.444444×(−1.169925)+0.555556×(−0.847997))=0.991076H_R = -(0.444444 \times (−1.169925) + 0.555556 \times (−0.847997)) = 0.991076

Step 3. Weighted impurity and gain.

Gsplit=2240×0.165289+1840×0.493827=0.313131,    ΔG=0.375−0.313131=0.061869G_{\text{split}} = \frac{22}{40} \times 0.165289 + \frac{18}{40} \times 0.493827 = 0.313131, \;\; \Delta G = 0.375 - 0.313131 = 0.061869
Hsplit=0.55×0.439497+0.45×0.991076=0.687708,    IG=0.811278−0.687708=0.12357H_{\text{split}} = 0.55 \times 0.439497 + 0.45 \times 0.991076 = 0.687708, \;\; \text{IG} = 0.811278 - 0.687708 = 0.12357

The tree compares these numbers over all candidate thresholds and features and keeps the best split. Part B of the interactive lab runs this search.

Worked example: The margin of a separating line

Two positive examples, (3,3)(3, 3) and (4,2)(4, 2), and two negative ones, (1,1)(1, 1) and (2,0)(2, 0), are separated by w=(0.5,0.5)w = (0.5, 0.5) and b=−2b = -2.

Step 1. The constraints. For the positives and the negatives:

0.5×3+0.5×3−2=1,    0.5×4+0.5×2−2=10.5 \times 3 + 0.5 \times 3 - 2 = 1, \;\; 0.5 \times 4 + 0.5 \times 2 - 2 = 1
0.5×1+0.5×1−2=−1,    0.5×2+0.5×0−2=−10.5 \times 1 + 0.5 \times 1 - 2 = -1, \;\; 0.5 \times 2 + 0.5 \times 0 - 2 = -1

Every product yi(w⋅xi+b)y_i(w \cdot x_i + b) equals 1, so all four points lie on the edges of the margin and are support vectors.

Step 2. The margin. ∥w∥=0.52+0.52=0.5=0.707107\lVert w \rVert = \sqrt{0.5^2 + 0.5^2} = \sqrt{0.5} = 0.707107, so the margin is 2/0.707107=2.8284272/0.707107 = 2.828427, the distance between the lines x1+x2=6x_1 + x_2 = 6 and x1+x2=2x_1 + x_2 = 2.

Step 3. A kernel value. With γ=0.5\gamma = 0.5, the points (3,3)(3, 3) and (1,1)(1, 1) have ∥x−x′∥2=22+22=8\lVert x - x' \rVert^2 = 2^2 + 2^2 = 8 and K=e−0.5×8=e−4=0.018316K = e^{-0.5 \times 8} = e^{-4} = 0.018316: Distant points barely interact.

The confusion matrix and its metrics

For a binary problem with a positive class, such as "failure", the confusion matrix counts true positives (TP), false positives (FP), false negatives (FN) and true negatives (TN). Accuracy, the share of correct decisions, is misleading when classes are imbalanced: If 3 percent of machines fail, a model that always predicts "no failure" is 97 percent accurate and useless. Precision, TP / (TP + FP), is the share of alarms that are real. Recall, TP / (TP + FN), is the share of real failures that are caught. The F1 score is their harmonic mean. For several classes, the metrics are computed per class and averaged, either equally for every class (macro average) or weighted by class size (weighted average); the macro average reveals poor performance on small classes.

Check your understanding. A model raises 50 alarms, of which 40 are real failures, and misses 10 failures. What are its precision and recall?

Formulas: Metrics of the confusion matrix

With the counts TP, FP, FN and TN of a binary classifier [8]:

accuracy=TP+TNTP+FP+FN+TN,    precision=TPTP+FP,    recall=TPTP+FN\text{accuracy} = \frac{\text{TP} + \text{TN}}{\text{TP} + \text{FP} + \text{FN} + \text{TN}}, \;\; \text{precision} = \frac{\text{TP}}{\text{TP} + \text{FP}}, \;\; \text{recall} = \frac{\text{TP}}{\text{TP} + \text{FN}}
specificity=TNTN+FP,    F1=2×precision×recallprecision+recall\text{specificity} = \frac{\text{TN}}{\text{TN} + \text{FP}}, \;\; F_1 = \frac{2 \times \text{precision} \times \text{recall}}{\text{precision} + \text{recall}}

Recall is also called the true positive rate and specificity the true negative rate. F1F_1 is the harmonic mean of precision and recall, which stays low unless both are high. With KK classes, each class kk is in turn treated as positive, and its scores are averaged either equally or weighted by the numbers nkn_k of examples of each class:

F1macro=1K∑k=1KF1,k,    F1weighted=1n∑k=1KnkF1,kF_1^{\text{macro}} = \frac{1}{K} \sum_{k=1}^{K} F_{1,k}, \;\; F_1^{\text{weighted}} = \frac{1}{n} \sum_{k=1}^{K} n_k F_{1,k}

Worked example: Alarms for 1000 machines

A model raised 50 alarms for 1000 machines; 40 of them were real failures, and 10 failures were missed. So TP = 40, FP = 10, FN = 10 and TN = 1000 − 40 − 10 − 10 = 940.

Step 1. The metrics.

accuracy=40+9401000=0.98,    precision=4040+10=0.8,    recall=4040+10=0.8\text{accuracy} = \frac{40 + 940}{1000} = 0.98, \;\; \text{precision} = \frac{40}{40 + 10} = 0.8, \;\; \text{recall} = \frac{40}{40 + 10} = 0.8
specificity=940940+10=0.989474,    F1=2×0.8×0.80.8+0.8=0.8\text{specificity} = \frac{940}{940 + 10} = 0.989474, \;\; F_1 = \frac{2 \times 0.8 \times 0.8}{0.8 + 0.8} = 0.8

Step 2. A useless model. A model that never raises an alarm has TP = 0, FN = 50, FP = 0 and TN = 950: Its accuracy is 950/1000=0.95950/1000 = 0.95, while its recall is 0/50=00/50 = 0. Accuracy hides the rare class.

Worked example: Three classes, macro and weighted averages

A classifier of 100 machine states gives the following confusion matrix, with the actual classes in the rows and the predicted classes in the columns:

Actual \ predicted normal wear overheat Total
normal 45 3 2 50
wear 5 22 3 30
overheat 2 3 15 20

The column totals are 52, 28 and 20.

Class normal. Precision 45/52=0.86538545/52 = 0.865385, recall 45/50=0.945/50 = 0.9, F1=2×0.865385×0.9/(0.865385+0.9)=0.882353F_1 = 2 \times 0.865385 \times 0.9/(0.865385 + 0.9) = 0.882353.

Class wear. Precision 22/28=0.78571422/28 = 0.785714, recall 22/30=0.73333322/30 = 0.733333, F1=2×0.785714×0.733333/(0.785714+0.733333)=0.75862F_1 = 2 \times 0.785714 \times 0.733333/(0.785714 + 0.733333) = 0.75862.

Class overheat. Precision 15/20=0.7515/20 = 0.75, recall 15/20=0.7515/20 = 0.75, F1=2×0.75×0.75/(0.75+0.75)=0.75F_1 = 2 \times 0.75 \times 0.75/(0.75 + 0.75) = 0.75.

Averages. The accuracy is (45+22+15)/100=0.82(45 + 22 + 15)/100 = 0.82. The macro average is (0.882353+0.75862+0.75)/3=0.796991(0.882353 + 0.75862 + 0.75)/3 = 0.796991, and the weighted average is (50×0.882353+30×0.75862+20×0.75)/100=(44.11765+22.7586+15)/100=0.818762(50 \times 0.882353 + 30 \times 0.75862 + 20 \times 0.75)/100 = (44.11765 + 22.7586 + 15)/100 = 0.818762. The macro average is lower, because the two small classes are recognised worse than the large one. Part C of the interactive lab asks for such metrics in engineering scenarios and checks each answer.

Curves, thresholds and costs

Varying the threshold traces curves. The ROC curve plots the true positive rate against the false positive rate, and the area under it (AUC) equals the probability that a random positive example receives a higher score than a random negative one [8]. When positives are rare, the precision-recall curve is more informative, because the false positive rate can look small while alarms are still mostly false [9].

The threshold should come from the application. If a missed bearing failure costs twenty times more than an unnecessary inspection, the expected cost for each threshold on validation data can be computed, and the threshold with the lowest cost chosen. The default of 0.5 is only correct when both errors cost the same and the probabilities are well calibrated.

Check your understanding. Positives are 1 percent of the data. Which curve is recommended to compare classifiers?

Formulas: ROC curve, AUC and the cost of a threshold

Lowering the threshold tt from above the largest score to below the smallest moves a classifier through all its operating points. At each threshold, the true positive rate and the false positive rate are [8]

TPR(t)=TP(t)nP,    FPR(t)=FP(t)nN\text{TPR}(t) = \frac{\text{TP}(t)}{n_{\text{P}}}, \;\; \text{FPR}(t) = \frac{\text{FP}(t)}{n_{\text{N}}}

where nPn_{\text{P}} and nNn_{\text{N}} are the numbers of positive and negative examples. The ROC curve joins the points (FPR,TPR)(\text{FPR}, \text{TPR}), and the area under it follows from the trapezoid rule over consecutive points:

AUC=∑i(FPRi+1−FPRi) TPRi+TPRi+12\text{AUC} = \sum_{i} (\text{FPR}_{i+1} - \text{FPR}_i)\, \frac{\text{TPR}_i + \text{TPR}_{i+1}}{2}

The same number is the share of positive and negative pairs in which the positive example has the higher score, with ties counted as one half:

AUC=1nPnN∑i∑j([si>sj]+12[si=sj])\text{AUC} = \frac{1}{n_{\text{P}} n_{\text{N}}} \sum_{i} \sum_{j} \left( [s_i > s_j] + \tfrac{1}{2} [s_i = s_j] \right)

The first sum runs over the positive examples ii, the second over the negative examples jj, and the bracket […][\ldots] is 1 when the condition holds and 0 otherwise. The precision-recall curve plots precision against recall instead, which shows the share of false alarms directly when positives are rare [9]. With costs cFNc_{\text{FN}} for a missed positive and cFPc_{\text{FP}} for a false alarm, the cost of a threshold on validation data is C(t)=cFNFN(t)+cFPFP(t)C(t) = c_{\text{FN}} \text{FN}(t) + c_{\text{FP}} \text{FP}(t). For a calibrated probability pp, raising an alarm costs (1−p)cFP(1 - p) c_{\text{FP}} on average and staying silent costs p cFNp\, c_{\text{FN}}, so an alarm pays off when

p≥t⋆=cFPcFP+cFNp \ge t^{\star} = \frac{c_{\text{FP}}}{c_{\text{FP}} + c_{\text{FN}}}

Worked example: A ROC curve from ten scores

Ten validation cases have the scores and labels (P for failure, N for normal) in the table, sorted by score; nP=4n_{\text{P}} = 4 and nN=6n_{\text{N}} = 6. Each row lowers the threshold to the score of that case:

Threshold Label TP FP TPR FPR Precision
0.95 P 1 0 1/4 = 0.25 0/6 = 0 1/1 = 1
0.85 P 2 0 2/4 = 0.5 0/6 = 0 2/2 = 1
0.8 N 2 1 2/4 = 0.5 1/6 = 0.1667 2/3 = 0.6667
0.7 P 3 1 3/4 = 0.75 1/6 = 0.1667 3/4 = 0.75
0.6 N 3 2 3/4 = 0.75 2/6 = 0.3333 3/5 = 0.6
0.5 N 3 3 3/4 = 0.75 3/6 = 0.5 3/6 = 0.5
0.45 P 4 3 4/4 = 1 3/6 = 0.5 4/7 = 0.5714
0.3 N 4 4 4/4 = 1 4/6 = 0.6667 4/8 = 0.5
0.2 N 4 5 4/4 = 1 5/6 = 0.8333 4/9 = 0.4444
0.1 N 4 6 4/4 = 1 6/6 = 1 4/10 = 0.4

Step 1. Trapezoids. Only the steps in which FPR grows add area, and each of them widens FPR by 1/61/6. The first lies at TPR = 0.5, the next two at 0.75 and the last three at 1:

16×0.5+0.52=112,    16×0.75+0.752=18,    16×1+12=16\frac{1}{6} \times \frac{0.5 + 0.5}{2} = \frac{1}{12}, \;\; \frac{1}{6} \times \frac{0.75 + 0.75}{2} = \frac{1}{8}, \;\; \frac{1}{6} \times \frac{1 + 1}{2} = \frac{1}{6}
AUC=112+2×18+3×16=224+624+1224=2024=0.833333\text{AUC} = \frac{1}{12} + 2 \times \frac{1}{8} + 3 \times \frac{1}{6} = \frac{2}{24} + \frac{6}{24} + \frac{12}{24} = \frac{20}{24} = 0.833333

Step 2. Pairs. There are 4×6=244 \times 6 = 24 pairs. The failure with score 0.95 outranks all 6 normal cases, the one with 0.85 also 6, the one with 0.70 outranks 5 (all but 0.80), and the one with 0.45 outranks 3. Hence AUC=(6+6+5+3)/24=20/24=0.833333\text{AUC} = (6 + 6 + 5 + 3)/24 = 20/24 = 0.833333, the same value.

Step 3. Costs. A missed failure costs cFN=20c_{\text{FN}} = 20 and a false alarm cFP=1c_{\text{FP}} = 1. At t=0.5t = 0.5 one failure is missed and three false alarms are raised: C=20×1+1×3=23C = 20 \times 1 + 1 \times 3 = 23. At t=0.45t = 0.45 no failure is missed and three false alarms remain: C=0+3=3C = 0 + 3 = 3. At t=0.1t = 0.1 every case is an alarm: C=0+6=6C = 0 + 6 = 6. The cheapest threshold here is 0.45. For calibrated probabilities the formula gives t⋆=1/(1+20)=0.047619t^{\star} = 1/(1 + 20) = 0.047619, far below the default of 0.5. Part D of the interactive lab builds the table, the curve and the costs for any scores, and Part E moves a threshold over generated scores and shows how the confusion matrix, the metrics and the expected cost follow.

Imbalanced data

Rare events are the rule in engineering: failures, defects, accidents, rock bursts. Several remedies exist. Stratified splits keep the class proportions equal in training and test data. Class weights make errors on the rare class more expensive during training. Resampling duplicates rare examples or removes common ones, and SMOTE creates synthetic minority examples by interpolating between neighbouring minority samples [10]. None of these remedies creates information; the most effective step is often better features, more minority data or a problem definition that matches the decision.

Precision-recall curves of three classifiers for rare machine failures on the synthetic stand-in of the AI4I data. The dashed line marks the share of failures, the precision of random guessing.
Figure 8.1. Precision-recall curves of three classifiers for rare machine failures on the synthetic stand-in of the AI4I data. The dashed line marks the share of failures, the precision of random guessing.

Formulas: Class weights, stratification and SMOTE

With nn examples, KK classes and nkn_k examples of class kk, balanced class weights make every class contribute the same total weight to the loss:

wk=nK nkw_k = \frac{n}{K\, n_k}

A stratified split keeps the class shares: A test share qq receives about q nkq\, n_k examples of every class. SMOTE creates a synthetic minority example on the segment between a minority example xix_i and one of its kk nearest minority neighbours xnnx_{\text{nn}}, with a random number λ\lambda between 0 and 1 [10]:

xnew=xi+λ (xnn−xi)x_{\text{new}} = x_i + \lambda\, (x_{\text{nn}} - x_i)

Worked example: Rare failures in a milling data set

The AI4I 2020 data describe 10000 machining runs, of which 339 ended in a failure [11].

Step 1. Class weights. wfailure=10000/(2×339)=10000/678=14.749263w_{\text{failure}} = 10000/(2 \times 339) = 10000/678 = 14.749263 and wnormal=10000/(2×9661)=10000/19322=0.517545w_{\text{normal}} = 10000/(2 \times 9661) = 10000/19322 = 0.517545. Each failure counts about 14.749263/0.517545=28.514.749263/0.517545 = 28.5 times as much as a normal run, and both classes reach the total weight 339×14.749263=9661×0.517545=5000339 \times 14.749263 = 9661 \times 0.517545 = 5000.

Step 2. Stratified split. A test share of 20 percent receives 0.2×339=67.80.2 \times 339 = 67.8, that is about 68 failures, among 2000 runs; an unstratified random split could by chance give noticeably fewer.

Step 3. One synthetic failure. A failure with torque 62 Nm and tool wear 210 min has a neighbouring failure at 58 Nm and 190 min. With λ=0.4\lambda = 0.4:

xnew=(62,210)+0.4×(58−62,190−210)=(62,210)+(−1.6,−8)=(60.4,202)x_{\text{new}} = (62, 210) + 0.4 \times (58 - 62, 190 - 210) = (62, 210) + (-1.6, -8) = (60.4, 202)

The new point lies between two real failures and adds no new information about where failures occur; it only changes how strongly the existing ones pull the model. Part F of the interactive lab generates such points.

Explaining classifiers

Engineers need to know why a model decides. Permutation importance measures how much a performance metric drops when the values of one feature are shuffled, which breaks its link to the target. SHAP values distribute a single prediction among the features according to a game-theoretic rule and are widely used for tree ensembles [12]. In predictive maintenance, explanations connect model output to physical mechanisms such as heat dissipation or tool wear [13]. Newer methods study the geometry of a model's decision function, for example GEMEX, which the instructor developed as an open Python package [14]. Explanations are models of models and can mislead, so they should be checked against domain knowledge and, where possible, against deliberately constructed test cases.

Check your understanding. Shuffling the values of "torque" in the test data lowers the F1 score of a failure classifier from 0.82 to 0.41. What does this indicate?

Formulas: Permutation importance and Shapley values

Permutation importance compares the score ss of a model on intact data with the scores sk,js_{k,j} obtained after the values of feature jj have been shuffled, in KK repetitions:

Ij=s−1K∑k=1Ksk,jI_j = s - \frac{1}{K} \sum_{k=1}^{K} s_{k,j}

Shapley values divide one prediction among the features. If v(S)v(S) is the prediction when only the features in the set SS are known, feature jj receives the weighted average of its marginal contributions, where the sum runs over all sets SS of the other features, including the empty set, among MM features [12]:

ϕj=∑S∣S∣! (M−∣S∣−1)!M!(v(S∪{j})−v(S))\phi_j = \sum_{S} \frac{|S|!\, (M - |S| - 1)!}{M!} \big( v(S \cup \{j\}) - v(S) \big)

The exclamation mark denotes the factorial of Week 2. The values add up exactly to the difference between the prediction and the baseline, ∑jϕj=v(F)−v({})\sum_j \phi_j = v(F) - v(\{\}), which is why they are called additive explanations.

Worked example: Permutation importance of torque

A failure classifier reaches F1=0.82F_1 = 0.82. Shuffling torque three times gives 0.41, 0.45 and 0.39, with the mean (0.41+0.45+0.39)/3=0.4167(0.41 + 0.45 + 0.39)/3 = 0.4167, so the importance is 0.82−0.4167=0.40330.82 - 0.4167 = 0.4033. Without the information in torque, the model loses about half of its F1F_1, which agrees with the physics of tool failure under high load [13].

Worked example: Shapley values for three features

A model predicts the failure probability of one machine from torque (T), tool wear (W) and air temperature (A). The table gives the prediction v(S)v(S) when only the features in SS are known and the others are averaged out:

Known features SS v(S)v(S)
{}\{\} 0.1
{T}\{\text{T}\} 0.4
{W}\{\text{W}\} 0.25
{A}\{\text{A}\} 0.12
{T,W}\{\text{T}, \text{W}\} 0.8
{T,A}\{\text{T}, \text{A}\} 0.45
{W,A}\{\text{W}, \text{A}\} 0.28
{T,W,A}\{\text{T}, \text{W}, \text{A}\} 0.85

With M=3M = 3, the weights are 0! 2!3!=13\frac{0!\, 2!}{3!} = \frac{1}{3} for the empty set, 1! 1!3!=16\frac{1!\, 1!}{3!} = \frac{1}{6} for sets of one feature and 2! 0!3!=13\frac{2!\, 0!}{3!} = \frac{1}{3} for sets of two features.

Step 1. Torque. The marginal contributions of T are v({T})−v({})=0.4−0.1=0.3v(\{T\}) - v(\{\}) = 0.4 - 0.1 = 0.3, v({T,W})−v({W})=0.8−0.25=0.55v(\{T, W\}) - v(\{W\}) = 0.8 - 0.25 = 0.55, v({T,A})−v({A})=0.45−0.12=0.33v(\{T, A\}) - v(\{A\}) = 0.45 - 0.12 = 0.33 and v({T,W,A})−v({W,A})=0.85−0.28=0.57v(\{T, W, A\}) - v(\{W, A\}) = 0.85 - 0.28 = 0.57:

ϕT=13×0.3+16×0.55+16×0.33+13×0.57\phi_{\text{T}} = \tfrac{1}{3} \times 0.3 + \tfrac{1}{6} \times 0.55 + \tfrac{1}{6} \times 0.33 + \tfrac{1}{3} \times 0.57
ϕT=2×0.3+0.55+0.33+2×0.576=2.626=0.436667\phi_{\text{T}} = \frac{2 \times 0.3 + 0.55 + 0.33 + 2 \times 0.57}{6} = \frac{2.62}{6} = 0.436667

Step 2. Tool wear. The contributions are 0.25−0.1=0.150.25 - 0.1 = 0.15, 0.8−0.4=0.40.8 - 0.4 = 0.4, 0.28−0.12=0.160.28 - 0.12 = 0.16 and 0.85−0.45=0.40.85 - 0.45 = 0.4:

ϕW=13×0.15+16×0.4+16×0.16+13×0.4\phi_{\text{W}} = \tfrac{1}{3} \times 0.15 + \tfrac{1}{6} \times 0.4 + \tfrac{1}{6} \times 0.16 + \tfrac{1}{3} \times 0.4
ϕW=2×0.15+0.4+0.16+2×0.46=1.666=0.276667\phi_{\text{W}} = \frac{2 \times 0.15 + 0.4 + 0.16 + 2 \times 0.4}{6} = \frac{1.66}{6} = 0.276667

Step 3. Air temperature. The contributions are 0.12−0.1=0.020.12 - 0.1 = 0.02, 0.45−0.4=0.050.45 - 0.4 = 0.05, 0.28−0.25=0.030.28 - 0.25 = 0.03 and 0.85−0.8=0.050.85 - 0.8 = 0.05:

ϕA=13×0.02+16×0.05+16×0.03+13×0.05\phi_{\text{A}} = \tfrac{1}{3} \times 0.02 + \tfrac{1}{6} \times 0.05 + \tfrac{1}{6} \times 0.03 + \tfrac{1}{3} \times 0.05
ϕA=2×0.02+0.05+0.03+2×0.056=0.226=0.036667\phi_{\text{A}} = \frac{2 \times 0.02 + 0.05 + 0.03 + 2 \times 0.05}{6} = \frac{0.22}{6} = 0.036667

Step 4. Check. 2.62+1.66+0.226=4.56=0.75=0.85−0.1\frac{2.62 + 1.66 + 0.22}{6} = \frac{4.5}{6} = 0.75 = 0.85 - 0.1, the difference between the prediction and the baseline. Of the raised risk, torque explains 0.436667/0.75=580.436667/0.75 = 58 percent, tool wear 0.276667/0.75=370.276667/0.75 = 37 percent and air temperature 0.036667/0.75=50.036667/0.75 = 5 percent. Part G of the interactive lab computes Shapley values for any table.

Python step 8: Grouping, counting and evaluation functions

The Python step of this week is part of the Colab notebook, where every explanation stands next to a cell that runs it and the step closes with a quick check and exercises with immediate feedback. The printable lecture notes contain the same step together with the outputs of its code. The Colab button below opens the notebook at the step. Run the setup cell just above the step first, since the exercises use its check function.

Open Python step 8 in Colab View the notebook on GitHub

Review cards

Select a card to turn it over.

Precision
TP / (TP + FP): the share of positive predictions that are correct.
Recall
TP / (TP + FN): the share of actual positives that are detected.
Macro average
Average of a per-class metric with equal weight for every class; exposes weak small classes.
ROC AUC
Probability that a random positive receives a higher score than a random negative [8].
Precision-recall curve
Preferred for rare positives, where the false positive rate hides many false alarms [9].
Permutation importance
Drop in a metric when one feature's values are shuffled; shows reliance, not causation.

Continue the week

The week continues with the simulation and the self-assessment of the interactive lab and with the Python step and the hands-on work of the Colab notebook. The week overview lists the discipline challenges, the weekly task and the research assignment.

Interactive lab Colab notebook Self-assessment Week overview and tasks

References

[1] Hastie, T., Tibshirani, R., & Friedman, J. (2009). The Elements of Statistical Learning: Data Mining, Inference, and Prediction (2nd ed.). Springer. https://doi.org/10.1007/978-0-387-84858-7

[2] Cover, T., & Hart, P. (1967). Nearest neighbor pattern classification. IEEE Transactions on Information Theory, 13(1), 21-27. https://doi.org/10.1109/TIT.1967.1053964

[3] Cortes, C., & Vapnik, V. (1995). Support-vector networks. Machine Learning, 20(3), 273-297. https://doi.org/10.1007/BF00994018

[4] Breiman, L., Friedman, J. H., Olshen, R. A., & Stone, C. J. (1984). Classification and Regression Trees. Wadsworth.

[5] Quinlan, J. R. (1986). Induction of decision trees. Machine Learning, 1(1), 81-106. https://doi.org/10.1007/BF00116251

[6] Breiman, L. (2001). Random forests. Machine Learning, 45(1), 5-32. https://doi.org/10.1023/A:1010933404324

[7] Chen, T., & Guestrin, C. (2016). XGBoost: A scalable tree boosting system. In Proceedings of the 22nd ACM SIGKDD International Conference on Knowledge Discovery and Data Mining (pp. 785-794). https://doi.org/10.1145/2939672.2939785

[8] Fawcett, T. (2006). An introduction to ROC analysis. Pattern Recognition Letters, 27(8), 861-874. https://doi.org/10.1016/j.patrec.2005.10.010

[9] Saito, T., & Rehmsmeier, M. (2015). The precision-recall plot is more informative than the ROC plot when evaluating binary classifiers on imbalanced datasets. PLOS ONE, 10(3), e0118432. https://doi.org/10.1371/journal.pone.0118432

[10] Chawla, N. V., Bowyer, K. W., Hall, L. O., & Kegelmeyer, W. P. (2002). SMOTE: Synthetic minority over-sampling technique. Journal of Artificial Intelligence Research, 16, 321-357. https://doi.org/10.1613/jair.953

[11] Matzka, S. (2020). AI4I 2020 Predictive Maintenance Dataset [Dataset]. UCI Machine Learning Repository, ID 601. https://doi.org/10.24432/C5HS5C

[12] Lundberg, S. M., & Lee, S.-I. (2017). A unified approach to interpreting model predictions. In Advances in Neural Information Processing Systems 30 (pp. 4765-4774). https://arxiv.org/abs/1705.07874

[13] Matzka, S. (2020). Explainable artificial intelligence for predictive maintenance applications. In 2020 Third International Conference on Artificial Intelligence for Industries (AI4I) (pp. 69-74). IEEE. https://doi.org/10.1109/AI4I49448.2020.00023

[14] Kose, U. (2026). GEMEX: Geodesic Entropic Manifold Explainability (Version 1.2.2) [Python package]. Python Package Index. https://pypi.org/project/gemex/