DecodeLabs AI Internship / Project 02 / Supervised Learning

Iris Species Classifier

A K-Nearest Neighbors model trained on the classic Iris dataset. Give it four measurements and it returns a species, a confidence score, and the exact neighbours that cast the vote.

Test accuracy
93.33%
F1 (weighted)
0.9327
5-fold CV
96.00%
Samples
150
Neighbours
K=5

Predict a species

Drag the sliders or type exact values. Predictions run live as you move, or turn auto-predict off and submit manually.

Measurements centimetres

Each range matches the smallest and largest value in the training data.

Load a typical example

Awaiting measurements

Adjust any slider or pick an example to run the model.

Feature space

All 150 training samples. Your flower is the outlined point.

  • setosa
  • versicolor
  • virginica
  • your input

Model performance

Evaluated on a held-out 20 % test split (30 samples) and cross-validated on all 150. Every chart below is drawn from the live model.

Confusion matrix

Rows are the true class; columns are the prediction.

rows are the true class, columns are the predicted class

Per-class report

Precision, recall and F1 on the test split.

Species Precision Recall F1 Support
setosa 1.00 1.00 1.00 10
versicolor 0.83 1.00 0.91 10
virginica 1.00 0.80 0.89 10
Train / test
120 / 30
CV accuracy
96.0 % ± 2.49
Scaling
StandardScaler

Choosing K: the elbow curve

Test accuracy for K from 1 to 20. K = 5 was chosen because K = 1 fits the split too closely.

Feature distribution by species

Petal measurements separate the classes far better than sepal measurements.

  • setosa
  • versicolor
  • virginica

How the model is built

The full supervised-learning workflow, from raw data to a validated prediction.

  1. Load

    150 iris samples from scikit-learn: 4 numeric features and 3 balanced classes of 50.

    load_iris()
  2. Scale

    Z-score every feature so petal width (0.1 to 2.5 cm) counts as much as sepal length (4.3 to 7.9 cm) in the distance.

    StandardScaler()
  3. Split

    80 / 20 stratified split with a fixed seed, so every run gives the same result and each class appears in both sets.

    train_test_split(stratify=y)
  4. Train

    KNN has no training phase in the usual sense. It stores the scaled points and does the work at prediction time.

    KNeighborsClassifier(n_neighbors=5)
  5. Evaluate

    Accuracy, weighted F1 and a confusion matrix on the test split, then 5-fold cross-validation to check the score holds on other splits too.

    cross_val_score(cv=5)
  6. Serve

    A Flask API returns the prediction, the probabilities and the neighbours that voted, so you can see why the model answered the way it did.

    POST /api/predict

JSON API

Everything on this page comes from three public endpoints. No keys needed.

POST /api/predict

Classify a flower. Returns the species, confidence, per-class probabilities and the K nearest training samples.

curl -X POST https://iris-knn.vercel.app/api/predict \
  -H "Content-Type: application/json" \
  -d '{"sepal_length":5.1,"sepal_width":3.5,
       "petal_length":1.4,"petal_width":0.2}'
GET /api/model

Metrics, confusion matrix, per-class report, K curve, feature ranges and species notes.

curl https://iris-knn.vercel.app/api/model
GET /api/dataset

All 150 raw samples with labels. This is the data behind the scatter plot and histograms.

curl https://iris-knn.vercel.app/api/dataset