Category: Easy

  • ML0014 Confusion Matrix

    What is the confusion matrix?

    Answer

    A confusion matrix is a table that summarizes the performance of a classification model by comparing its predicted labels against the actual labels. For binary classification it is a 2×2 table with four cells: true positives (correctly predicted positive), false positives (negative predicted as positive), false negatives (positive predicted as negative), and true negatives (correctly predicted negative). Unlike a single scalar metric, it shows not only how many predictions were wrong but which kinds of errors were made. For multi-class problems, the matrix expands into a larger square table where cell (i, j) counts the instances of actual class i predicted as class j; off-diagonal clusters reveal which specific classes the model systematically confuses, guiding model refinement or relabeling.

    (1) Structure: Rows are actual classes, columns are predicted classes; the diagonal holds all correct predictions.
    (2) Binary Case: The four cells TP/FP/FN/TN feed every classification metric: accuracy, precision, recall, F1.
    (3) Multi-Class Case: An n \times n matrix whose off-diagonal hotspots expose systematic class confusions.

    Actual \ PredictedPredicted PositivePredicted Negative
    Actual PositiveTrue Positives (TP): correct positiveFalse Negatives (FN): missed positive
    Actual NegativeFalse Positives (FP): false alarmTrue Negatives (TN): correct negative

    Mathematical Formulation:
    M_{ij} = \sum_{k=1}^{N} \mathbb{1}\big(y_k = i,\; \hat{y}_k = j\big)
    \text{row-normalized:} \quad \tilde{M}_{ij} = \frac{M_{ij}}{\sum_j M_{ij}}

    Where:

    • M_{ij} is the count in row i, column j of the confusion matrix.
    • y_k is the true label of sample k, \hat{y}_k its predicted label, and k\in\{1,\ldots,N\} indexes the N samples.
    • \mathbb{1}(\cdot) is the indicator function: 1 when the condition holds, 0 otherwise.
    • \tilde{M}_{ij} is the row-normalized version: row i then shows the per-class recall distribution, which reads better under class imbalance.
    Three class confusion matrix heatmap with off-diagonal confusions

    Figure 1: A 3-class example: strong diagonal means healthy classification; the bright off-diagonal cell shows the model systematically confuses class 1 with class 2.


    Login to view more content
  • ML0013 Accuracy

    What is accuracy?

    Answer

    Accuracy is a metric that evaluates a classification model as the ratio of correct predictions to the total number of predictions: if a model classifies 99 of 100 samples correctly, its accuracy is 99%. It is intuitive and widely used because it directly answers “how often is the model right?” The four outcomes behind it are: true positives (positive correctly predicted), true negatives (negative correctly predicted), false positives (negative predicted as positive), and false negatives (positive predicted as negative). The important caveat: accuracy alone can be misleading on imbalanced data: a model that labels every patient “healthy” scores 99% on a dataset with 1% disease prevalence while being clinically useless. In such cases, precision, recall, and the F1 score give a truer picture.

    (1) Definition: Correct predictions divided by all predictions, simple and intuitive.
    (2) When It Works: Roughly balanced classes and symmetric error costs.
    (3) The Accuracy Paradox: On skewed data, a trivial “always predict the majority” model scores high accuracy with zero real skill.

    Grid of 100 samples with 90 majority class showing the trivial 90 percent accuracy paradox

    Figure 1: The accuracy paradox: with 90 negatives and 10 positives, predicting “negative” for everything yields 90% accuracy while catching zero positives.

    Mathematical Formulation:
    \text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}

    Where:

    • TP and TN are the counts of correct positive and negative predictions: the numerator counts everything the model got right.
    • FP and FN are the false alarm and miss counts; the denominator is simply the total number of samples.
    • Because TN dominates on imbalanced data, high accuracy can hide a model that never identifies the minority class at all.

    Login to view more content
  • ML0012 F1 Score

    What is F1 Score?

    Answer

    The F1 score is a metric for classification models that combines precision and recall into a single number, and it is particularly useful when classes are imbalanced. It is the harmonic mean of the two: precision measures how many positive predictions are correct, recall measures how many actual positives are found, and the harmonic mean punishes imbalance between them: a high F1 requires both to be high at once. A model with 99% precision but 10% recall scores poorly on F1, because the harmonic mean is dominated by the smaller of the two values. This makes F1 a stricter, more informative summary than accuracy or either component alone when false positives and false negatives both matter.

    (1) Definition: The harmonic mean of precision and recall, one number summarizing both.
    (2) Key Property: It is dominated by the lower component, so it cannot be gamed by maximizing only precision or only recall.
    (3) When To Use: Imbalanced classes, or when false positives and false negatives carry comparable cost.

    F1 score contour lines over the recall-precision plane

    Figure 1: F1 contour lines over the recall–precision plane: to reach a higher F1 band you must improve both metrics: moving along one axis alone quickly flattens out.

    Mathematical Formulation:
    \text{F1} = \frac{2 \times \text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}
    \text{F1} = \frac{2TP}{2TP + FP + FN}

    Where:

    • \text{Precision} is TP/(TP+FP) and \text{Recall} is TP/(TP+FN) (see the precision-and-recall question).
    • TP, FP, and FN are the true positive, false positive, and false negative counts.
    • The second line is the equivalent counts-only form; TN does not appear, which is why F1 stays meaningful under class imbalance.

    Login to view more content
  • ML0011 Precision and Recall

    What are Precision and Recall?

    Answer

    Precision and recall are two fundamental metrics for evaluating classification models, especially with imbalanced data or asymmetric error costs. Precision (positive predictive value) is the ratio of correctly predicted positives to all predicted positives: it answers “when the model says positive, how often is it right?” If a spam detector flags 100 emails and 99 are truly spam, precision is 99%. Recall (sensitivity, true positive rate) is the ratio of correctly predicted positives to all actual positives: it answers “of all the real positives, how many did the model catch?” If 100 spam emails exist and the model finds 90, recall is 90%. High precision means few false alarms (a spam filter that almost never marks good mail as spam); high recall means few misses (a cancer screening that almost never misses a real case). The two metrics sit on a trade-off controlled by the decision threshold.

    (1) Precision = Accuracy Of Positive Predictions: Minimizing false positives matters when false alarms are costly.
    (2) Recall = Coverage Of Actual Positives: Minimizing false negatives matters when misses are costly.
    (3) Trade-Off: Lowering the classification threshold raises recall and usually lowers precision; raising it does the reverse.

    Set diagram of relevant and retrieved items showing TP, FP, and FN regions

    Figure 1: The set view: precision is the share of the retrieved set that is correct, recall is the share of the relevant set that was retrieved.

    Mathematical Formulation:
    \text{Precision} = \frac{TP}{TP + FP}
    \text{Recall} = \frac{TP}{TP + FN}

    Where:

    • TP (true positives) is the count of positives correctly predicted as positive.
    • FP (false positives) is the count of negatives wrongly predicted as positive, the false alarms.
    • FN (false negatives) is the count of positives wrongly predicted as negative, the misses.
    • TN (true negatives) is the count of negatives correctly predicted as negative; it enters neither formula, which is why these metrics shine on imbalanced data.

    Login to view more content
  • ML0010 Epoch Selection

    What are effective strategies for selecting the appropriate number of training epochs in machine learning?

    Answer

    An epoch is one complete pass through the entire training dataset; choosing the right number of epochs means striking a balance between undertraining and overfitting. The effective strategies are: monitor validation metrics: if validation loss plateaus or starts increasing, further training adds nothing; implement early stopping: halt automatically when performance stops improving and keep the best weights; experiment: begin with a moderate range such as 10–100 epochs and adjust from the training/validation curves; and assess model and data complexity: complex models or datasets may need more epochs to capture the underlying patterns, while simpler problems converge quickly. Related definitions: an iteration is a single parameter update over one batch, so with 1,000 training samples and batch size 100, one epoch consists of 10 iterations.

    (1) What An Epoch Is: One full pass over the training set; N/B iterations per epoch for N samples and batch size B.
    (2) Selection Strategy: Watch validation loss and let early stopping pick the epoch for you.
    (3) Practical Range: Start around 10–100 epochs; scale up with task complexity, down for simple ones.

    Validation loss curve with undertraining, sweet spot, and overfitting zones

    Figure 1: The validation curve answers “how many epochs”: stop in the sweet-spot zone: before it, the model is undertrained; after it, you are overfitting.

    Mathematical Formulation:
    I = \frac{N}{B}
    t^{*} = \arg\min_t \; \mathcal{L}_{\mathrm{val}}(t)

    Where:

    • I is the number of iterations (parameter updates) in one epoch.
    • N is the number of training samples and B is the batch size.
    • t^{*} is the ideal number of epochs, the one early stopping approximates.
    • \mathcal{L}_{\mathrm{val}}(t) is the validation loss after t epochs; its minimum marks the sweet spot in Figure 1.

    Login to view more content
  • ML0009 Batch Size Selection

    What are the best strategies for selecting the appropriate batch size?

    Answer

    Selecting an appropriate batch size is a crucial hyperparameter choice that trades training efficiency against optimization behavior. The practical strategy: start with a moderate value (16, 32, or 64) and adjust based on memory, gradient stability, and validation performance. Three factors drive the choice: memory constraints (larger batches need more GPU memory), dataset size (large datasets can sustain large batches; small datasets often benefit from the extra variability of small batches), and learning rate interaction (large batches usually allow (or require) a proportionally higher learning rate). Large batches train faster per epoch with stabler gradient estimates and higher memory cost, but can converge to sharp minima that generalize worse; small batches update more often with noisier gradients, which can explore flatter minima and generalize better at lower memory cost.

    (1) Trade-Off Summary: Large batch = fast and stable but possibly sharp minima; small batch = slow and noisy but often better generalization.
    (2) Practical Starting Points: Try 16/32/64 first, then scale with memory and dataset size.
    (3) Interaction With LR: Scale the learning rate roughly proportionally when you change the batch size.

    Optimization trajectories of small versus large batch size on a loss surface

    Figure 1: Same loss surface, two batch sizes: the small-batch path is noisy but wanders into a wider basin, while the large-batch path is smooth but direct.

    Mathematical Formulation:
    \hat{g} = \frac{1}{B}\sum_{i=1}^{B}\nabla_\theta \mathcal{L}_i
    \mathrm{Var}[\hat{g}] \propto \frac{\sigma^2}{B}

    Where:

    • \hat{g} is the gradient estimate computed from one mini-batch.
    • B is the batch size, and i\in\{1,\ldots,B\} indexes the samples in the batch.
    • \nabla_\theta \mathcal{L}_i is the per-sample loss gradient.
    • \sigma^2 is the per-sample gradient variance; doubling B halves the gradient noise, which is why large batches give stable estimates and small batches give noisy ones.

    Login to view more content
  • ML0008 Learning Rate Selection

    What are the best practices for selecting an optimal learning rate?

    Answer

    Selecting an appropriate learning rate is one of the most important choices in training a neural network: it largely determines how quickly and how well the model learns. Four practices cover most situations. First, grid or random search over a range (e.g., 0.0001, 0.001, 0.01) while watching training performance, to narrow down an effective value. Second, use adaptive optimizers such as Adam, RMSProp, or Adagrad, which adjust the effective rate per parameter from gradient history and need less manual tuning. Third, apply learning rate schedules (step decay, exponential decay, or cosine annealing) that shrink the rate as training approaches convergence. Fourth, monitor the training loss: if it stops decreasing or oscillates, adjust the rate. Too high a rate overshoots the optimum and oscillates or diverges; too low a rate converges slowly or stalls; the right rate converges efficiently to a good solution.

    (1) Why It Matters: The learning rate scales every update step; it is the single hyperparameter that most often decides whether training works at all.
    (2) Four Practices: Search a range, prefer adaptive optimizers, decay the rate over time, and watch the loss curve.
    (3) Diagnose From The Curve: Oscillating or rising loss means too high; an almost flat, slowly creeping loss means too low.

    Training loss for too low, good, and too high learning rates

    Figure 1: Reading the loss curve: too low creeps down slowly, a good rate drops fast and plateaus, too high oscillates and can diverge.

    Mathematical Formulation:
    \theta_{t+1} = \theta_t - \eta_t \, \nabla_\theta \mathcal{L}(\theta_t)
    \eta_t = \eta_0 \, \gamma^{\lfloor t / s \rfloor} \quad \text{(step decay)}

    Where:

    • \theta_t denotes the model parameters at step t.
    • \eta_t is the learning rate at step t, now time-dependent because of the schedule.
    • \eta_0 is the initial learning rate, \gamma \in (0,1) is the decay factor, and s is the number of steps between decays.
    • \nabla_\theta \mathcal{L}(\theta_t) is the gradient of the training loss \mathcal{L}, scaled by \eta_t at every update.
    Step, exponential, and cosine learning rate decay schedules

    Figure 2: Three standard schedules. All shrink \eta_t over training so the model takes large steps early and fine steps near convergence.


    Login to view more content
  • ML0004 Underfitting

    What is underfitting, how do you recognize it, and how can you fix it?

    Answer

    Underfitting occurs when a model is too simple to capture the underlying patterns in the data, so it performs poorly on both the training data and new, unseen data: it has not even learned the training set. An underfit model exhibits high bias and low variance: its predictions are consistently wrong in the same way, regardless of the particular training sample. Common causes are an overly simple model, inadequate training (stopped too early), over-regularization, and poor feature selection. The fixes mirror the causes: increase model complexity, train longer, reduce regularization, and engineer more informative features.

    (1) Definition: The model lacks the capacity to fit the signal, so error stays high on training and test data alike.
    (2) Recognition: High training error is the key signature; contrast with overfitting, where training error is low and only validation error suffers.
    (3) Fixes: Add capacity (more layers, higher-degree features), train longer, weaken regularization, and improve the feature set.

    Three fits to the same data: underfit line, good low-degree fit, and overfit high-degree curve

    Figure 1: The same data fitted three ways. The underfit line misses the pattern entirely; the good fit follows the trend; the overfit curve chases every noisy point.

    Mathematical Formulation:
    \mathrm{Err}(x_0) = \mathrm{Bias}^2\big(\hat{f}(x_0)\big) + \mathrm{Var}\big(\hat{f}(x_0)\big) + \sigma^2
    \text{underfitting} \;\Rightarrow\; \mathrm{Bias}^2 \text{ dominates the total error}

    Where:

    • \mathrm{Err}(x_0) is the expected prediction error of the model \hat{f} at a new point x_0.
    • \mathrm{Bias}^2 measures how far the average prediction sits from the truth: the term that dominates when a model underfits.
    • \mathrm{Var} measures how much the prediction swings across different training sets: the term that dominates in overfitting.
    • \sigma^2 is the irreducible noise in the data, which no model can remove.

    Login to view more content
  • ML0003 Overfitting

    What is overfitting and how to avoid overfitting?

    Answer

    Overfitting happens when a model learns the training data too well (including its noise and outliers) and as a result performs poorly on new, unseen data. The model becomes too specialized to the training set and fails to generalize. The telltale sign is a growing generalization gap: training loss keeps falling while validation loss turns back up. To avoid overfitting: simplify the model, get more data or use data augmentation, apply regularization (L1/L2), validate frequently with early stopping, and for neural networks use dropout.

    (1) Definition: The model memorizes noise as if it were signal, so training error keeps dropping while test error rises.
    (2) Detection: Watch the train/validation loss gap and use cross-validation: wildly varying performance across folds indicates overfitting.
    (3) Remedies: More or augmented data, L1/L2 regularization, dropout, early stopping, or a smaller model; all reduce effective capacity or expose the model to more variation.

    Training loss keeps decreasing while validation loss turns upward, with an early stopping marker

    Figure 1: The validation loss minimum marks the ideal stopping point; training past it widens the generalization gap; that widening is overfitting.

    Mathematical Formulation:
    \mathcal{L}_{\mathrm{reg}}(\theta) = \mathcal{L}(\theta) + \lambda \lVert \theta \rVert_2^2
    \mathrm{gap} = \mathcal{L}_{\mathrm{val}}(\theta) - \mathcal{L}_{\mathrm{train}}(\theta)

    Where:

    • \mathcal{L}(\theta) is the original training loss over parameters \theta.
    • \lambda is the regularization strength; larger values shrink the weights \theta toward zero, trading training fit for generalization.
    • \lVert \theta \rVert_2^2 is the squared L2 norm of the weights (weight decay); an L1 penalty \lVert \theta \rVert_1 instead drives weights to exactly zero.
    • \mathcal{L}_{\mathrm{val}} and \mathcal{L}_{\mathrm{train}} are validation and training loss; a small, stable gap indicates good generalization.
    Bias-variance tradeoff: total error is U-shaped over model complexity

    Figure 2: Why the remedies work: they move the model left along the complexity axis, out of the high-variance region and back toward the total-error minimum.


    Login to view more content
  • ML0002 Machine Learning Type

    What is the difference between supervised learning and unsupervised learning?

    Answer

    Supervised learning trains on labeled data (every example comes with a target output) and learns a mapping that predicts the label of new, unseen inputs. Unsupervised learning works on unlabeled data and instead discovers hidden structure in the inputs themselves, such as clusters or low-dimensional representations. The practical difference is the supervision signal: supervised models are corrected against known answers during training, while unsupervised models optimize an intrinsic objective like reconstruction error or cluster compactness. Supervised learning covers classification and regression; unsupervised learning covers clustering and dimensionality reduction.

    (1) Data Requirement: Supervised needs input–label pairs (x_i, y_i); unsupervised needs only inputs x_i, which are far cheaper to collect.
    (2) Learning Objective: Supervised minimizes prediction error against the labels; unsupervised optimizes structure objectives such as cluster compactness or reconstruction loss.
    (3) Typical Tasks: Classification and regression for supervised; clustering, dimensionality reduction, and density estimation for unsupervised.

    Supervised learning pipeline with labels versus unsupervised pipeline without labels

    Figure 1: The supervision signal is the dividing line: supervised training compares predictions with labels, unsupervised training exploits structure in the inputs alone.

    Mathematical Formulation:
    \hat{y} = f(x;\theta)
    \min_\theta \frac{1}{N}\sum_{i=1}^{N}\ell(f(x_i;\theta), y_i)
    \min_{C_1,\ldots,C_K}\sum_{k=1}^{K}\sum_{x_i \in C_k}\lVert x_i - \mu_k \rVert^2

    Where:

    • f(x;\theta) is the supervised model with parameters \theta, mapping input x to a predicted label \hat{y}.
    • \ell(\cdot) is the per-example supervised loss (e.g., cross-entropy or squared error), and i\in\{1,\ldots,N\} indexes the N labeled examples.
    • C_k is the k-th cluster of a K-means objective and \mu_k its centroid; there are no labels y_i anywhere in the unsupervised objective.
    Labeled data with a decision boundary versus unlabeled data grouped into clusters

    Figure 2: Left: labeled points let a supervised model fit a decision boundary. Right: without labels, an unsupervised model can only group points by similarity.

    AspectSupervised LearningUnsupervised Learning
    Training dataLabeled pairs (x_i, y_i)Unlabeled inputs x_i
    GoalPredict outputs for new inputsDiscover hidden patterns or structure
    Typical tasksClassification, regressionClustering, dimensionality reduction
    Feedback signalDirect error against known labelsIntrinsic objective (compactness, reconstruction)

    Login to view more content