0

I am making a Streamlit application to do experimentation on data using pycaret time series. I created a function to return the experiment and return the information I need, but for some reason I am getting only one of the 2 plots I am returning (out-of-sample). The insample doesn't show!

Here is my code:

from pycaret.time_series import TSForecastingExperiment
import streamlit as st

exp_auto = TSForecastingExperiment()

def run_pycaret(df, target='qty', horizon=12):
    exp_auto.setup(
        data=df, target=target, fh=horizon, 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)
    best = exp_auto.compare_models(sort="mape", turbo=False, verbose=False)
    metrics = exp_auto.pull()
    insample = exp_auto.plot_model(best, plot='insample',display_format='streamlit')
    outsample = exp_auto.plot_model(best, plot='forecast', display_format='streamlit')
    return best, metrics, insample, outsample


if st.button("Run Time series models:"):
    best, metrics, insample, outsample = run_pycaret(df_exog, horizon=steps)

    st.write(metrics)
    # Plot graph
    col1_ts_exp, col2_ts_exp = st.columns(2)
    with col1_ts_exp:
        insample
    with col2_ts_exp:
        outsample

The results: enter image description here

Sam.H
  • 193
  • 2
  • 14

1 Answers1

1

Instead of creating the plots inside the run_pycaret function (as you do with insample = exp_auto.plot_model(best, plot='insample',display_format='streamlit'), I suggest you return exp_auto and then plot "first time" inside the with col1_ts_exp:. Like this:

from pycaret.time_series import TSForecastingExperiment
import streamlit as st

exp_auto = TSForecastingExperiment()

def run_pycaret(df, target='qty', horizon=12):
    exp_auto.setup(
        data=df, target=target, fh=horizon, 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)
    best = exp_auto.compare_models(sort="mape", turbo=False, verbose=False)
    metrics = exp_auto.pull()
    # Here, return exp_auto instead of the plots
    return best, metrics, exp_auto


if st.button("Run Time series models:"):
    best, metrics, exp_auto = run_pycaret(df_exog, horizon=steps)

    st.write(metrics)
    # Plot graph
    col1_ts_exp, col2_ts_exp = st.columns(2)

    # Plot here directly
    with col1_ts_exp:
        exp_auto.plot_model(best, plot='insample', display_format='streamlit')
    with col2_ts_exp:
        exp_auto.plot_model(best, plot='forecast', display_format='streamlit')

I tried a similar code (could not use yours since I don't have df etc.) and it now displays the 2 graphs correctly:

import streamlit as st
from pycaret.datasets import get_data
from pycaret.classification import ClassificationExperiment

def run_pycaret(data):
    exp_auto = ClassificationExperiment()
    exp_auto.setup(data, target = "Class variable", session_id=123)

    best = exp_auto.compare_models()

    exp_auto.evaluate_model(best)
    # Here, return "exp_auto" instead of the plots
    return best, exp_auto

data = get_data("diabetes")

if st.button("Run Time series models:"):
    
    best, exp_auto = run_pycaret(data)

    st.write(f"Best is {best}")

    col1_ts_exp, col2_ts_exp = st.columns(2)
    with col1_ts_exp:
        st.write("Begin Col 1")
        exp_auto.plot_model(best, plot="auc", display_format="streamlit")
        st.write("End Col 1")
    with col2_ts_exp:
        st.write("Begin Col 2")
        exp_auto.plot_model(best, plot="confusion_matrix", display_format="streamlit")
        st.write("End Col 2")

and it works:

The 2 graphs are displayed correctly each in its own column

vvvvv
  • 25,404
  • 19
  • 49
  • 81
  • that for ur help. I still don't get the second chart. I actually tried before to get out the exp and then pull the charts, never worked with me ( I added picture to question to show end results) – Sam.H Apr 06 '23 at 08:13
  • Have you tried your code outside streamlit? – vvvvv Apr 06 '23 at 09:11
  • yes I did, it works fine and normal on Jupyter, but just when I want to convert it to streamlit app i get these errors – Sam.H Apr 08 '23 at 19:37