Diagnosing Diseases Using kNN

An Application of kNN to Diagnose Diabetes

Elena Boiko, Jacqueline Razo (Advisor: Dr. Cohen)

2026-11-01

Introduction

In healthcare, kNN has shown promise in predicting chronic diseases like diabetes (Suriya and Muthu 2023) and hypertension (Khateeb and Usman 2017).

In this project, we focus on how kNN can be applied and optimized to predict diabetes, a critical and growing public health issue.

Why This Matters

- Diabetes affects millions worldwide. Early detection can improve outcomes.

- Machine learning, especially interpretable models like kNN, can support diagnosis.

- Our project explores:

- How different k values, distance metrics, and preprocessing techniques affect kNN’s performance.

- Whether kNN is competitive with other models for this task.

Why We Chose kNN for This Project

Well-suited for medical datasets with small to medium size

Easy to interpret — great for health professionals

Flexible with minimal assumptions

Can impute missing data and detect patterns

Our Approach

  • Clean and preprocess real-world survey data.
  • Train kNN models with various configurations.
  • Evaluate performance and compare with tree-based models.

Method: kNN Overview

k-Nearest Neighbors (kNN) is a non-parametric, instance-based learning algorithm

It is a lazy learner — no explicit training phase is required

Instead, it classifies new data based on similarity to existing labeled points (Zhang 2016)

Classification Process:

1. Distance Calculation:
Measures similarity using metrics like Euclidean or Manhattan distance

2. Neighbor Selection:
Hyperparameter k defines how many nearby points to consider

3. Majority Voting:
The most frequent class among the k nearest neighbors determines the prediction

Distance Calculation:

kNN identifies the nearest neighbors by calculating distances between points.

Euclidean distance: (Theerthagiri et al. 2022) \[ d = \sqrt{(X_2 - X_1)^2 + (Y_2 - Y_1)^2} \]

Manhattan distance: (Aggarwal et al. 2015) \[ d = |X_2 - X_1| + |Y_2 - Y_1| \]

Classification Process

The red square represents a data point to be classified. The algorithm selects the 5 nearest neighbors within the green circle—3 hearts and 2 circles. Based on the majority vote, the red square is classified as a heart.

Figure 1. kNN with k=5

Strengths and Weaknesses of kNN

Strengths

  • Simple, intuitive, and non-parametric — no assumptions about data distribution
  • No training phase — the algorithm learns during prediction
  • Performs well on small to medium datasets, especially when features are well-scaled
  • Easy to understand and implement — ideal for baseline models or educational use

Weaknesses of kNN

  • Slow prediction time on large datasets due to distance calculations (Deng et al. 2016)
  • Sensitive to feature scaling and distance metric choice (Uddin et al. 2022)
  • Choosing the right ‘k’ is critical — too low or high can reduce performance
  • Affected by irrelevant or correlated features, which may distort neighbor similarity

Analysis and Results

Data Source and Collection

Data Source: CDC Diabetes Health Indicators

Collected via the CDC’s Behavioral Risk Factor Surveillance System (BRFSS)

Dataset contains 253,680 survey responses

Covers 21 features: demographics, lifestyle, healthcare, and health history

Target: Diabetes_binary

(0 = No diabetes, 1 = Diabetes/Prediabetes)

Data Challenges

Data Quality
No missing values
24,206 duplicate rows detected

Outliers & Scaling Sensitivity
BMI, MentHlth, PhysHlth had extreme values
kNN is highly sensitive to scale

Feature Relationships
No strong multicollinearity (max r = 0.52)
All features retained for now

Early Insight
Higher BMI in diabetic cases, but overlapping range
Used as a predictor along with other features

Class Distribution:

Diabetes Class Imbalance

Key Points
Significant class imbalance observed
Majority class: No Diabetes (0) – 86.07%
Minority class: Diabetes (1) – 13.93%

Impact on Modeling
Imbalance can bias predictions
Models may underpredict diabetes cases

Preparing the Data

  • Removed 24,206 duplicate rows
    Diabetic class increased from 13.9% → 15.3%

  • Kept ordinal features as numeric
    Age, Education, Income, and GenHlth retained due to natural ordering

  • Scaled Features with Outliers
    Features scaled with StandardScaler or RobustScaler (fitted on training data)

  • Handled class imbalance
    Applied SMOTE to generate synthetic diabetic samples

➡️ Final dataset: clean and scaled; SMOTE applied to training data only

kNN Model Setup

  • Explored different k values: 5, 10, 15
  • Compared distance metrics: Euclidean vs. Manhattan
  • Evaluated weighting methods: uniform vs. distance
  • Tested multiple scaling techniques
  • Included variations with SMOTE and Feature Selection
