Skip to main content

ssmdkmeans (Python)

SSMD k-means: discovering separable subgroups and their number

Author: Xiaohua Douglas Zhang (Department of Biostatistics, College of Public Health, University of Kentucky; douglas.zhang@uky.edu) · Version 1.1.0 · December 1, 2026 · License: MIT

SSMD k-means clusters data so that every pair of subgroups is separated by an optimal linear score, measured by the strictly standardized mean difference (SSMD). It can be used in two ways:

  • Path 1: form a preset number of subgroups and report which pairs can be discriminated.
  • Path 2: find the largest number of subgroups whose every pair reaches a preset SSMD c, such as 3, with K̂ = max{K : J(K) ≥ c}. When no partition qualifies, it concludes that there are no subgroups.

J(K) is the minimum over pairs of clusters of the maximized SSMD. It is compared with a no-cluster reference distribution, simulated with the same algorithm for the data set's own n and p.

This package applies the method to your own data set and writes a figure and tables. Only SSMD k-means is run; no other clustering method is used.

Zhang, X. D. SSMD k-Means: Discovering Separable Subgroups and Their Number in Precision Medicine.

Installation

pip install ssmdkmeans                  # from PyPI, once available
pip install ./ssmdkmeans                # from this folder
pip install "./ssmdkmeans[all]"         # with numba (faster refinement, identical results) and openpyxl (Excel)

Requires Python ≥ 3.9 with numpy, scipy, pandas, scikit-learn, matplotlib and joblib.

Quick start

import pandas as pd
from ssmdkmeans import ssmd_report

df = pd.read_csv("my_data.csv")

# (1) data with known groups: figure like Figures 4 and 5 of the paper, and tables like Tables 3-5
rep = ssmd_report(df.drop(columns="group"), df["group"], name="My data", outdir="my_report")

# (2) data without known groups: figure like Figures 7 and 8
rep = ssmd_report(df, name="My data", outdir="my_report")
rep = ssmd_report(df, name="My data", K_preset=3)            # also show Path 1 with 3 subgroups
rep = ssmd_report(df, name="My data", external=markers)      # plus variables not used in forming the subgroups (Figure 8D)

print(rep)                    # summary of Path 1 and Path 2
rep.tables["path2"]           # every table is a pandas DataFrame
rep.figure                    # the matplotlib figure

With outdir given, the figure, tables and summary are written to that folder. Without it (the default), nothing is written and the results are returned in rep. Random numbers come from local generators, so the global NumPy random state is never changed.

From the command line:

ssmdkmeans-report my_data.csv --groups group --name "My data" --jobs -1
ssmdkmeans-report my_data.csv --external ESR1 PGR KRT5 --kpreset 3

There are two worked examples, which use data from the paper: examples/example_known_groups.py (dermatology data) and examples/example_no_known_groups.py (diabetes data). load_example() loads these data sets.

The figure (<name>_figure.png and .pdf)

Data with known groups (Figures 4, 5) Data without known groups (Figures 7, 8)
A First two principal components, coloured by known group First two principal components, coloured by Path 2 subgroup (grey if none)
B J(K) with the no-cluster reference (mean ± 2 SD), the threshold c, Path 1 (square) and Path 2 (circle) Same
C Maximized SSMD between every pair of known groups (orange: below c) The Path 2 subgroups on their maximized-SSMD direction; the closest pair when K̂ ≥ 3
D, E, … Consecutive pairs of the Path 2 subgroups (1 vs 2, 2 vs 3, …), each on its maximized-SSMD direction. If K̂ = 1, the Path 1 subgroups are shown instead. Consecutive pairs of the Path 2 subgroups when K̂ ≥ 3. If K̂ = 1 and K_preset is given, the Path 1 subgroups are shown instead.
last Variables given in external, by subgroup (box plots) Same (as Figure 8D)

Subgroups are numbered by the known group to which most of their members belong, then by size. Without known groups, they are numbered by size. In the pair panels, colour shows the subgroup and marker shape shows the known group. The horizontal axis is scaled so that the distance between the two subgroup means equals their SSMD.

The tables (<name>_<table>.csv, and all in <name>_tables.xlsx)

