Classification Score Visualization


Hello Community!
So, I want to show the classification_score from sklearn in the streamlit project I have with expectation the score will show as neatly arranged as in Jupyter notebook. But, it seems the behavior isn’t the same, leading the arrangement become odd and put out of the place.

I have tried several ways to re-arrange the placement namely : with st.column, st.expaner, etc but sadly to no avail.

I was wondering if anyone had the same problem previously and found the right way to do the arrangement so the score become neatly arrange like in the Jupyter notebook?
Thank you in advance.

Expected behavior:

Actual behavior:

Steps to reproduce

Code snippet:

        st.markdown("<br>", unsafe_allow_html=True)

        if "scaled_data_train" in st.session_state:
            # Setting the upload variabel
            uploaded_file = st.file_uploader("Choose a file to upload for training data",
                                             help="The file will be used for training the Machine Learning",

            # Setting the upload options when there's file on uploader menu
            if uploaded_file is not None:
                    # Uploading Dataframe
                    dataframe = get_data(uploaded_file)

                    X = dataframe.drop(columns="Outcome")
                    y = dataframe["Outcome"]

                    # Storing dataframe to session state
                    if 'X' not in st.session_state:
                        st.session_state["X"] = X

                    if 'y' not in st.session_state:
                        st.session_state["y"] = y

                    st.write("Please upload any data")

        # Markdown to gice space
        st.markdown("<br>", unsafe_allow_html=True)
        st.markdown("<br>", unsafe_allow_html=True)
        st.markdown("<h3 style='text-align: center; color: cyan;'>Model Setting</h3>",
        st.markdown("<br>", unsafe_allow_html=True)

        # Selecting Model for Classification
        model_selection = st.selectbox(
            "Select Machine Learning Model for Classification Task",
            ("Logistic Regression", "Random Forest")

        st.write("Model selected:", model_selection)

        # Setting Logistic Regression Model
        if model_selection == "Logistic Regression":

            col1, col2, col3 = st.columns(3)

            with col1:
                # Setting Logistic Regression Penalty
                log_res_penalty =
                    "Norm of the penalty",
                    ('l1', 'l2', 'none'))

            with col2:
                # Setting Logistis Regression Solver
                log_res_solver =
                    "Algorithm optimization",
                    ("lbfgs", "liblinear", "newton-cg", "newton-cholesky", "sag", "saga"

            with col3:
                # Inverse of regularization strength
                log_res_inverse = st.number_input(
                    "Inverse of regularization",

            # Logistic Regression Object
            log_res_obj = LogisticRegression(
                penalty=log_res_penalty, C=log_res_inverse, solver=log_res_solver)

            # Fitting Data to Logistic Regression Model
            if st.button("Fit Data to Logistic Re Model"):

                # Initiating variable to fir data
                X_train = st.session_state.scaled_data_train
                X_test = st.session_state.scaled_data_test
                y_train = st.session_state.y_train
                y_test = st.session_state.y_test

                # Fitting model to data
      , y_train)

                st.write("Training Success")

                # Predicting train data
                y_train_predict = log_res_obj.predict(X_train)
                y_train_predict_df = pd.DataFrame(y_train_predict)

                # Predicting test data
                y_test_predict = log_res_obj.predict(X_test)
                y_test_predict_df = pd.DataFrame(y_train_predict)

                # Predicting F1 score
                classification_report_train = classification_report(
                    y_train, y_train_predict, labels=[0, 1])
                classification_report_test = classification_report(
                    y_test, y_test_predict, labels=[0, 1])

Hi @little_death ,

Your code snippet is not reproducible so it’s difficult to propose a solution (some functions are not defined within the snippet, and libraries used are missing).

In any case, check the type of the classification_score and try converting it to a pandas DataFrame (or two DataFrames, one for training score and one for dev score). Then you can use Streamlit’s st.dataframe() method.

Hope this helps!

The output of sklearn.metrics.classification_report is a string by default, but it can be set as a dictionary with the kwarg output_dict=True. As @marduk pointed out, that is rendered nicely with a st.dataframe().


import streamlit as st
from sklearn.metrics import classification_report

y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]

target_names = ["class 0", "class 1", "class 2"]

    classification_report(y_true, y_pred, target_names=target_names, output_dict=True)

Edit: I just realized the output is flipped, but that can be fixed transposing the dataframe.


import streamlit as st
from sklearn.metrics import classification_report
import pandas as pd

y_true = [0, 1, 2, 2, 2]
y_pred = [0, 0, 2, 2, 1]
target_names = ["class 0", "class 1", "class 2"]

        classification_report(y_true, y_pred, target_names=target_names, output_dict=True)

Thank you sir @marduk @edsaac.
The problem solved!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.