Prediction result
Complete the form and calculate a prediction.
Class vote summary
Nearest neighbours used
Step-by-step explanation
Calculation record
Vote chart
Distance chart
Feature-space view
The first two usable features are shown when automatic feature mode is active.
Predictions across K values
Formula used
KNN classification selects the K closest labelled samples. Each selected neighbour contributes a vote to its class. The class with the strongest final vote becomes the prediction.
How to use this calculator
- Select known-neighbour or automatic feature mode.
- Enter class labels and distances, or paste labelled feature rows.
- Choose K, a voting method, and a tie rule.
- Configure scaling, feature weights, and ignored features when needed.
- Calculate and review votes, distances, charts, warnings, and K comparisons.
- Copy, export, download, or print the completed calculation.
Example data formats
| Mode | Example | Meaning |
|---|---|---|
| Known neighbours | N1,Cat,0.42,1,0.70 | ID, class, distance, weight, similarity. |
| Training feature row | S1,Red,1.2,2.4,0 | ID, class, then feature values. |
| Query feature row | Q1,2.0,2.5,0 | Query ID followed by matching features. |
Understanding KNN majority voting
How K changes predictions
A small K reacts strongly to nearby samples. A larger K usually creates smoother predictions. Very large values can hide important local patterns.
Why feature scaling matters
Features with large numeric ranges can dominate distances. Standardisation or min-max scaling reduces that problem. Scaling should use statistics from the training data.
Majority versus weighted voting
Majority voting treats selected neighbours equally. Weighted voting gives closer neighbours greater influence. Weighted methods often help when class boundaries overlap.
Distance metric differences
Euclidean distance measures straight-line separation. Manhattan distance sums absolute differences. Hamming distance compares mismatches in categorical or binary features.
Imbalanced classes
Common classes may dominate local votes. Distance weighting can reduce some effects. Balanced sampling or class-aware methods may still be necessary.
Advantages and limitations
KNN is intuitive and requires little training. Prediction can become slow for large datasets. Results depend heavily on scaling, K, and distance choices.
Frequently asked questions
What does majority vote mean in KNN?
It counts class labels among the K nearest samples. The most frequent class becomes the prediction.
Should K always be odd?
No. Odd values only reduce two-class ties. Multiclass data can still tie with odd K.
What happens when a neighbour has zero distance?
The sample exactly matches the query. Weighted methods give it extremely strong influence using a small safety value.
Can this calculator handle multiple classes?
Yes. It groups every selected neighbour by its entered class label.
When should I use cosine distance?
Cosine distance is useful when direction matters more than magnitude. Text vectors are a common example.
Why can scaling change the predicted class?
Scaling changes feature contributions to distance. This can change which samples are considered nearest.
Is the vote ratio a calibrated probability?
No. It is a descriptive confidence-like ratio. Proper probability calibration requires separate validation methods.
How are missing values handled?
You can reject rows, replace values, or ignore missing feature pairs. Each choice can affect distances.
What is the difference between classification and regression?
Classification predicts a class label. KNN regression instead averages numerical target values from nearby samples.