Search code examples
python-3.xstreamlitpycaret

Unable to capture or show the compare_model() metrics in streamlit


I have created streamlit app to allow me to run multiple experiments on a dataset using PyCaret Time series. My problem is that I am unable to capture and show the compare_model() results table in my streamlit app, (the metrics table that shows the performance in all tested algorithms).

Here is my code:

    import streamlit as st
    from pycaret.time_series import TSForecastingExperiment
    from pycaret.time_series import *

    # set up for time series forecasting
    exp_auto = TSForecastingExperiment()
    exp_auto.setup(
        data=df_exog, target='qty', fh=fh, enforce_exogenous=False,
        numeric_imputation_target="ffill", numeric_imputation_exogenous="ffill",
        scale_target="minmax",
        scale_exogenous="minmax",
        fold_strategy="expanding",
        session_id=42,verbose=False)
    
    
    
    if st.button("Run Time series models:"):
    
        best = exp_auto.compare_models(sort="mape", turbo=False,verbose=True)
        st.write(best)

I can not get the metrics table to print out, i keep getting the setup table results instead of the compare_model() results, although I tried verbose=False in the setup()


Solution

  • By default compare_models() didn't return results as DataFrame instead it returned the trained model. If you want the metrics table try to use pull() to get the last printed score grid and then you can display it on streamlit.

    from pycaret.datasets import get_data
    from pycaret.time_series import TSForecastingExperiment
    import streamlit as st
    
    # Get data
    y = get_data('airline', verbose=False)
    
    # Experiment
    exp = TSForecastingExperiment()
    exp.setup(data=y, fh=12, verbose=False)
    
    if st.button("Run Time series models:"):
        best = exp.compare_models(include=['arima','exp_smooth','ets'], verbose=False)
        # Pull metrics
        metrics = exp.pull()
        st.write(metrics)
        
        # Plot graph
        exp.plot_model(estimator=best, display_format='streamlit')
    

    demo