This release is a pre-release and may not be stable for production use.
datanuggets
Data nuggets bring a large dataset down to a size you can explore with weighted clustering and PCA. This package implements the method in Python, following the R packages datanugget and WCluster.
A million observations can be hard to work with even when there are only a handful of variables. Hierarchical clustering needs pairwise distances and quickly runs into memory limits. Taking a random sample is easier, but a small sample can miss a rare group or the edges of a distribution.
Data nuggets summarize all the rows as a much smaller collection of weighted points. Each nugget has:
- a center, which is its location
- a weight, which counts the original observations that belong to it
- a scale, which describes the variation among those observations
The package keeps track of which nugget each original row belongs to, so you can cluster the nuggets and carry the cluster labels back to the full dataset. Reduction isn't lossless, though. You still need to check whether the structures you care about survive at the nugget count you choose.
This is a native Python port of 17 functions from R's datanugget 1.5.0 and WCluster 1.3.0, plus stats::cutree. You don't need R to use it.
Research alpha:
0.1.0a3. The API may change. Results are checked against recorded R reference cases, but there are documented differences. Use the release notes if you need the exact scope of this version.
Python walkthrough · API reference · Methodology · Limitations
Quick start
Install
Use Python 3.12 or newer. The package installs on Windows x64, Apple silicon Macs running macOS 12 or newer, and Linux x86-64 or ARM64 with glibc 2.27 or newer. Numba doesn't publish wheels for Intel Macs or Alpine Linux, so the normal install doesn't support those systems. See the installation guide for details.
Create a virtual environment, then install the published alpha:
python -m pip install "datanuggets[frame,plot]==0.1.0a3"
The frame and plot extras install pandas and matplotlib. The version is pinned on purpose. 0.1.0a3 is a pre-release, and naming it tells pip to install that release.
If you want the latest code on main, which may differ from the PyPI release:
python -m pip install "datanuggets[frame,plot] @ git+https://github.com/ron2k1/datanuggets.git@main"
There's also a complete example in examples/first_analysis.py. It runs from a clone of the repository, uses the recorded R quakes data and saves a plot and two tables under outputs/.
Make nuggets from a CSV
Pick numeric columns that describe the observations. Leave out IDs and known outcome labels. Here's a two-feature example. Replace the column names with your own.
import numpy as np
import pandas as pd
from datanuggets import create_dn
df = pd.read_csv("my_data.csv")
features = ["feature_a", "feature_b"]
x = df[features].to_numpy(dtype=float)
if len(x) < 2 or not np.isfinite(x).all():
raise ValueError("Expected at least two rows of finite numeric data.")
# Put these features on comparable scales before using distances.
mean = x.mean(axis=0)
scale = x.std(axis=0)
scale[scale == 0] = 1
x = (x - mean) / scale
dn = create_dn(x, dn_num1=2_000, dn_num2=200, seed=42)
print(f"{len(x):,} rows -> {dn.n_nuggets:,} nuggets")
print(dn.to_frame().head())
dn_num1 sets the intermediate target and dn_num2 the final target. They're starting values for this example, and they won't suit every dataset. If a target is at least as large as the data available at that stage, the function may warn and skip that reduction. Check the number of nuggets it returns.
The result contains dn.centers, dn.weights, dn.scales and dn.assignments. The last array has one nugget index for every input row. Keep your original row order if you plan to attach cluster labels to the DataFrame later.
Cluster the nuggets
Weighted k-means clusters the nugget centers and counts each one by its weight:
from datanuggets import dn_wkmeans
km = dn_wkmeans(dn, k=5, num_init=10, max_iter=100, seed=42)
df["cluster"] = km.row_labels
print(df["cluster"].value_counts().sort_index())
km.labels labels the nuggets. km.row_labels gives the labels for the original rows, using their nugget assignments.
For a weighted Ward hierarchy, build a tree over the nuggets and choose where to cut it:
from datanuggets import cut_tree, dn_whclust
if dn.n_nuggets < 5 or np.any(dn.weights == 0):
raise ValueError("Ward clustering needs at least five occupied nuggets here.")
tree = dn_whclust(dn)
nugget_labels = cut_tree(tree, k=5)
ward_row_labels = nugget_labels[dn.assignments]
Or run weighted PCA on the nugget centers:
from datanuggets import dn_wpca
pca = dn_wpca(dn)
print(pca.sdev) # Principal-component standard deviations
print(pca.x.shape) # One score row per nugget
You can also call refine_dn(x, dn, ...) before these analyses to split nuggets that are unusually spread out. Refinement can raise the nugget count. The walkthrough covers refinement, plots and assigning new observations to existing clusters.
How the reduction works
The method is from Beavers and colleagues. It selects a smaller, well-spaced set of starting points. Those points aren't k-means centers of the full dataset. On data with more rows than the chunk size, the method never computes the distance between every pair of original observations.
create_dn shuffles the rows and divides them into chunks of nearly equal size. The chunk size r is 5,000 by default, and data with no more rows than r stays in one chunk. Within each chunk, it repeatedly removes observations that have very close neighbors. It then pools the survivors, reduces them again and assigns every original row to its nearest surviving center. With the default center_method="mean", each final center is replaced by the mean of its assigned observations.
This tends to remove more points from dense parts of the data and keep points in sparse regions, including the tails. That's why the method can keep details that a random sample may miss. It's still a heuristic. The result depends on the random seed and the reduction settings, and it doesn't guarantee that every rare group is recovered.
A nugget's scale is the trace of the sample covariance of its rows divided by the number of features. Nuggets with fewer than two rows are handled separately. refine_dn looks at covariance eigenvalues and splits selected nuggets using two-means. It can also split elongated nuggets when shape_split=True.
Weighted k-means, Ward clustering and PCA use the nugget centers and weights. The scales describe the variation inside each nugget. The weighted methods don't use them, so they don't reconstruct all the variation lost in compression.
For the exact chunking, deletion, tie-breaking and refinement rules, see the methodology guide. The port follows the R implementation where it differs from the published algorithm.
What comes from the research
The original data nuggets paper develops the reduction method and runs PCA and k-means clustering for weighted observations on the nuggets. One application starts with 1,048,575 flow cytometry observations of nine variables, creates 2,000 nuggets and refines them to 3,135. In the paper's comparison, the refined nuggets recovered the structure of the first three principal components, and a random sample of 3,000 rows didn't recover it well. This package doesn't reproduce that comparison.
A second paper uses data nuggets for linear regression, and weighted least squares on the nugget summaries is one of its estimators. The original paper also names projection pursuit as a method that could be applied to data nuggets. This port implements neither nugget regression nor projection pursuit.
For what this package implements, see the function map below and the API reference.
R and Python names
R function names become snake_case in Python. The Python results are objects with named fields rather than R lists. Indices and labels are zero-based rather than one-based.
| R | Python |
|---|---|
datanugget::create.DN |
create_dn |
datanugget::refine.DN |
refine_dn |
datanugget::create.DNcenters |
create_dn_centers |
datanugget::getDN.means |
get_dn_means |
datanugget::AC |
arithmetic_complexity |
WCluster::Wkmeans |
wkmeans |
WCluster::Whclust |
whclust |
WCluster::Wpca |
wpca |
WCluster::cluster.predict |
cluster_predict |
WCluster::wmean |
wmean |
WCluster::wss |
wss |
WCluster::wwcss |
wwcss |
WCluster::distw |
distw |
WCluster::DN.Wkmeans |
dn_wkmeans |
WCluster::DN.Whclust |
dn_whclust |
WCluster::DN.Wpca |
dn_wpca |
WCluster::DNcluster.predict |
dn_cluster_predict |
stats::cutree |
cut_tree |
datanugget::optimal.DN isn't ported. It selects a nugget count by running the method at candidate sizes and comparing propensity score indices.
A few call signatures differ from R's on purpose. create_dn_centers and cluster_predict take their arguments in a different order, and wkmeans and dn_wkmeans take every option after k by keyword. Some arguments are renamed too, such as no.cores to n_jobs. Read the compatibility notes before translating an R script line by line. They also show the same pipeline in R and in Python.
Reproducing R's random draws
An integer seed draws from NumPy, so it doesn't give the draws R makes after set.seed(). To get R's draws, pass RDraws:
from datanuggets import RDraws, create_dn
dn = create_dn(x, dn_num1=2_000, dn_num2=200, seed=RDraws(291102))
That helps when comparing the two implementations, but it doesn't make every result identical. Eigen calculations, some exact distance ties and some edge cases can still differ. The compatibility notes say what was checked and what still differs.
Things to check before trusting a result
- Try more than one seed and nugget count. Nugget creation is a heuristic. Weighted k-means also depends on its random starts, and Ward clustering never revisits a merge.
- Look for lost structure. Compare the reduced data with the original where you can. Small or distant groups are a reason to try data nuggets, and the method can still miss them.
- Keep the weights. Ignoring them changes what the clustering and the PCA mean. A cluster number is an arbitrary label, and it doesn't identify a known class.
- Watch memory use. When the data has more rows than the chunk size, reduction avoids a full pairwise matrix over the original rows. It still builds a distance matrix within each chunk and one over the intermediate centers. Ward clustering is still quadratic in the number of nuggets. A dense 10,000 by 10,000
float64matrix alone takes about 800 MB. - Record your environment. Keep the package version, input preprocessing, seed and nugget settings with the analysis. This is a research alpha, and exact R parity isn't guaranteed on every machine or dataset.
See limitations and open problems, reproducibility and the contributor guide for more detail.
Papers and credits
If you use the data nuggets method, please cite the original research:
Beavers, T. E., Cheng, G., Duan, Y., Cabrera, J., Lubomirski, M., Amaratunga, D., and Teigler, J. E. (2025). Data Nuggets: A Method for Reducing Big Data While Preserving Data Structure. Journal of Computational and Graphical Statistics, 34(1), 330-342. Published online in 2024. doi:10.1080/10618600.2024.2341896.
Cherasia, K. E., Cabrera, J., Fernholz, L. T., and Fernholz, R. (2023). Data Nuggets in Supervised Learning. In M. Yi and K. Nordhausen (eds.), Robust and Multivariate Statistical Methods: Festschrift in Honor of David E. Tyler (pp. 429-449). Springer. Published online in 2022. doi:10.1007/978-3-031-22687-8_20.
The methods come from the R packages datanugget and WCluster. Those packages are the work of Rituparna Dey, Yajie Duan, Traymon Beavers, Javier Cabrera, Ge Cheng, Kunting Qi and Mariusz Lubomirski (datanugget), and of Rituparna Dey, Yajie Duan, Javier Cabrera and Ge Cheng (WCluster). Rituparna Dey maintains both R packages. The third-party notices give the upstream credits and the release approval. This Python port is distributed separately under GPL-2.0-only and claims no authorship of the original algorithms.
Metadata
Release files for datanuggets 0.1.0a3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| datanuggets-0.1.0a3.tar.gz | 4.4 MB | With provenance | Details for datanuggets-0.1.0a3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| datanuggets-0.1.0a3-py3-none-any.whl | Python 3 | none | any | Details for datanuggets-0.1.0a3-py3-none-any.whl |
Total release size: 4.5 MB
Release files / datanuggets-0.1.0a3.tar.gz
| Download URL | datanuggets-0.1.0a3.tar.gz |
|---|---|
| Size | 4.4 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d68840d809cad14ef5fabe79968141201c3c183936d679a7359786d0cd077bec
|
|
BLAKE2b-256 checksum How to use checksums |
4b1b2241f14df0c20c51c2849e64147ff77484a2139ba69c5a9b689cd39332c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logRelease files / datanuggets-0.1.0a3-py3-none-any.whl
| Download URL | datanuggets-0.1.0a3-py3-none-any.whl |
|---|---|
| Size | 138.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e637fa0ecd091857d897e52e869fb6fd03baa9baec515bd26cf322c343053cab
|
|
BLAKE2b-256 checksum How to use checksums |
bd2d6b26ae9005ccb5f14f22427acae8684258df089588975d28ff416c8fcac4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logSecurity for datanuggets 0.1.0a3
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for datanuggets-0.1.0a3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.
Transparency logA 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.