How to build a Streamlit UI to Analyze Different Classifiers on the Wine, Iris and Breast Cancer Dataset | by Rahul Banerjee | Towards Data Science

In this tutorial, we will be working with three datasets (Iris, Breast Cancer, Wine)
We will use 3 different models (KNN, SVM, Random Forest) for classification and give the user the ability to set some parameters.
pip install virtualenv  /* Install virtual environment */virtualenv venv         /* Create a virtual environment */venv/Scripts/activate   /* Activate the virtual environment */
Make sure your virtual environment is activated before installing the libraries
pip install streamlit, seaborn, scikit-learn
import streamlit as stfrom sklearn.datasets import load_wine, load_breast_cancer, load_irisfrom  sklearn.neighbors import KNeighborsClassifierfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.svm import SVCfrom sklearn.model_selection import train_test_splitfrom sklearn.decomposition import PCAimport matplotlib.pyplot as pltimport seaborn as snsimport pandas as pd
We import streamlit, the datasets from sklearn, various models from sklearn, libraries needed to make our plots and pandas.
def return_data(dataset):    if dataset == 'Wine':        data = load_wine()    elif dataset == 'Iris':        data = load_iris()    else:        data = load_breast_cancer()    df = pd.DataFrame(data.data, columns=data.feature_names , index=None)    df['Type'] = data.target    X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, random_state=1, test_size=0.2)    return X_train, X_test, y_train, y_test,df,data.target_names
The function takes in a string which contains the name of the dataset the User selects
It loads the relevant dataset
We create a dataframe which we can show in our UI
We use sklearn’s train_test_split() to create the train and testing sets
The function returns the train set, test set, the dataframe and the target classes
We will be using streamlit’s slider component to get the input for the parameters from the user.
st.sidebar.slider(label = ‘’ , min_value = 1, max_value = 100) creates a slider in the sidebar.
def getClassifier(classifier):    if classifier == 'SVM':        c = st.sidebar.slider(label='Chose value of C' , min_value=0.0001, max_value=10.0)        model = SVC(C=c)    elif classifier == 'KNN':        neighbors = st.sidebar.slider(label='Chose 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
Like the previous function, this function takes a parameter which is a string containing the model’s name.
Based on the selected model, we ask the user to give the value of the parameter.
For SVM, we take the C parameter as an input from the user
For KNN, we take the number of nearest neighbours for the model to consider while making its prediction
For Random Forest, we take the number of decision trees and the max_depth of the decision tree
We then create an instance of the model and return the model
def getPCA(df):    pca = PCA(n_components=3)    result = pca.fit_transform(df.loc[:,df.columns != 'Type'])    df['pca-1'] = result[:, 0]    df['pca-2'] = result[:, 1]    df['pca-3'] = result[:, 2]    return df
We use sklearn’s PCA. We add the 3 components to the dataframe and return it.
# Titlest.title("Classifiers in Action")# Descriptionst.text("Chose a Dataset and a Classifier in the sidebar. Input your values and get a prediction")#sidebarsideBar = st.sidebardataset = sideBar.selectbox('Which Dataset do you want to use?',('Wine' , 'Breast Cancer' , 'Iris'))classifier = sideBar.selectbox('Which Classifier do you want to use?',('SVM' , 'KNN' , 'Random Forest'))
We use streamlit’s selectbox component to create a dropdown menu for the user to select the dataset and the model
# Get DataX_train, X_test, y_train, y_test, df , classes= return_data(dataset)st.dataframe(df.sample(n = 5 , random_state = 1))st.subheader("Classes")for idx, value in enumerate(classes):    st.text('{}: {}'.format(idx , value))
We use our helper function to get the data
We use streamlit’s dataframe component to display a sample of our dataset
We also display the classes using the last variable our helper function returns
We will use seaborn and matplotlib to visualize the PCA in 2-D and 3-D.
streamlit’s pyplot component takes in a figure as the parameter and displays the plot in the UI.
# 2-D PCAdf = getPCA(df)fig = plt.figure(figsize=(16,10))sns.scatterplot(    x="pca-1", y="pca-2",    hue="Type",    palette=sns.color_palette("hls", len(classes)),    data=df,    legend="full")plt.xlabel('PCA One')plt.ylabel('PCA Two')plt.title("2-D PCA Visualization")st.pyplot(fig)
#3-D PCAfig2 = plt.figure(figsize=(16,10)).gca(projection='3d')fig2.scatter(    xs=df["pca-1"],    ys=df["pca-2"],    zs=df["pca-3"],    c=df["Type"],)fig2.set_xlabel('pca-one')fig2.set_ylabel('pca-two')fig2.set_zlabel('pca-three')st.pyplot(fig2.get_figure())
Finally, we will train the model and get the train, test accuracy scores.
# Train Modelmodel = getClassifier(classifier)model.fit(X_train, y_train)test_score = round(model.score(X_test, y_test), 2)train_score = round(model.score(X_train, y_train), 2)st.subheader('Train Score: {}'.format(train_score))st.subheader('Test Score: {}'.format(test_score))
You have successfully built a project you can showcase on your portfolio 👏 👏 👏
I recently created a blog using WordPress, I would love it if you check it out 😃
realpythonproject.com
Connect with me on LinkedIn
www.linkedin.com
Connect with me on Twitter