
An Application of kNN to Diagnose Diabetes
2026-11-01
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.
- 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.
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
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)
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
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| \]

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.

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 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
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

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
| 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) |
| 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
| 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 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
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.