Table Contents
summary n, p, the number of known groups, c, the Path 1 K and its J, K̂ and its minimum SSMD, and the 95th percentile of maxK J(K) in the no-cluster reference
known_groups Separation of the known groups (as Table 3): the minimum maximized SSMD, the closest pair, AUC = Φ(min SSMD) and the 10-fold cross-validated minimum SSMD
known_ssmd_matrix Maximized SSMD between every pair of known groups (panel C)
J_path For each K: J(K), whether it reaches c, the cluster sizes, and the reference mean, SD and 95th percentile. With known groups, also ARI and homogeneity.
path1 Path 1 with K preset (as Table 4, SSMD k-means only): J(K), the sizes, ARI, homogeneity, and the SSMD between the known groups computed in the same way
path2 Path 2 (as Table 5, SSMD k-means only): K̂, the minimum pairwise SSMD, the sizes, and (with known groups) ARI and homogeneity
pairwise_ssmd_path1, pairwise_ssmd_path2 Maximized SSMD between every pair of subgroups
crosstab_path1, crosstab_path2 Subgroups by known groups
cluster_profile Mean (SD) of each variable by subgroup
external_by_cluster Summary of each external variable by subgroup, with the Kruskal–Wallis p-value
assignments Known group and subgroup of every observation, for every K
summary.txt Text summary

Main options of ssmd_report

Option Default Meaning
K_max 8 The largest K examined. If K̂ = K_max, increase it (Section 2.4 of the paper).
c 3 SSMD threshold of Path 2. An SSMD of 3 corresponds to an AUC of 0.999 for two normal subgroups.
K_preset number of known groups K for Path 1
n_init 30 Starts in each of the four affine-invariant coordinate systems (120 starts in all)
B 60 Simulated no-cluster data sets for the reference; 0 skips it. This is the slowest step: use n_jobs=-1.
pca None Form the subgroups from the first pca principal components. Every subgroup needs more than p observations, so use this when p is large relative to n.
pca_standardize True Standardize the variables before computing principal components (for pca and for panel A)
short_names, group_order Labels and order of the known groups in the figure
external, external_scale Variables not used in forming the subgroups, shown on their own scale ("raw") or standardized ("z")
outdir, formats, dpi None (no files), png + pdf, 300 Output

Lower-level functions are ssmd_kmeans(X, K), ssmd_path(X, K_max, c), pairwise_ssmd(X, labels), null_reference(n, p) and known_group_separation(X, y).

Rows with missing values are dropped.

Running time. On a laptop, SSMD k-means for K = 2 to 8 takes about 20–30 seconds for 366 observations with p = 5. Each simulated reference data set takes about as long, and n_jobs spreads them across cores.

Example data

  • dermatology: UCI Machine Learning Repository (https://archive.ics.uci.edu/dataset/33/dermatology), CC BY 4.0; Guvenir, Demiroz and Ilter (1998), Artificial Intelligence in Medicine, 13, 147–165.
  • diabetes: Efron, Hastie, Johnstone and Tibshirani (2004), The Annals of Statistics, 32, 407–499.

Tests

pip install "./ssmdkmeans[test]"
pytest ssmdkmeans/tests

Metadata

Release files for ssmdkmeans 1.1.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 ssmdkmeans 1.1.0
File Size Uploaded Provenance Actions
ssmdkmeans-1.1.0.tar.gz 56.1 kB Without provenance Details for ssmdkmeans-1.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 107.8 kB

Release files / ssmdkmeans-1.1.0.tar.gz

Download URL ssmdkmeans-1.1.0.tar.gz
Size 56.1 kB
Tags Source
SHA-256 checksum
How to use checksums
eae86c5ecc6c595ecc37a1bd53423293fee881378d35bb4ed1737fff0fc49e82
BLAKE2b-256 checksum
How to use checksums
89ad5f7da5b59ad03123a85913b97fb785b096cf0f2a4f263e263bcc1dfd29da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / ssmdkmeans-1.1.0-py3-none-any.whl

Download URL ssmdkmeans-1.1.0-py3-none-any.whl
Size 51.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3984f3502e21346638fbfd67e2d87fcb8523f1cd217740df11f9c1e0b4d20641
BLAKE2b-256 checksum
How to use checksums
252a54fa7ce3e163445266db9ac752a38c7016d5909ce1bde01b0110b211f146
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for ssmdkmeans 1.1.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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

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