Skip to content

Repository files navigation

Iris Classifier Inference API

Inference API using FastAPI with Python 3.12 Data explorations on the IRIS dataset done in Jupyter notebooks helped data understanding and informed model evaluation.

Development follows an interative cycle between:

  1. Devops
  2. Software engineering/full-stack developement
  3. Data science
  4. Data engineering

Each step allowed to have minimal project basis, which could foster collaboration in a team setting. It allowed for quick wins and identifying future improvements quickly. Devops setup informed project structure and tooling for the tech-stack and tooling in Github. Designing and implementing a first version of the API with mocked endpoints allowed simple integration of the model to come. Data exploration/science informed model selection, which in turn informed model training scripts. These scripts then were available to be included in the devops tooling, restarting the cycle. Second cycle the addresses improvements and informs the discussion to be had on the project.

img

Quick Start

Setup

Setting up the Python virtual env for dev requires running the following commands.

> python -m venv .venv 
> source .venv/bin/activate
> pip install -r requirements.txt

Running the API Locally

# Start the FastAPI server
python src

The API will be available at http://localhost:8888

Running with Docker

# Build and start all services
docker compose up -d --build

# Stop all services
docker compose down

The API will be available as a single uvicorn worker at http://localhost:8888 and behind a NGINX proxy in replicas at http://localhost/api.

Model Training

To train the KNN model:

# Make sure you're in the project root
python models/train_knn.py

The trained model will be saved to models/knn_model.pkl.

Model Details

  • Type: K-Nearest Neighbors (KNN) Classifier
  • Hyperparameters:
    • n_neighbors: 9
    • Default settings for other parameters (uniform weights, euclidean distance)
  • Features used:
    • Sepal length (cm)
    • Sepal width (cm)
    • Petal length (cm)
    • Petal width (cm)

Model Performance

  • Training Accuracy: 98.2%
  • Mean Absolute Error: 0.013422818791946308

The model was selected after comparing multiple algorithms including SVC, LogisticRegression, DecisionTree, and RandomForest classifiers. KNN demonstrated the best performance on this dataset before and after removing duplicates.

For detailed model evaluation and selection process, refer to notebooks/model-selection.ipynb.

Working with Notebooks

Using VS Code

  1. Open the repository in VS Code
  2. Select the .venv Python interpreter
  3. Navigate to notebooks/ directory
  4. Open and run .ipynb files

Using Jupyter

# Activate virtual environment
source .venv/bin/activate

# Start Jupyter
jupyter notebook notebooks/

Available notebooks:

  • eda.ipynb: Exploratory data analysis
  • model-selection.ipynb: Model evaluation and selection

API Documentation

Once the API is running, you can access:

Development

Code Quality

This project uses Ruff for code linting and formatting.

To check your code:

> ruff check .

To automatically fix linting issues:

> ruff check --fix .

To format your code:

> ruff format .

Testing

This project uses pytest for testing. To run the tests:

> pytest

To run tests with coverage report:

> pytest --cov=src --cov-report=term-missing

To generate an HTML coverage report:

> pytest --cov=src --cov-report=html

The HTML coverage report will be generated in the htmlcov directory and can be viewed in your browser:

> open htmlcov/index.html

VS Code users can run tests directly in the editor using the Testing sidebar or by clicking the "Run Test" links that appear above each test.

About

This repository is a project that assigned through a recruitment process for an applied machine learning position. The position was not fulfilled. I believe it's a great showcase of my ability to work in a full-stack context as much as on the data (science/ml) side.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Used by

Contributors

Languages