Search⌘ K
AI Features

Solution: Plot Evaluation Metrics using Streamlit

Explore how to implement and display classification evaluation metrics within a Streamlit web app. This lesson guides you through adding multiselect options for metrics, plotting confusion matrices, ROC curves, and precision-recall curves for classifiers using Scikit-learn. Understand how to enhance interactive data visualization to evaluate model performance effectively.

Solution for Task 1

In Task 1, you were asked to add multiselect on the left sidebar on the Streamlit interface.

Let’s run the code below.

import streamlit as st
from  sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC

def getClassifier(classifier):
    if classifier == 'SVM':
        c = st.sidebar.slider(label='Choose value of C' , min_value=0.0001, max_value=10.0)
        model = SVC(C=c)
    elif classifier == 'KNN':
        neighbors = st.sidebar.slider(label='Choose Number of Neighbors',min_value=1,max_value=20)
        model = KNeighborsClassifier(n_neighbors = neighbors)
    else:
        max_depth = st.sidebar.slider('max_depth', 2, 10)
        n_estimators = st.sidebar.slider('n_estimators', 1, 100)
        model = RandomForestClassifier(max_depth = max_depth , n_estimators= n_estimators,random_state= 1)
    return model


st.title("Classifiers in Action")

# Description
st.text("Breast Cancer Dataset")

#sidebar
sideBar = st.sidebar
classifier = sideBar.selectbox('Which Classifier do you want to use?',('SVM' , 'KNN' , 'Random Forest'))
getClassifier(classifier)
# The below line will create a multiselect option for evaluation metrics.

metrics = st.sidebar.multiselect("What metrics to plot?", ("Confusion Matrix", "ROC Curve", "Precision-Recall Curve"))


Displaying multiselect options for evaluation metrics

Explanation

  • Lines 1–4: We import the required modules.

  • Lines 6–17: We implement the getClassifier() function.

  • Line 20: We add the title on the interface.

  • Line 23: We add text on the interface.

  • Lines 26–27: We create a sidebar and add a selectbox for the classifier’s selection.

  • ...