DEPLOYING ML CLASSIFIER INTO WEB APP USING FASTAPI
Gideon Dadzie
Data Analyst/Engineer ||Backend Engineer||Electrical/ Electronic Engineer|| Writer
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;
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.
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.
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.
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.
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).
领英推荐
iii) Training: Six models were trained using the trainset and evaluated over the evaluation set. Below were the evaluation scores for each model.
iv) Hyper Parameter Tuning
The best performing models were further tuned to select the best performing estimator. Find the results of that below:
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.
iii) Deploying App
3. Container: Finally, the docker container is created using Dockerfile to build an image.
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 年Great man??
-
1 年Keep this up man. ??????????????