Model k Distance Weights Scaler SMOTE
kNN 1 5 Euclidean (p=2) Uniform StandardScaler No
kNN 2 15 Manhattan (p=1) Distance RobustScaler No
kNN 3 10 Euclidean (p=2) Uniform StandardScaler Yes
kNN 4 15 Euclidean (p=2) Distance StandardScaler Yes (Feature Selection)

Performance of kNN Variants

Table 4: Performance Comparison of kNN Models

Model k Distance Weights Scaler SMOTE Accuracy ROC_AUC PR_AUC Precision_1 Recall_1 F1_1
kNN 1 5 Euclidean (p=2) Uniform StandardScaler No 0.832 0.705 0.281 0.406 0.206 0.274
kNN 2 15 Manhattan (p=1) Distance RobustScaler No 0.841 0.752 0.334 0.446 0.155 0.230
kNN 3 10 Euclidean (p=2) Uniform StandardScaler Yes 0.691 0.733 0.286 0.278 0.641 0.388
kNN 4 15 Euclidean (p=2) Distance StandardScaler Yes (FS) 0.731 0.722 0.300 0.303 0.579 0.397
  • Best ROC-AUC among kNN: kNN 2 (0.752)
    RobustScaler, Manhattan distance, distance weighting

  • SMOTE raised recall from ~0.2 to 0.58–0.64
    at the cost of lower precision

  • Feature selection (kNN 4): 12 features instead of 21, mixed results vs. kNN 3

Comparing kNN with Tree Models

Table 5: kNN 4 vs. Tree-Based Models

Model SMOTE Accuracy ROC_AUC PR_AUC Precision_1 Recall_1 F1_1
KNN 4 Yes 0.731 0.722 0.300 0.303 0.579 0.397
Decision Tree Yes 0.671 0.756 0.317 0.281 0.740 0.407
Decision Tree No 0.849 0.794 0.388 0.517 0.154 0.237
Random Forest No 0.854 0.813 0.432 0.599 0.134 0.219

- Random Forest had the best ROC-AUC (0.813) and PR-AUC (0.432)

- Decision Tree with SMOTE had the highest recall (0.740) and F1 (0.407)

- Random Forest had highest accuracy, but poor recall (0.13) shows it struggled to detect diabetic cases

Tree-based models offer interpretability, but may need tuning or resampling for minority detection

ROC_AUC curves comparison

ROC curves of kNN 4 and the tree-based models on the same test set.
Random Forest has the highest AUC (0.81), kNN 4 reaches 0.72.

ROC Curve

Conclusion

  • Preprocessing changes kNN’s behavior: configurations with SMOTE had ~3× higher recall for diabetic cases (~0.6 vs. ~0.2); RobustScaler, Manhattan distance, k = 15 and distance weighting (kNN 2) improved ROC-AUC (0.70 → 0.75).

  • On this large dataset, tree-based models achieved higher ROC-AUC: Random Forest had the best ROC-AUC (0.813), Decision Tree with SMOTE the highest recall (0.740).

  • No single best model: the choice of model and metric should follow the goal of the analysis.

  • Accuracy alone is misleading for imbalanced medical data.

  • Revision 2026: the original evaluation of kNN 4 (AUC 0.88) contained data leakage; all models were re-evaluated on one untouched test set.

References

Aggarwal, Charu C et al. 2015. Data Mining: The Textbook. Vol. 1. Springer.
Deng, Zhenyun, Xiaoshu Zhu, Debo Cheng, Ming Zong, and Shichao Zhang. 2016. “Efficient kNN Classification Algorithm for Big Data.” Neurocomputing 195: 143–48.
Khateeb, Nida, and Muhammad Usman. 2017. “Efficient Heart Disease Prediction System Using k-Nearest Neighbor Classification Technique.” Proceedings of the International Conference on Big Data and Internet of Thing, 21–26.
Suriya, S, and J Joanish Muthu. 2023. “Type 2 Diabetes Prediction Using k-Nearest Neighbor Algorithm.” Journal of Trends in Computer Science and Smart Technology 5 (2): 190–205.
Theerthagiri, Prasannavenkatesan, A Usha Ruby, and J Vidya. 2022. “Diagnosis and Classification of the Diabetes Using Machine Learning Algorithms.” SN Computer Science 4 (1): 72.
Uddin, Shahadat, Ibtisham Haque, Haohui Lu, Mohammad Ali Moni, and Ergun Gide. 2022. “Comparative Performance Analysis of k-Nearest Neighbour (KNN) Algorithm and Its Different Variants for Disease Prediction.” Scientific Reports 12 (1): 6256.
Zhang, Zhongheng. 2016. “Introduction to Machine Learning: K-Nearest Neighbors.” Annals of Translational Medicine 4 (11).