KNN-Based Handwritten Digit Classification with Accuracy Analysis and Visualization
DOI:
https://doi.org/10.51903/776j7727Keywords:
Handwritten Digit Recognition, K-Nearest Neighbors, Digital Image Processing, Machine Learning, ClassificationAbstract
The rapid advancement of information technology has significantly impacted various fields, particularly in pattern recognition and image processing. One of the persistent challenges in this area is accurately recognizing handwritten digits, which plays a crucial role in digitizing documents, automated form reading, and other intelligent systems. This study aims to implement and evaluate the K-Nearest Neighbors (KNN) algorithm, a simple yet effective classification technique, for recognizing handwritten digit images. The research utilizes the publicly available load_digits dataset from Scikit-learn, which contains 1,797 grayscale images of handwritten digits sized 8x8 pixels. Each image is represented as a 64-dimensional feature vector. The dataset is divided into training and testing sets with an 80:20 ratio, and the model is trained using KNN with k=3. The experimental results show a high classification accuracy of 96.94%, with minimal misclassifications mainly occurring between visually similar digits such as 5 and 9. This study demonstrates that KNN, despite its simplicity, is capable of delivering high accuracy in handwritten digit recognition when supported by appropriate preprocessing and parameter selection. The implications of this research highlight the potential for developing intelligent applications in fields requiring digit recognition, such as education, data entry automation, and identity verification.
References
R. Ghanamah, H. Eghbaria-Ghanamah, and E. Adi-Japha, “The differential association of COVID-19 remote digital instruction period with second-grade students’ graphomotor, handwriting, visual, and sequential memory skills,” Learn Instr, vol. 91, p. 101898, Jun. 2024, doi: 10.1016/j.learninstruc.2024.101898.
K. Barrere, Y. Soullard, A. Lemaitre, and B. Coüasnon, “Training transformer architectures on few annotated data: an application to historical handwritten text recognition,” International Journal on Document Analysis and Recognition (IJDAR), vol. 27, no. 4, pp. 553–566, Dec. 2024, doi: 10.1007/s10032-023-00459-2.
H. A. Alhamad et al., “Handwritten Recognition Techniques: A Comprehensive Review,” Symmetry (Basel), vol. 16, no. 6, p. 681, Jun. 2024, doi: 10.3390/sym16060681.
A. İ. Çetin and A. H. Büyüklü, “A new approach to K-nearest neighbors distance metrics on sovereign country credit rating,” Kuwait Journal of Science, vol. 52, no. 1, p. 100324, Jan. 2025, doi: 10.1016/j.kjs.2024.100324.
K. D. Toennies, “Distance-Based Classifiers,” in An Introduction to Image Classification, Singapore: Springer Nature Singapore, 2024, pp. 95–107. doi: 10.1007/978-981-99-7882-3_5.
R. K. Halder, M. N. Uddin, Md. A. Uddin, S. Aryal, and A. Khraisat, “Enhancing K-nearest neighbor algorithm: a comprehensive review and performance analysis of modifications,” J Big Data, vol. 11, no. 1, p. 113, Aug. 2024, doi: 10.1186/s40537-024-00973-y.
E. Kandić, A. Akagic, and M. Bohlouli, “Exploring Convolutional Autoencoder Efficacy in Noise Removal for Image Processing and Computer Vision: A Study Using the MNIST Dataset,” in 2024 10th International Conference on Control, Decision and Information Technologies (CoDIT), IEEE, Jul. 2024, pp. 1275–1280. doi: 10.1109/CoDIT62066.2024.10708626.
J. Mei, “A comparison between CNN and leNet in fashion Mnist dataset,” 2024, p. 040017. doi: 10.1063/5.0223181.
UCI ML repository, “Optical Recognition of Handwritten Digits - UCI Machine Learning Repository.” Accessed: May 29, 2025. [Online]. Available: https://archive.ics.uci.edu/dataset/80/optical+recognition+of+handwritten+digits
S. V. Razavi-Termeh, A. Sadeghi-Niaraki, S. Razavi, and S.-M. Choi, “Enhancing flood-prone area mapping: fine-tuning the K-nearest neighbors (KNN) algorithm for spatial modelling,” Int J Digit Earth, vol. 17, no. 1, Dec. 2024, doi: 10.1080/17538947.2024.2311325.
M. I. Fidatama, F. Bimantoro, G. S. Nugraha, B. Irmawati, and R. Dwiyansaputra, “Recognition of Bima script handwriting patterns using the local binary pattern feature extraction method and K-nearest neighbour classification method,” 2023, p. 020009. doi: 10.1063/5.0111770.
A. Susanto, I. U. W. Mulyono, C. A. Sari, E. H. Rachmawanto, and R. R. Ali, “Javanese Character Recognition Based on K-Nearest Neighbor and Linear Binary Pattern Features,” Kinetik: Game Technology, Information System, Computer Network, Computing, Electronics, and Control, Sep. 2022, doi: 10.22219/kinetik.v7i3.1491.
F. E. Batool et al., “Offline signature verification system: a novel technique of fusion of GLCM and geometric features using SVM,” Multimed Tools Appl, vol. 83, no. 5, pp. 14959–14978, Apr. 2020, doi: 10.1007/s11042-020-08851-4.
I. W. A. S. Darma, “Handwritten Balinesse Character Recognition using K-Nearest Neighbor,” May 22, 2018. doi: 10.31227/osf.io/z6m8u.
E. Prasetyo, R. D. Adityo, N. Suciati, and C. Fatichah, “Multi-class K-support Vector Nearest Neighbor for Mango Leaf Classification,” TELKOMNIKA (Telecommunication Computing Electronics and Control), vol. 16, no. 4, p. 1826, Aug. 2018, doi: 10.12928/telkomnika.v16i4.8482.
A. S. Wijaya, N. Chamidah, and M. M. Santoni, “Pengenalan Karakter Tulisan Tangan Dengan K-Support Vector Nearest Neighbor,” IJEIS (Indonesian Journal of Electronics and Instrumentation Systems), vol. 9, no. 1, p. 33, Apr. 2019, doi: 10.22146/ijeis.38729.
E. Prasetyo, R. D. Adityo, and R. Purbaningtyas, “Classification of Segmented Milkfish Eyes using Cosine K-Nearest Neighbor,” in 2019 2nd International Conference on Applied Information Technology and Innovation (ICAITI), IEEE, Sep. 2019, pp. 93–98. doi: 10.1109/ICAITI48442.2019.8982124.
P. H. Jain, V. Kumar, J. Samuel, S. Singh, A. Mannepalli, and R. Anderson, “Artificially Intelligent Readers: An Adaptive Framework for Original Handwritten Numerical Digits Recognition with OCR Methods,” Information, vol. 14, no. 6, p. 305, May 2023, doi: 10.3390/info14060305.
J. Memon, M. Sami, R. A. Khan, and M. Uddin, “Handwritten Optical Character Recognition (OCR): A Comprehensive Systematic Literature Review (SLR),” IEEE Access, vol. 8, pp. 142642–142668, 2020, doi: 10.1109/ACCESS.2020.3012542.
D. Impedovo and G. Pirlo, “Zoning methods for handwritten character recognition: A survey,” Pattern Recognit, vol. 47, no. 3, pp. 969–981, Mar. 2014, doi: 10.1016/j.patcog.2013.05.021.
C.-L. Liu, K. Nakashima, H. Sako, and H. Fujisawa, “Handwritten digit recognition: investigation of normalization and feature extraction techniques,” Pattern Recognit, vol. 37, no. 2, pp. 265–279, Feb. 2004, doi: 10.1016/S0031-3203(03)00224-3.
K. Korovai, D. Zhelezniakov, O. Yakovchuk, O. Radyvonenko, N. Sakhnenko, and I. Deriuga, “Handwriting Enhancement: Recognition-Based and Recognition-Independent Approaches for On-Device Online Handwritten Text Alignment,” IEEE Access, vol. 12, pp. 99334–99348, 2024, doi: 10.1109/ACCESS.2024.3412433.
C. Reddy, S. Mallesh, R. Rao, R. Sivan, and P. B. Pati, “Comparison of GLCM and DCT features for Malayalam Palm Leaf Character Recognition,” in 2024 5th International Conference for Emerging Technology (INCET), IEEE, May 2024, pp. 1–5. doi: 10.1109/INCET61516.2024.10592989.
Downloads
Published
Issue
Section
License
All articles published in this journal are open access and licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License (CC BY-SA 4.0).
Under this license:
-
Authors retain copyright of their work.
-
Others are free to:
-
Share — copy and redistribute the material in any medium or format;
-
Adapt — remix, transform, and build upon the material for any purpose, even commercially;
-
-
As long as they:
-
Give appropriate credit to the original author(s) and the source;
-
Provide a link to the license;
-
Indicate if changes were made;
-
Distribute their contributions under the same license as the original.
-
This ensures that derivative works will also remain free and open under the same terms.













.png)