Skip to main content

torch-dcr

CI License: MIT

A library for efficient, GPU-accelerated computation of DCR (Distance to Closest Record) for heterogeneous tabular data.

DCR measures, for each record in a source dataset, how close the nearest record in a target dataset is. It is commonly used to evaluate privacy risk in synthetic data: if synthetic records are too close to the training records, synthetic data probably leaks information about individuals in the training set. torch-dcr computes this efficiently on CPU or GPU using PyTorch, and handles datasets with a mix of continuous and categorical columns out of the box.

Basic usage

import pandas as pd
from torch_dcr import dcr

source_df = pd.DataFrame(
    {
        "A": [4, 0, 3],
        "B": ["a", "b", "d"],
        "C": [1.12, -1.6, 6],
    }
)
target_df = pd.DataFrame(
    {
        "A": [1, 2, 3, 4, 5],
        "B": ["f", "b", "g", "d", "d"],
        "C": [0.0, 0.0, 2.0, -12.0, -4.0],
    }
)

dcr(source_df, target_df, metric="cosine")

Output:

      dcr_1
0  0.667516
1  0.166855
2  0.449738

Each row corresponds to a record in source_df, and dcr_1 is the distance to the closest record found in target_df.

Installation

To install the library run:

pip install torch-dcr

How mixed-type data is handled

Real-world tabular data usually mixes continuous columns (e.g. floats, ints) with categorical columns (e.g. strings). torch-dcr handles this automatically. The standard approach consists in converting categorical columns to one-hot encoded vectors, and then computing distances in the resulting high-dimensional space. torch-dcr performs this computation without instantiating the full one-hot encodings, saving a considerable amount of memory and computation time. Categorical columns are automatically detected based on the DataFrame's dtypes.

Advanced usage

dcr_df, indexes_df = dcr(
    source_df=source_df,   # Source DataFrame; DCR is computed for each record in this DataFrame
    target_df=target_df,   # Target DataFrame where the closest records are searched
    output_indexes=True,   # If True, also return the indexes of the closest records
    k=2,                   # Number of closest records to consider for each record in source_df
    metric="l1",           # Distance metric for continuous columns: "cosine", "euclidean", or "l1"
    device="cuda",         # "cpu" or "cuda" for GPU acceleration
    standardize=True,      # Whether to standardize continuous features before computing distances
    batch_size=1000,       # Batch size for processing, useful for large DataFrames
)

Output:

DCR:
       dcr_1     dcr_2
0  2.790001  3.465423
1  1.551357  2.918901
2  2.716115  3.055198

Indexes:
   index_1  index_2
0        2        1
1        1        0
2        2        4

With k=2, dcr_1/dcr_2 are the distances to the 1st and 2nd closest records in target_df, and index_1/index_2 (from indexes_df) are the corresponding row indexes in target_df.

Parameters

Parameter Type Default Description
source_df pd.DataFrame required Records for which DCR is computed
target_df pd.DataFrame required Records searched for nearest neighbors
metric str "cosine" Distance metric for continuous columns: "cosine", "euclidean", or "l1"
k int 1 Number of nearest neighbors to return per record
output_indexes bool False If True, also return a DataFrame of nearest-neighbor indexes
standardize bool True Standardize continuous columns before computing distances
device str "cpu" "cpu" or "cuda"
batch_size int 1000 Batch size to limit memory usage on large datasets
progress_bar bool True If True, show a progress bar during computation

License

This project is licensed under the MIT License — see the LICENSE file for details.

Citation

If you use torch-dcr in your research or project, please cite it using the following bibtex entry:

@misc{torch_dcr_2026,
  author       = {Davide Scassola},
  title        = {torch-dcr: A fast implementation of the DCR (Distance to Closest Record) for tabular data},
  year         = {2026},
  publisher    = {GitHub},
  journal      = {GitHub repository},
  howpublished = {\url{[https://github.com/DavideScassola/torch-dcr](https://github.com/DavideScassola/torch-dcr)}}
}

Metadata

Release files for torch-dcr 0.1.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 torch-dcr 0.1.1
File Size Uploaded Provenance
torch_dcr-0.1.1.tar.gz 8.5 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for torch-dcr 0.1.1
File Interpreter ABI Platform
torch_dcr-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.9 kB

Release files / torch_dcr-0.1.1.tar.gz

Download URL torch_dcr-0.1.1.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
054a0a33671a03968d0449db142413d6c951b85870384b8b26dc849e14302c3a
BLAKE2b-256 checksum
How to use checksums
ec247cb0f7eab3bd5c1c8da0b29e2b71cccd352beb9893c92b7261472a9449f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 13, 2026.

Transparency log

Release files / torch_dcr-0.1.1-py3-none-any.whl

Download URL torch_dcr-0.1.1-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
810121cf8b24628d5419b0c5b3d4bcb7064d5f8a59d5a319f62fec01dd6f23b9
BLAKE2b-256 checksum
How to use checksums
4503dd727d2ac9e092552e46f1133dd01e0ac5859be3c5176c51ed4c257bf969
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jul 13, 2026.

Transparency log

Security for torch-dcr 0.1.1

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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for torch_dcr-0.1.1.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 Jul 13, 2026.

Transparency log

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

0.1.1 This release

2 release files Full 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