Learn to make interactive, beautiful machine learning app using Streamlit :)

What you’ll learn:

  1. Brief introduction about Streamlit
  2. Installation procedure
  3. Basic examples using Streamlit
  4. How to save machine learning model?
  5. Iris flower classification app
  6. Step by step code explanation + video demo :)

Hi everyone :)

Recently I participated in a webinar of learning about Streamlitin my local community and thought, let’s make a tutorial on it and share it with the open source community :)

As a beginner, I believe we all want to make cool stuff using machine learning as quickly as possible!

Developers of Streamlit have created a very simple way for us :)


Before wasting any time let’s get started.

Image for post

What is Streamlit?

Streamlit is an open-source framework to create an interactive, beautiful visualization app. All in python!

Streamlit provides many useful features that can be very helpful in making visualizations for data-driven projects.

Why should I use Streamlit?

  • Simple and easy way to create an interactive user interface
  • Requires zero development experience
  • It’s fun making use of different function in your data-driven projects :)
  • Comprehensive documentation

Face-GAN explorer using Streamlit

Image for post

This Streamlit app demonstrates


Installation Procedure

For Linux

You just need to write below command in your terminal to install Streamlit

pip install streamlit

That’s all :)

For Windows

You can find installation instructions for windows :

Install streamlit for windows

Now let’s explore Streamlit

Drawing content

Type below lines of code and save it as .py extension

import streamlit as st 
x = 4
st.write(x, 'squared is', x * x)

This is how you run the python script using Streamlit:

Open a terminal and make sure you are in the same working directory in which file is saved.

Type below command and press enter:

streamlit run filename.py

Output:

Image for post

This is what you will see in your default web browser

Okay now let’s make it more interactive by including slider widget in our code:

x = st.slider('x')  # 👈 this is a widget
#st.write(x, 'squared is', x * x)

Output:

Image for post

Image for post

Using Slider Widget in Streamlit

Notice: Whenever you modify code, you can see rapid changes in your project.


Okay now let’s get back to our main objective!

Image for post

Iris Classification App

Problem statement: classifying Iris flower species from its features.

Iris features: Sepal, Petal, lengths, and widths

This classification problem is also known as the **Hello World **of supervised machine learning!

First, let’s explore iris dataset:

from sklearn.datasets import load_iris

iris= load_iris()
# Store features matrix in X
X= iris.data
#Store target vector in 
y= iris.target

Features

# Names of features/columns in iris dataset
print(iris.feature_names)

Output : ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']

Target/Iris species

# Names of target/output in iris dataset
print(iris.target_names)

Output : ['setosa' 'versicolor' 'virginica']

Image for post

Types of Iris flower

Dimensions/Size of Iris dataset

# size of feature matrix
print(iris.data.shape)

Output: (150, 4)
# size of target vector
print(iris.target.shape)
Output : (150,)

So, we have 150 rows/observations and 4 columns/features.

Next step is to work on machine learning algorithm :)

Just to get started we will use K Nearest Neighbour.

KNN is like:

“ _Show me who your friends are, and I will tell you what you are _”

Image for post

Following are KNN steps:

Give new data:

  1. Compute the distance between new data and each observation/row in a dataset
  2. Get the k number of observations that are nearest to new data

Nearest: Observations with minimum distances (Ascending order)

3. Get the output labels of k number of observations and classify new data based on these most represented/popular output labels

Let’s apply KNN using Scikit learn library!

#beginner #application #how-to #streamlit #deep learning

 Let’s make an interactive Iris flower classification app using Streamlit
6.80 GEEK