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 the probability of the positive class. A threshold turns it into a decision:
The decision boundary is the set of inputs at which the decision changes, . With classes, the classifier estimates for every class , and the usual rule picks the class with the largest probability, . The model supplies ; 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.
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]:
Solving for shows that the score is the logarithm of the odds, , so a coefficient multiplies the odds by for every unit of . The coefficients minimise the log-loss, also called the cross-entropy, of the training examples, whose gradient has a simple form:
with for the intercept. Gradient descent repeats with a learning rate .
Decision trees choose the split that makes the children purest. With the class shares in a node, the Gini impurity and the entropy are [4, 5]
and a split into children with and of the examples is scored by the weighted impurity of the children; for the entropy, the decrease is the information gain:
Support vector machines separate two classes, coded , by the hyperplane with the widest margin. Requiring for every training example, the margin between the two classes is , so the machine minimises under these constraints [3]. Kernels such as the radial basis function replace dot products and give curved boundaries. The nearest-neighbour classifier of Week 7 takes a majority vote among the closest training examples [2].
Worked example: A logistic model of machine failure
A failure model uses two standardised inputs, torque and tool wear , with , and . A machine has and .
Step 1. Score and probability.
Step 2. Odds. One standard deviation more torque multiplies the odds of failure by .
Step 3. Loss. If the machine failed (), ; if it did not (), .
Step 4. One gradient step. The machine did fail. With the gradient is
and with :
Step 5. The effect. The new score is , so and the loss falls from 0.913016 to . 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 :
Step 2. The children. Left, with and ; right, with and :
Step 3. Weighted impurity and gain.
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, and , and two negative ones, and , are separated by and .
Step 1. The constraints. For the positives and the negatives:
Every product equals 1, so all four points lie on the edges of the margin and are support vectors.
Step 2. The margin. , so the margin is , the distance between the lines and .
Step 3. A kernel value. With , the points and have and : 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]:
Recall is also called the true positive rate and specificity the true negative rate. is the harmonic mean of precision and recall, which stays low unless both are high. With classes, each class is in turn treated as positive, and its scores are averaged either equally or weighted by the numbers of examples of each class:
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.
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 , while its recall is . 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 , recall , .
Class wear. Precision , recall , .
Class overheat. Precision , recall , .
Averages. The accuracy is . The macro average is , and the weighted average is . 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 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]
where and are the numbers of positive and negative examples. The ROC curve joins the points , and the area under it follows from the trapezoid rule over consecutive points:
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:
The first sum runs over the positive examples , the second over the negative examples , and the bracket 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 for a missed positive and for a false alarm, the cost of a threshold on validation data is . For a calibrated probability , raising an alarm costs on average and staying silent costs , so an alarm pays off when
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; and . 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 . The first lies at TPR = 0.5, the next two at 0.75 and the last three at 1:
Step 2. Pairs. There are 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 , the same value.
Step 3. Costs. A missed failure costs and a false alarm . At one failure is missed and three false alarms are raised: . At no failure is missed and three false alarms remain: . At every case is an alarm: . The cheapest threshold here is 0.45. For calibrated probabilities the formula gives , 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.

Formulas: Class weights, stratification and SMOTE
With examples, classes and examples of class , balanced class weights make every class contribute the same total weight to the loss:
A stratified split keeps the class shares: A test share receives about examples of every class. SMOTE creates a synthetic minority example on the segment between a minority example and one of its nearest minority neighbours , with a random number between 0 and 1 [10]:
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. and . Each failure counts about times as much as a normal run, and both classes reach the total weight .
Step 2. Stratified split. A test share of 20 percent receives , 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 :
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 of a model on intact data with the scores obtained after the values of feature have been shuffled, in repetitions:
Shapley values divide one prediction among the features. If is the prediction when only the features in the set are known, feature receives the weighted average of its marginal contributions, where the sum runs over all sets of the other features, including the empty set, among features [12]:
The exclamation mark denotes the factorial of Week 2. The values add up exactly to the difference between the prediction and the baseline, , which is why they are called additive explanations.
Worked example: Permutation importance of torque
A failure classifier reaches . Shuffling torque three times gives 0.41, 0.45 and 0.39, with the mean , so the importance is . Without the information in torque, the model loses about half of its , 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 when only the features in are known and the others are averaged out:
| Known features | |
|---|---|
| 0.1 | |
| 0.4 | |
| 0.25 | |
| 0.12 | |
| 0.8 | |
| 0.45 | |
| 0.28 | |
| 0.85 |
With , the weights are for the empty set, for sets of one feature and for sets of two features.
Step 1. Torque. The marginal contributions of T are , , and :
Step 2. Tool wear. The contributions are , , and :
Step 3. Air temperature. The contributions are , , and :
Step 4. Check. , the difference between the prediction and the baseline. Of the raised risk, torque explains percent, tool wear percent and air temperature 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.
Review cards
Select a card to turn it over.
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/