Homebrew k-Nearest Neighbors Algorithm (k-NN) vs scikit-learn's built-in KNeighborsClassifier on the Iris Flower Dataset
This Python script does the following:
- Builds a homebrew k-nearest neighbors algorithm (k-NN);
- Uses the homebrew k-NN as well as scikit-learn's built-in KNeighborsClassifier function, to classify the 3 different types of Iris flowers (Setosa, Versicolour, and Virginica), by their sepal length and width, from Ronald Fisher's Iris flower data set, and measures their accuracies, for k equals to 1 to the number of entries in the Iris flower data set minus 1;
- Plots the Iris flower data set on prediction grids generated by the homebrew k-NN and scikit-learn's KNeighborsClassifier, each using a value of k that maximises their respective prediction accuracies; and
- Plots also the prediction accuracies against k of both the homebrew k-NN and scikit-learn's KNeighborsClassifier for comparison.
Get started in three simple steps:
- Download a copy of the script;
- Install the dependencies; and
- Run the script manually.
Download a copy of the script with git clone
.
$ git clone https://github.com/kurtcms/knn-sklearn-iris /app/knn-sklearn-iris/
This script requires the following libraries:
Install them with pip3
:
$ pip3 install numpy matplotlib scikit-learn
Run the script with python3
.
$ python3 /app/knn-sklearn-iris/knn-sklearn-iris.py