Skip to main content

A fast implementation of the DCR (Distance to Clostest Record) for tabular data.

Project description

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)}}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_dcr-0.1.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_dcr-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file torch_dcr-0.1.1.tar.gz.

File metadata

  • Download URL: torch_dcr-0.1.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torch_dcr-0.1.1.tar.gz
Algorithm Hash digest
SHA256 054a0a33671a03968d0449db142413d6c951b85870384b8b26dc849e14302c3a
MD5 d3495cf97029354d07af76491ec55af4
BLAKE2b-256 ec247cb0f7eab3bd5c1c8da0b29e2b71cccd352beb9893c92b7261472a9449f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_dcr-0.1.1.tar.gz:

Publisher: cd.yml on DavideScassola/torch-dcr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file torch_dcr-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: torch_dcr-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for torch_dcr-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 810121cf8b24628d5419b0c5b3d4bcb7064d5f8a59d5a319f62fec01dd6f23b9
MD5 335970c105bb3c18aa8afa95a8c31acf
BLAKE2b-256 4503dd727d2ac9e092552e46f1133dd01e0ac5859be3c5176c51ed4c257bf969

See more details on using hashes here.

Provenance

The following attestation bundles were made for torch_dcr-0.1.1-py3-none-any.whl:

Publisher: cd.yml on DavideScassola/torch-dcr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page