PCA Index Dashboard Examples

PCA Index Dashboard Examples#

import pull_fred
import pandas as pd
import plotly.express as px

import pca_index
from settings import config

DATA_DIR = config("DATA_DIR")

import warnings

warnings.filterwarnings("ignore")
/home/runner/work/example-updating-dashboard/example-updating-dashboard/.venv/lib/python3.10/site-packages/requests/__init__.py:113: RequestsDependencyWarning: urllib3 (2.6.3) or chardet (6.0.0.post1)/charset_normalizer (3.4.4) doesn't match a supported version!
  warnings.warn(
now = pd.Timestamp.utcnow()
# convert now to central time
print(f"This script was last run at {now} (UTC)")
print(f"In US/Central Time, this is {now.tz_convert('US/Central')}")
This script was last run at 2026-03-14 10:04:10.663360+00:00 (UTC)
In US/Central Time, this is 2026-03-14 05:04:10.663360-05:00
df = pull_fred.load_fred(data_dir=DATA_DIR)
dfn = pca_index.transform_series(df)
## Visualize Principal Component 1
pc1, loadings = pca_index.pca(dfn, module="scikitlearn")
pc1.plot();
../../_images/ed75453cbcd2d22393930d0a5b5ff68baa9647dfeaf9be9794a51833505ad003.png
# Simple version
fig = px.line(pc1)
fig.show()
# Using slider and quick views
fig = pca_index.pc1_line_plot(pc1)
fig.show()
## Visualize normalized and raw series
dfn.plot(subplots=True, figsize=(10, 10));
../../_images/2424d7524369a022cc6c7f86959e8aa5352c301afbecf5ebfd041c343eab7ca8.png
fig = px.line(dfn, facet_col="variable", facet_col_wrap=1)
fig.update_yaxes(matches=None)
fig.show()
fig = pca_index.plot_unnormalized_series(df)
fig.show()
fig = pca_index.plot_normalized_series(dfn)
fig.show()