Logistic, k-NN, naive Bayes formulas
Logistic regression, k-nearest neighbour and naive Bayes. 21 formulas for GATE DA Machine Learning, each with when to use it. Each is explained in the topic notes.
IAE Academy: Logistic regression, k-nearest neighbour and naive Bayes formulas (GATE DA)
All 21 Logistic, k-NN, naive Bayes formulas
SIGMOID (LOGISTIC FUNCTION)
The output lies in (0, 1). ; as and as .
SIGMOID IDENTITIES
The derivative is at most 0.25, reached at . This identity is what makes the gradient of the cross-entropy loss so simple.
LOGISTIC REGRESSION MODEL
Binary classification. The prediction is class 1 when this probability exceeds the chosen threshold, which is 0.5 unless the question states another.
LOG-ODDS (LOGIT) IS LINEAR
With the other features fixed, each unit increase in adds to the log-odds, which multiplies the odds by .
DECISION BOUNDARY
The boundary is a hyperplane, so logistic regression is a linear classifier. is normal to the boundary and points towards the class-1 side. Changing moves the hyperplane parallel to itself.
LOG-LIKELIHOOD
Each sample contributes . Maximising and minimising give the same weights.
CROSS-ENTROPY LOSS (NATURAL LOG)
For one sample the loss is if and if . Using instead only multiplies the loss by a constant.
THE SAME LOSS WITH LABELS IN {−1, +1}
This is identical to the cross-entropy above, rewritten for labels using . Check which label convention a question uses before you substitute.
GRADIENT OF THE LOSS
Labels in . This has the same form as linear regression's gradient when that loss is written , with in place of the linear prediction. For a single sample, drop the and the sum.
L2-REGULARISED LOSS AND GRADIENT
sets the strength of the penalty. The bias is usually not penalised, so is unchanged. Books scale differently (with or without or ), so use the form the question gives.
GRADIENT DESCENT UPDATE
is the learning rate. With the squared-error loss per sample instead, the gradient picks up an extra factor . Learning then slows when the sigmoid saturates, and the loss is no longer convex in .
EUCLIDEAN, MANHATTAN, MINKOWSKI AND CHEBYSHEV DISTANCE
Euclidean is , Manhattan is , and Chebyshev is the limit . is a metric for . The order is written here because means a predicted probability in these notes. The nearest neighbour can change from one metric to another, so always use the one the question names.
K-NN REGRESSION
is the set of the nearest training points. A distance-weighted version uses weights proportional to . A query that coincides with a training point () is usually given that point's value.
MIN-MAX AND Z-SCORE SCALING
and are the mean and standard deviation of the feature. Compute , , and from the training data, and apply the same values to the query.
NAIVE BAYES POSTERIOR
This assumes the features are conditionally independent given the class. The denominator is the same for every class, so you need it only when the question asks for an actual probability.
NAIVE BAYES DECISION RULE (MAP)
Compare the unnormalised scores. In practice the logs are summed, , to avoid numerical underflow. With equal priors, the rule reduces to picking the class with the largest likelihood (maximum likelihood).
ESTIMATES FROM COUNTS
is the number of training samples in class , and is how many of them have feature equal to . These are maximum-likelihood estimates and can be zero. Priors are smoothed only if the question says so.
LAPLACE (ADD-$\ALPHA$) SMOOTHING
is the number of distinct values feature can take (2 for a binary feature). is Laplace smoothing. It stops a single unseen value from forcing the whole product to zero.
MULTINOMIAL NAIVE BAYES (WORD COUNTS)
Used for text classification. is the number of times word appears across the training documents of class , is the vocabulary size, and is how often occurs in the document being classified.
GAUSSIAN NAIVE BAYES
For continuous features. and are the mean and variance of feature within class (here is a standard deviation, not the sigmoid). The maximum-likelihood variance divides by and the unbiased one by ; use the one the question states. The result is a density, so it can exceed 1. Multiply the terms together as before.
NUMBER OF PARAMETERS (D BINARY FEATURES, K CLASSES)
The independence assumption makes the parameter count grow linearly in instead of exponentially. The term counts the priors, since they sum to 1.
Where this was asked: 2024 (3), 2025 (1). See the 4 previous-year questions on Logistic, k-NN, naive Bayes. This shows what was asked, not what will be asked.