Skip to content

About

Exhaustive KNN hyperparameter search and evaluation pipeline using cross-validation and multiple classification metrics.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

KNN Hyperparameter Search

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.

What It Does

The search space contains:

  • 100 values of K, from 1 to 100
  • 2 weighting strategies: uniform and distance
  • 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

Distance Metrics

The experiment compares:

  • Euclidean
  • Manhattan
  • Minkowski
  • Chebyshev
  • Cosine
  • Canberra
  • Bray-Curtis
  • Correlation

Tech Stack

  • Python
  • NumPy
  • pandas
  • scikit-learn
  • Matplotlib

Pipeline

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.

Running the Project

Requirements

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

Install the dependencies:

pip install numpy pandas matplotlib scikit-learn

Run the experiment:

python main.py

Output

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

Implementation Notes

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.

About

Exhaustive KNN hyperparameter search and evaluation pipeline using cross-validation and multiple classification metrics.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages