DEPLOYING ML CLASSIFIER INTO WEB APP USING FASTAPI

DEPLOYING ML CLASSIFIER INTO WEB APP USING FASTAPI

INTRODUCTION

Deploying an ML model using an API architecture is mostly a secure and reliable way by which users can interact with the ML model, mostly through making an API request. That is, users with authentication access to the API can just send request and expect a response (usually in JSON or XML format) from the analytic server on which the model runs. There are several API architecture for these kind of task, however, FASTAPI has gained popularity since its introduction in 2018. As the name suggest, FASTAPI is fast(i.e. high performance), provides an excellent documentation and has relevant framework for building web APIs with Python. In this article, it is intended to walk readers through a project where a docker image was created for deploying an ML classifier(built on scikit-learn) using FASTAPI.

PROJECT STRUCTURE

The project proceeded in two main stages;

  1. Building ML Classifier : This phase of the project was carried out in Google Colab. Consistent with the CRISP-DM approach , this phase included:

a) Data Understanding: Patient dataset was acquired from John Hopkins University. The dataset had 11 fields where the first 10 fields had patient information and the last field(target variable) specified whether the patient was diagnosed with sepsis or not.

i) Data Overview: An exploratory analysis was performed on the dataset to identify potential issues in the dataset. Fortunately the dataset had little to no issues -thus little time was used in cleaning the data as only the column names were changed.

No alt text provided for this image
Data Overview and Descriptive Stat of variables


ii) Formulating and Testing Hypothesis

Null Hypothesis: There is no significant difference in the likelihood of young and old patients developing Sepsis

Alternate Hypothesis: The likelihood of young patients developing Sepsis differs significantly from that of old patients.

No alt text provided for this image
Hypothesis Testing


ii) Uni-variate and Multivariate Analysis: Using the visualization, the distribution of the individual variables (to detect outliers and "tailedness" of their variables) and their relationship between these variables was explored.

No alt text provided for this image
Visualizing the distribution of the variables using KDE plot

b) Data Preparation : Here, the target variable (Sepsis) and independent variables were encoded and scaled respectively using scikit-learn packages LabelEncoder() and StandardScalar().

c) Modelling : Here, six different models were built, trained and evaluated. After evaluation, four(4) best models underwent hyperparameter tuning and the best estimator with high performance was chosen as our ML classifier. The main stages in the modelling:

i) Data Splitting : Dataset available for training was split into train and anevaluation set.

No alt text provided for this image
The split data was straified over the target variables to ensure that the original class distribution over both the train and eval set

ii) Balancing the target variables: There was an imbalance in the target variables. As part of standard practice to reduce bias towards one class during training, the dataset was balanced using the SMOTE technique (over-sampling).

No alt text provided for this image
Count grouped by classes before balancing
No alt text provided for this image
Count group by class after balancing

iii) Training: Six models were trained using the trainset and evaluated over the evaluation set. Below were the evaluation scores for each model.

No alt text provided for this image

iv) Hyper Parameter Tuning

The best performing models were further tuned to select the best performing estimator. Find the results of that below:

No alt text provided for this image


v) Exporting components

All relevant components such as requirements,txt, model preprocessing functions and the best model were exported to be used in the application

2. Web Application using FastAPI

i) Setting environment variables: Here, a virtual environment was created and all dependencies for the project were installed.

No alt text provided for this image

iii) Deploying App

No alt text provided for this image
Available at: https://github.com/MrDadzie/Sepsis_Classification_Project.git
No alt text provided for this image
Swagger Documentation with endpoints
No alt text provided for this image
Swagger Documentation with response body from a request


3. Container: Finally, the docker container is created using Dockerfile to build an image.

No alt text provided for this image
Available at: https://github.com/MrDadzie/Sepsis_Classification_Project.git

CONCLUSION

FASTAPI provides a fast, modern architecture for deploying ML applications. This project has been insightful and anpther step deeper into the world of data and solutions . All files are available on my GitHub : https://github.com/MrDadzie/Sepsis_Classification_Project.git.

There is more to come...........

REFERENCES

  1. Azubi Africa. https://www.azubiafrica.org/
  2. FASTAPI Documentation. https://fastapi.tiangolo.com/
  3. Machine Learning with Scikit -learn.https://engineering.rappi.com/serve-your-first-model-with-scikit-learn-flask-docker-df95efbbd35e

要查看或添加评论,请登录

Gideon Dadzie的更多文章

  • Sentiment Analysis

    Sentiment Analysis

    Introduction Sentiment analysis is a Natural Learning Processing(NLP) technique which centers on finding the intentions…

  • ML EMBEDDING PROJECT

    ML EMBEDDING PROJECT

    INTRODUCTION After successful implementation of ML models to solve real life business situations, it is important that…

    1 条评论
  • Telco Customer Churn Prediction - An ML Classification Problem

    Telco Customer Churn Prediction - An ML Classification Problem

    The continuous influx of innovative products and services by telecom companies provide customers with a wide range of…

  • Store Sales Analysis and Forecast

    Store Sales Analysis and Forecast

    Sales is an essential part of any business venture. It acts as an indicator of business health and holds the lifeline…

  • Indian Startup Ecosystem Analysis Project

    Indian Startup Ecosystem Analysis Project

    The nascence of creativity as well as innovational ideas is constant in a world where socioeconomic gaps exists and new…

社区洞察

其他会员也浏览了