Skip to main content

tgmm: Gaussian Mixture Models in PyTorch

PyPI version Documentation Python 3.8+ PyTorch

tgmm Logo

tgmm is a flexible, GPU-accelerated implementation of Gaussian Mixture Models (GMM) in PyTorch, supporting EM and MAP estimation, multiple covariance types, several initialization strategies, and a comprehensive suite of clustering metrics and visualization tools.

Installation

Install the latest stable version from PyPI:

pip install tgmm

For development installation:

git clone https://github.com/adriansousapoza/TorchGMM.git
cd TorchGMM
pip install -e .

Requirements: Python 3.8+ and PyTorch 2.5+. For GPU usage, install the CUDA-enabled version of PyTorch as per the official instructions.

Documentation

Full documentation is hosted at adriansousapoza.github.io/tgmm:

Quick Start

import torch
import numpy as np
from tgmm import GaussianMixture

# Generate sample data
np.random.seed(42)
X = np.vstack([
    np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 1]], 300),
    np.random.multivariate_normal([3, 3], [[1, -0.3], [-0.3, 1]], 300),
    np.random.multivariate_normal([-2, 2], [[0.5, 0], [0, 2]], 200)
])
X_tensor = torch.tensor(X, dtype=torch.float32)

# Create and fit GMM
gmm = GaussianMixture(n_components=3, random_state=42)
gmm.fit(X_tensor)

# Make predictions
labels = gmm.predict(X_tensor)
print(f"Converged: {gmm.converged_}, Iterations: {gmm.n_iter_}")

See the Tutorials for runnable, end-to-end examples of every feature -- covariance types, all initialization strategies, MAP estimation with priors, Classification EM, constrained sampling, save/load, and more.

Key Features

1. Gaussian Mixture Model

The core GaussianMixture class supports:

  • Covariance Types: 'full', 'diag', 'spherical', 'tied_full', 'tied_diag', 'tied_spherical'
  • Estimation Methods: MLE (Maximum Likelihood) or MAP (Maximum A Posteriori) with priors
  • Algorithms: Standard EM or CEM (Classification EM) for hard assignments
  • Initialization: Multiple strategies via GMMInitializer

2. Bayesian Inference with Priors

Support for conjugate priors enables proper Bayesian inference:

  • Weight Prior: Dirichlet distribution
  • Mean Prior: Gaussian distribution
  • Covariance Prior: Wishart/Inverse-Wishart distribution
  • NIW Conjugate Prior: Normal-Inverse-Wishart for joint mean-covariance updates

3. Clustering Metrics

Comprehensive evaluation with ClusteringMetrics:

Unsupervised Metrics (no ground truth needed):

  • Silhouette Score, Davies-Bouldin Index, Calinski-Harabasz Index, Dunn Index, BIC, AIC

Supervised Metrics (with ground truth labels):

  • Adjusted Rand Index (ARI), Normalized/Adjusted Mutual Information, Purity
  • Confusion Matrix and per-class Classification Report (precision, recall, F1, ROC-AUC)

4. Visualization Tools

Flexible plotting utilities in tgmm.plotting:

  • Component ellipses at multiple confidence levels
  • Cluster coloring, ground-truth comparison, and log-likelihood coloring
  • Initial vs. final mean trajectories and weight-scaled markers

Citation

TBA

License

Released under the MIT License. © 2025, Adrián A. Sousa-Poza

Contributing

tgmm isn't yet set up to accept external contributions. If you're interested in contributing, please reach out to asp@di.ku.dk.

Metadata

Release files for tgmm 0.2.1

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

Source distribution (sdist)

Source distribution for tgmm 0.2.1
File Size Uploaded
tgmm-0.2.1.tar.gz 48.4 kB Details

Built distribution (wheel)

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

Total release size: 91.9 kB

Release files / tgmm-0.2.1.tar.gz

Download URL tgmm-0.2.1.tar.gz
Size 48.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ec750893ae203e5b6c44170fb35bac323410578dae419e327d47eb35164ea720
BLAKE2b-256 checksum
How to use checksums
4b4ceff611416f6a4b7e433b537b3d128132e0969cc799f456f67efb3b75bdd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / tgmm-0.2.1-py3-none-any.whl

Download URL tgmm-0.2.1-py3-none-any.whl
Size 43.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7716d6904f8c00ba3285f3af2c8908a2ecf0fb4a0aa9567c7fb64fcde2e582da
BLAKE2b-256 checksum
How to use checksums
1f8c07a748c760b6f38ec23aaaf92606ecb3abe3e3f3a6840b0536745fe7431a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

0.4.0

2 release files

0.3.0

2 release files

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.7

2 release files

0.1.5

2 release files

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