Get startedGet started for free

Visualizing forecast results

After defining and training models using backtesting, it's time to visualize the results. Visualization is a quick and effective way to assess model performance across partitions.

The ts and bkt_df DataFrames from previous exercises, along with the Plotly library, have already been preloaded for you. Let's explore how well our models performed!

This exercise is part of the course

Designing Forecasting Pipelines for Production

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Get unique partition labels from the backtesting DataFrame
partitions_labels = bkt_df["cutoff"].unique()

# Filter the time-series data to include only the last 7 days
ts_sub = ts[ts["ds"] > ts["ds"].max() - datetime.timedelta(hours=24 * 7)]

# Create subplots with 4 rows (one for each partition)
fig = make_subplots(rows=4, cols=1, subplot_titles=["Partitions: " + str(i) for i in partitions_labels])

r = 1  # Row counter for subplots

for i in partitions_labels:
    # Show legend only for the first subplot
    if r == 1:
        showlegend = True
    else:
        showlegend = False
    # Filter backtesting results for the current partition
    bkt_sub = bkt_df[bkt_df["cutoff"] == i]
    # Add actual values to the plot
    fig.append_trace(go.Scatter(x=ts_sub["ds"], y=ts_sub["y"], legendgroup="actual", showlegend=showlegend, 
                                mode='lines', name='Actual', line=dict(color='#023047', width=2)), row=r, col=1)
    # Add k-nearest neighbors predictions to the plot
    fig.append_trace(go.Scatter(x=bkt_sub["ds"], y=bkt_sub["knn"], mode='lines', name='k-nearest neighbors', 
                                legendgroup="knn", showlegend=showlegend, line=dict(color='#2a9d8f', width=1.5, dash="dash")), row=r, col=1)
    # Add Multi-layer Perceptron predictions to the plot
    fig.append_trace(go.Scatter(x=bkt_sub["ds"], y=bkt_sub["mlp"], mode='lines', name='Multi-layer Perceptron', 
                                legendgroup="mlp", showlegend=showlegend, line=dict(color='#0077b6', width=1.5, dash="dot")), row=r, col=1)
    # Add ElasticNet predictions to the plot
    fig.append_trace(go.Scatter(x=bkt_sub["ds"], y=bkt_sub["enet"], mode='lines', name='ElasticNet', 
                                legendgroup="enet", showlegend=showlegend, line=dict(color='#ffc8dd', width=1.5, dash="dot")), row=r, col=1)
    r = r + 1  # Move to the next row

# Adjust the figure layout and display it
fig.update_layout(height=500)
fig.show()
Edit and Run Code