How to Build an Online Machine Learning App With Python



Original Source Here

How to Build an Online Machine Learning App With Python

Offering simplified and scalable machine learning as a service

Source: Dummy Learn

Introduction

Machine learning is rapidly becoming as ubiquitous as data itself. Quite literally wherever there is an abundance of data, machine learning is somehow intertwined. And for good reason too. After all, what utility would data have if we were not able to use it to predict something about the future? Luckily there is a plethora of toolkits and frameworks that have made it rather simple to deploy ML in Python. Specifically, Sklearn has done a terrifically effective job at making ML accessible to developers.

Even still, there are many technical and STEM-associated individuals that would have plenty of use for ML but who lack the necessary coding skills to see it through. This specific audience would require a solely visual tool with a friendly user interface that would allow them to replicate exactly what others would do with code.

These days tech is dominated by the ‘as-a-service’ paradigm that seeks to offer literally anything as a service, including but not limited to software, databases, CAD, and so forth. There’s no reason to believe that machine learning is somehow exempt from this notion. As you have probably guessed by now, there is indeed the term MLaaS or in other words, machine learning as a service.

While there are quite a few MLaaS platforms out there, most of them appear to be priced quite heftily, with some setting you back upwards of $300 a month. Well truth be told, it doesn’t have to be that way. There is in fact a way for you to render your own simplified MLaaS platform as an online web application. And in this tutorial, I will show you exactly how to do that.

But before we proceed any further, please take a look at the final ML app:

Source: Dummy Learn

You can also test the ML App at the following link:

Tech Stack

Sklearn

To implement our own version of MLaaS, we will be using the Sklearn library in Python. The novelty of this package is that it simplifies the complexity of training, and testing a variety of ML classifiers including but not limited to logistic regression, naive Bayes, support vector machine, decision tree, and K nearest neighbors.

Streamlit

To render our ML platform as a web app, we will be using Streamlit. Streamlit is a pure Python web framework that has all but closed the app development gap for Python programmers. It is a robust and scalable API with an exceptionally shallow learning curve, that has reduced development time from weeks to minutes.

Plotly

To plot and visualize your confusion matrix, ROC curve, and perhaps other data, we will be using Plotly — my favorite library when it comes to visualizing interactive charts and visuals in a web application.

Pandas

Pandas gives you the ability to manipulate, mutate, transform and visualize data in frames, all with a couple of lines of code. In this application, we will use Pandas to read/write our data from/into csv files and to manipulate our data frames based on selected parameters.

Code

Importing libraries

Please proceed by firing up Anaconda or any Python IDE of your choice and installing Sklearn as shown below:

pip install sklearn

Subsequently, import all of the required packages:

import streamlit as st
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
import plotly.express as px
import plotly.graph_objects as go
import plotly.figure_factory as ff
import base64
import os

In our ML app, we will create a logistic regression classifier, where we first upload a training dataset that we will use to train our model with. We will gauge the performance of our hyperparameters by visualizing the confusion matrix and ROC curve. And finally, when we are happy, we will upload an unlabeled test dataset to apply our ML model.

Dataset upload widget

First, we will create a file upload function as shown below:

Hyperparameter selection

Then, we will create multiple widgets to select our feature columns, label column, hyperparameters, and advanced parameters as shown below:

Train & visualize

Now we will use our training dataset and the selected parameters to train our model. Simultaneously we will visualize the confusion matrix, ROC curve, and other metrics to gauge the performance of our model:

Test unlabeled data

And finally, once we are satisfied with the performance of our model, we will upload an unlabeled dataset to test our model. Once the dataset has been labeled using our classifier, we will allow the user to download the data as a CSV file:

Source: Dummy Learn

Running app locally

To run your ML app locally proceed by typing the following commands in Anaconda prompt. First, change your root directory to where your source code is saved:

cd C:/Users/...

Then type the following to run your app:

streamlit run file_name.py

Cloud deployment

And what’s an awesome ML app if you can’t deploy it to the cloud for the world to see? Before you contemplate AWS, Azure, GCP, or Heroku, consider Streamlit itself. With Streamlit’s one-click deployment deploying web apps has never been so simple.

First, push your code to a public GitHub repository and then head over to Streamlit sharing, sign up, and link your GitHub account as follows:

Image by author

Select the relevant repository and script, and then click Deploy!

Image by author

Results

And there you have it, an elegant, highly interactive, cloud deployable free ML app at your service.

Source: Dummy Learn

You can also test the ML App at the following link:

If you want to learn more about data visualization, Python, and app development, then feel free to check out the following (affiliate-linked) courses:

Data Visualization with Python:

Python for Everybody Specialization:

Streamlit Guided Project:

GitHub repository:

The source code for this tutorial can be found in the following GitHub repository.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: