Deploying Machine Learning Models with Streamlit: A Step-by-Step Guide

Introduction

You‘ve spent weeks or months building a machine learning model that solves an important business problem. It has strong performance on your validation data and you‘re excited to put it to use. But how do you make this model accessible to end-users in a friendly way? That‘s where model deployment comes in.

Model deployment refers to integrating a machine learning model into an existing production environment so it can be used to make decisions or predictions based on live input data. Deploying a model enables others to use it and see the value without needing any knowledge of machine learning or how the model works under-the-hood.

While there are many ways to deploy models, using a web framework to create an interactive app is a common approach. The app provides an interface for users to input data and get predictions from the model in real-time. In this post, we‘ll walkthrough how to use the Streamlit framework to build a web app for a machine learning model. Streamlit is a rising star in the world of model deployment – it makes it incredibly quick and easy to create web apps for your models and data projects using simple Python scripts.

By the end of this post, you‘ll be able to:

  • Build a machine learning model for a loan default prediction problem
  • Save and load the trained model
  • Use Streamlit to create an interactive web app for the model
  • Deploy the app and share it with others

Let‘s get started!

The Loan Default Prediction Problem

To illustrate the model deployment process, we‘ll use a real problem that many lending companies face – predicting if a borrower will default on their loan. This is a critical issue for lenders. If they give a loan to someone who ends up not paying it back, the lender loses money. On the other hand, if they deny a loan to someone who would have repaid it, the lender misses out on revenue and the borrower misses out on a financial opportunity.

Machine learning models can analyze historical data on previous borrowers to identify patterns and predict the likelihood that a new potential borrower will repay their loan. Lenders can use these predictions to make more informed decisions about who to give loans to and at what interest rates.

We‘ll use a dataset from Kaggle containing information on past loans and whether the borrower defaulted (failed to repay) or not. The dataset has the following features:

  • Loan_ID: A unique ID for each past loan
  • Gender: Male or Female
  • Married: Yes or No
  • Dependents: Number of dependents (0, 1, 2, 3+)
  • Education: Graduate or Not Graduate
  • Self_Employed: Yes or No
  • ApplicantIncome: The borrower‘s income
  • CoapplicantIncome: The co-borrower‘s income
  • LoanAmount: The loan amount in thousands
  • Loan_Amount_Term: The loan term in months
  • Credit_History: 1 if the borrower has a good credit history, 0 otherwise
  • Property_Area: Urban, Semiurban or Rural
  • Loan_Status: 1 if the loan was repaid (non-default), 0 if it was not repaid (default)

Our goal is to build a model that predicts the Loan_Status for a potential new borrower given their information. We‘ll build this model and create an app that lets a user input a borrower‘s data and get a prediction.

Building the Predictive Model

The first step is to build and train a machine learning model on the historical loan data. We‘ll follow a typical data science workflow:

  1. Import packages and load data
  2. Explore the data
  3. Preprocess the data
  4. Train a model
  5. Evaluate model performance
  6. Save the trained model

We‘ll use Python and common data science libraries like pandas, numpy, matplotlib, and scikit-learn. If you want to follow along, the full code is available on GitHub.

After importing the required libraries, we load the data into a pandas DataFrame and take a look:

Let‘s check for missing values:

Quite a few features have missing values. We‘ll use strategies like filling them in with the median or dropping those rows.

Next, we create some visualizations to understand the data better, like this bar plot showing the counts of defaulted and repaid loans:

We also look at statistics of the numeric features and distributions of the categorical ones.

Before model training, we need to encode the categorical variables as numbers, split the data into features (X) and target (y), and divide into train and test sets.

With the data preprocessed, we‘re ready to train some models. We‘ll try a few common classification algorithms:

  • Logistic Regression
  • Decision Tree
  • Random Forest
  • Gradient Boosting

After fitting each model, we calculate its accuracy on the test set. Here are the results:

The Random Forest model does the best, with about 82% accuracy. Let‘s look at a confusion matrix to see where it makes mistakes:

The model predicts non-defaults (repaid loans) very well, but struggles more with defaults.

With the model built and validated, the final step is to save it to disk so we can load it later in the Streamlit app:

We‘re now ready to create the Streamlit app!

Building the Streamlit App

With the trained model in hand, it‘s time to make it accessible to end users through a web app. We‘ll create a Streamlit app that does the following:

  1. Loads the trained model
  2. Gets input from the user on a potential borrower‘s information
  3. Uses the model to predict if the borrower will repay their loan or default
  4. Displays the prediction to the user

The entire app is contained in one Python script. Let‘s go through it section by section.

First, we import the required libraries, load the saved model, and create a function to take in user input, preprocess it, and return the model‘s prediction:

Next, we create the user interface for the app using Streamlit functions:

This code sets up the title, then creates input fields to get data on the borrower like their gender, marital status, income, loan amount, etc.

Finally, we use Streamlit‘s button() function to display a button that triggers the prediction when clicked:

When the Predict button is clicked, it calls the predict_default() function we defined earlier, passing in the user‘s inputs. The function preprocesses the data, makes a prediction with the model, and returns the result.

The returned prediction is a probability between 0 and 1. We convert it to a more interpretable message saying if the model thinks the borrower will repay the loan or not. This message is displayed to the user with Streamlit‘s success() and warning() functions which show a colored box.

To run the app, we use Streamlit‘s run() function:

That‘s it! The complete app script. We run it with the command:

streamlit run app.py

This will open the app in a new browser tab. Here‘s what it looks like:

The user can input data about a potential borrower and click Predict to see what the model thinks. For example:

The model predicts this borrower is likely to repay the loan since they have a good income, reasonable loan amount, and a credit history.

In contrast:

The model predicts this borrower is likely to default, likely because of their low income and high loan amount relative to that income.

And that‘s it! We‘ve built a loan default predictive model and put an easy-to-use front-end interface on it with Streamlit. This app can now be shared with others in the lending company to inform their decisions about which borrowers to approve.

Advantages and Disadvantages of Streamlit

I really enjoyed using Streamlit to create this app and found it to have some significant strengths:

  • Streamlit is incredibly easy to use. If you know Python, you can have an app built in minutes. The functions for creating input widgets and displaying output are simple and intuitive.

  • No web development knowledge is required. You don‘t need to know any HTML, CSS, or Javascript to get a nice looking and functional app.

  • The ability to write the entire app in a single Python script is very convenient. You don‘t need to create HTML templates or figure out how to connect a Python backend to a Javascript frontend.

  • Streamlit has a surprising amount of customization available, including the ability to create more complex layouts with columns and expanders. Apps can evolve from simple tools to more full-featured dashboards.

However, there are some limitations to be aware of:

  • Streamlit is great for relatively simple apps, but may not be ideal for very complex, multi-page applications. More established web frameworks like Django, Flask, or Dash would likely be better choices there.

  • Creating highly customized layouts or interfaces likely still requires getting into the web code at some level. You won‘t be able to do everything in just Python.

  • Because the app is defined in one script, projects can get messy if you try to do too much. It‘s a good idea to separate data processing, modeling, etc. from the app code as the project grows.

  • Sharing Streamlit apps isn‘t quite as simple as it seems. To put them on the public internet, you‘ll need to host them somewhere, like an AWS EC2 instance, or a service like Streamlit Cloud. We‘ll be walking through deploying Streamlit apps in a future post.

Conclusion

Model deployment is a key final step of the data science process. A model doesn‘t provide value until it‘s in the hands of decision makers. Streamlit is a powerful but simple way to quickly get an ML model out of a Jupyter notebook and into a functional web app that users can interact with. I encourage you to try it out!

Some potential next steps:

  • Try adding more features to the model, like the borrower‘s number of dependents or property area. Does the model performance improve?
  • Can you add visualizations to the app that provide more insight into what factors the model thinks are most important for predicting default?
  • Deploy the app on the cloud so anyone can access it.

The complete code for this project is available on GitHub. Feel free to use it as a starting point for your own Streamlit apps!

Thanks for reading! Leave a comment and let me know what you think. Have you used Streamlit before? What are your favorite tools for model deployment?

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts