ARBIMETRA LAB
K-Nearest Neighbours
Predicts using the K most similar training rows, with a choice of distance and scaling.
What it does
Predicts using the K most similar training rows, with a choice of distance and scaling.
Use it when similar rows are likely to have similar outcomes.
Data you need
- Target
- Numeric features
Supported data: Numeric. Level: Beginner.
Formula and method
d(x,x′) = √Σ(xⱼ − x′ⱼ)²
Standardization parameters are learned from training rows. Each test row is assigned the majority or mean of its K nearest training rows under the chosen distance, so no model is fitted in the usual sense.
Assumptions
- Future data resemble the training data
- Rows are independent
- Distances are meaningful
Input checks
- At least 30 complete rows
- Test set held back from training
- Fixed random seed
How to read the result
Small K is flexible and large K is smoother.
Worked example
Illustrative run on Fisher iris measurements. These figures are the actual output of this model on that example, and they say nothing about your own data.
- Test accuracy = 0.9211 against a majority-class baseline of 0.3421; F1 = 0.922.
- The model is better than always predicting the most common class on this test set.
Limitations
- Sensitive to scaling and irrelevant features
Numerical validation
Validated 13 of 13 automated checks passed, including numerical results, invalid input, missing values, small samples, constant variables, charts and the interpretation text.
| Check | Reference | Result | Abs. error | Tolerance | Source |
|---|---|---|---|---|---|
| Accuracy by brute-force neighbours | 0.92105263 | 0.92105263 | 0.0e+0 | 1e-2 | Independent NumPy calculation |
Computed with python 3.13.2, numpy 2.2.5, scipy 1.14.1, pandas 2.3.3, statsmodels 0.14.4, scikit-learn 1.7.0, in your browser. A passing check confirms the listed comparison only. It does not make any result suitable for your data, and it is not a claim of equivalence with other statistical software.
Questions
Why standardize?
Without it, variables with large units dominate the distance.
ARBIMETRA LAB is an educational and research tool. Results should be checked by the researcher and are not professional advice.
