An exhaustive hyperparameter search and evaluation pipeline for K-Nearest Neighbors (KNN) using the scikit-learn breast cancer dataset.
The project evaluates 1,600 KNN configurations across different neighbor counts, weighting strategies, and distance metrics using 10-fold cross-validation. Results are compared across five classification metrics and exported for further analysis.
The search space contains:
- 100 values of K, from 1 to 100
- 2 weighting strategies:
uniformanddistance - 8 distance metrics
This produces a total of 1,600 model configurations.
Each configuration is evaluated with 10-fold cross-validation using:
- Accuracy
- Precision
- Recall
- Specificity
- F1-score
The experiment compares:
- Euclidean
- Manhattan
- Minkowski
- Chebyshev
- Cosine
- Canberra
- Bray-Curtis
- Correlation
- Python
- NumPy
- pandas
- scikit-learn
- Matplotlib
Breast Cancer Dataset
|
v
KNN Configuration Search
(K × weights × distance metrics)
|
v
10-Fold Cross-Validation
|
v
Evaluation Metrics
|
+--> CSV results
+--> JSON results
+--> Radar-chart visualization
The program automatically identifies the highest-scoring configuration for each evaluation metric and generates radar charts to compare their overall behavior.
- Python 3.x
- NumPy
- pandas
- scikit-learn
- Matplotlib
Clone the repository:
git clone https://github.com/saw-cdt/knn-hyperparameter-search.git
cd knn-hyperparameter-searchInstall the dependencies:
pip install numpy pandas matplotlib scikit-learnRun the experiment:
python main.pyGenerated artifacts are stored in the results/ directory:
results/
├── resultados_knn.csv
├── resultados_knn.json
└── mejores_5_metricas_knn.png
The CSV and JSON files contain the metrics for every evaluated configuration. The generated visualization compares the configurations that achieve the highest score for each target metric.
Specificity is implemented as a custom scikit-learn scorer using the confusion matrix. The remaining metrics use scikit-learn's standard scoring functions.
This project focuses on systematic model evaluation and hyperparameter experimentation rather than implementing the KNN algorithm from scratch.