Learn to make interactive, beautiful machine learning app using Streamlit :)
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.
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.
This Streamlit app demonstrates
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 :
Now let’s explore Streamlit
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:
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:
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!
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']
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 _”
Following are KNN steps:
Give 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