Skip to main content

Toplot

Reference docs

Visualizations for topic models.

Installation

pip3 install toplot

Getting started

Topic modelling is a Bayesian endevour. After training your topic model with $K$ components, you've inferred the distribution over two latent variables:

  • The posterior over the weights (i.e., the topics) of the model $\pmb{W} = [\pmb{w}_1, \dots, \pmb{w}_K]^T$. We assume that the weights have a two-level structure: each weight is composed of categorical variables (or actually, multinomials), each consisting of a set of categories.
  • Per training example $i$, the posterior over the hidden units $\pmb{h}^{(i)}$ (topic loadings, also denoted as $\pmb{\theta}_i$ in LDA).

Visualizing weights (the topic/cluster, $\pmb{w}$ or $\pmb{\phi}$)

Toplot expects your topic model's posterior samples to be organized in specific ways. As an example, we draw 1000 samples from "fake" topic weights $\pmb{W}$ containing two categories, body mass index (BMI) and sex, consisting of three and two categories each, respectively.

import pandas as pd
from numpy.random import dirichlet

# Draw 1000 samples from "posterior" distribution.
weight_bmi = dirichlet([16.0, 32.0, 32.0], size=1_000)
weight_sex = dirichlet([8.1, 4.1], size=1_000)
weight = pd.concat(
    {
        "BMI": pd.DataFrame(
            weight_bmi, columns=["Underweight", "Healthy Weight", "Overweight"]
        ),
        "sex": pd.DataFrame(weight_sex, columns=["Male", "Female"]),
    },
    axis="columns",
)

Use bar_plot to visualize the topic weight, including the 95% quantile range:

Visualization of topic weights with bar_plot.

from toplot import bar_plot

bar_plot(weight)

If you have many multinomials, you can use bar_plot_stacked to reduce the width of the plot. This plot folds the categories (e.g., "Underweight", "Healthy Weight", and "Overweight") belonging to the same multinomial (BMI) into a single bar.

Visualization of topic weights with bar_plot_stacked.

from toplot import bar_plot_stacked

bar_plot_stacked(weight)

To visualize more than one topic at a time, you can make a scattermap with scattermap.

Visualizing hidden units (topic proportions, $\pmb{h}$ or $\pmb{\theta}$)

Next, we plot the hidden units/topic identities $[\pmb{h}^{(1)}, \dots, \pmb{h}^{(m)}]^T$: that is, for each record $i$, the proportion over the components/topics. Let's generate the (average) proportion for $m=30$ records to visualize:

hidden = pd.DataFrame(
    dirichlet([0.6, 0.8, 0.2], size=30),  # 30 records
    columns=["Topic_1", "Topic_2", "Topic_3"],
)

The function plot_cohort computes the distance between all examples (the cohort) and, by default, sorts them accordingly using the travelling salesman problem. Currently, no uncertainty visualization is supported for plot_cohort (like in bar_plot), so you need to pass the posterior average. Visualization of hidden units, or topic identities, with plot_cohort

from toplot import plot_cohort

plot_cohort(hidden)

You can emphasize the periodicity inherent in the travelling salesman solution by visualizing all the examples using a polar plot:

Visualization of hidden units, or topic identities, emphasizing the periodicity with plot_polar_cohort

from toplot import plot_polar_cohort

plot_polar_cohort(hidden)

Metadata

Release files for toplot 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for toplot 1.2.0
File Size Uploaded Provenance
toplot-1.2.0.tar.gz 85.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for toplot 1.2.0
File Interpreter ABI Platform
toplot-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 101.2 kB

Release files / toplot-1.2.0.tar.gz

Download URL toplot-1.2.0.tar.gz
Size 85.1 kB
Tags Source
SHA-256 checksum
How to use checksums
51b193a5b8d29e8649376a8bb34b4ba42740eb6273c0b742f86731854d6d88de
BLAKE2b-256 checksum
How to use checksums
03acfb55c88d3dc5b3b2f76ed6fd6413042e542035d19c4da9d388238855a589
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / toplot-1.2.0-py3-none-any.whl

Download URL toplot-1.2.0-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d51da8416cef07d68b931ebe6bf6f16300746e9c7e418373bafe428aebd6d514
BLAKE2b-256 checksum
How to use checksums
daad01a2874e4b1b1e38e381c12d1b2548e904395c4b8fcb7673d77b101c7297
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for toplot 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page