Skip to main content

Python library for implementing post-randomisation method (PRAM) for disclosure control in synthetic data

Project description

pram

Python implementation of post-randomisation method for disclosure control

Build Status License Status Supported versions Version

Call the pram() method with a Pandas dataframe to apply post-randomisation perturbation to the dataset using a generated transition matrix.

You can specify the minimum diagonal value (i.e. the minimum probability that a data point remains unchanged) and an alpha value to modify the likelihood of perturbation (from zero to one).

By default all columns are modified, and there is no stratification. However you can specify the columns to process as a list, and also specify a column to use for stratification. If stratification is used, the column used for stratification is not modified.

The behaviour is largely the same as that in the "sdcMicro" R package.

Command-line usage

You can also call Pram from the command line, supplying a CSV file input and path to output the perturbed dataset as CSV.

From the command line you can also use the -f switch to print a table of the frequencies of categories in the original and changed versions of the dataset.

Example:

pram mydata.csv output.csv 0.8 0.5 -f

This will load the data in mydata.csv, run PRAM with m=0.8 and a=0.5, save the output in output.csv, and print a frequency table to the console.

Examples

Command-line use with stratification

Run PRAM stratified by gender on region and education, and output the frequency table

pram mydata.csv output.csv 0.8 0.5 region,education gender -f

Result:

           Column  Original  Output
female     gender      0.67    0.67
male       gender      0.33    0.33
rural      region      0.67    0.71
urban      region      0.33    0.29
lower   education      0.67    0.62
higher  education      0.33    0.38

Note that as gender was used to stratify the data, it didn't change.

Simple use within python

from pram import pram
persons = [
{'gender': 'female', 'region': 'rural', 'education': 'higher', 'age': 27},
{'gender': 'female', 'region': 'rural', 'education': 'lower', 'age': 35},
{'gender': 'male', 'region': 'rural', 'education': 'lower', 'age': 26},
{'gender': 'male', 'region': 'rural', 'education': 'lower', 'age': 22},
{'gender': 'female', 'region': 'urban', 'education': 'higher', 'age': 41},
{'gender': 'female', 'region': 'urban', 'education': 'lower', 'age': 54},
{'gender': 'female', 'region': 'rural', 'education': 'higher', 'age': 38},
{'gender': 'female', 'region': 'rural', 'education': 'lower', 'age': 44},
{'gender': 'male', 'region': 'rural', 'education': 'lower', 'age': 18},
{'gender': 'male', 'region': 'rural', 'education': 'lower', 'age': 52},
{'gender': 'female', 'region': 'urban', 'education': 'higher', 'age': 44},
{'gender': 'female', 'region': 'urban', 'education': 'lower', 'age': 35},
{'gender': 'female', 'region': 'rural', 'education': 'higher', 'age': 33},
{'gender': 'female', 'region': 'rural', 'education': 'lower', 'age': 31},
{'gender': 'male', 'region': 'rural', 'education': 'lower', 'age': 40},
{'gender': 'male', 'region': 'rural', 'education': 'lower', 'age': 23},
{'gender': 'female', 'region': 'urban', 'education': 'higher', 'age': 68},
{'gender': 'female', 'region': 'urban', 'education': 'lower', 'age': 19},
{'gender': 'female', 'region': 'rural', 'education': 'higher', 'age': 27},
{'gender': 'female', 'region': 'rural', 'education': 'lower', 'age': 24},
{'gender': 'male', 'region': 'rural', 'education': 'lower', 'age': 48},
{'gender': 'male', 'region': 'rural', 'education': 'lower', 'age': 38},
{'gender': 'female', 'region': 'urban', 'education': 'higher', 'age': 30},
{'gender': 'female', 'region': 'urban', 'education': 'lower', 'age': 27}
]
df = pd.DataFrame(persons)
print(pram(df))

The output of the example might be:

    gender region education age
0   female  rural     lower  27
1   female  urban     lower  35
2     male  rural    higher  26
3     male  rural     lower  40
4   female  rural    higher  41
5   female  urban     lower  54
6     male  rural    higher  38
7   female  rural     lower  44
8     male  urban     lower  18
9     male  urban     lower  27
10  female  urban     lower  44
11  female  urban     lower  33
12  female  urban     lower  27
13    male  rural     lower  31
14  female  urban    higher  40
15    male  urban    higher  23
16  female  rural    higher  68
17  female  urban     lower  19
18  female  urban     lower  48
19  female  rural     lower  24
20    male  rural     lower  38
21    male  rural     lower  38
22    male  urban     lower  30
23  female  rural     lower  41

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

pram-0.1.5.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

pram-0.1.5-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pram-0.1.5.tar.gz.

File metadata

  • Download URL: pram-0.1.5.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.1

File hashes

Hashes for pram-0.1.5.tar.gz
Algorithm Hash digest
SHA256 43deb414fc0ca0a5ea1e9360dbd5130f944e801ca4e11c9235c325e215f02436
MD5 7bfb7428ad6eb9bcbd43756bfd1d7b5b
BLAKE2b-256 12fdc59d3b4a3d3fbc6e4dfffa38e450ff6e8a86926855b996ab3d80a9629863

See more details on using hashes here.

File details

Details for the file pram-0.1.5-py2.py3-none-any.whl.

File metadata

  • Download URL: pram-0.1.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.1

File hashes

Hashes for pram-0.1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cc397be3b3d751d88eb9e5429cfc7a32b70ed15cfcd0bbdf10b25cb18ad694d9
MD5 fbc2c653939a74d1a7fd0b6f7b62831a
BLAKE2b-256 b0e31b16d78b38a390a6e96725df39c65d28030f6470b169d30f353e7d127f3e

See more details on using hashes here.

Supported by

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