Skip to main content

Introduction

SamRand is a tool designed to sample datasets and produce statistically representative samples for research purposes.

Who is this for?

I developed this primarily for researchers who deal with large datasets, and need to sample them to conduct some qualitative analysis. While it was meant to pull samples for qualitative purposes, it could be used for quantitative purposes as well. And even though, at the time, it was meant for researchers, there is no reason to believe that it can be used for non-research use-cases. As such, this project is licensed under the MIT license.

How does SamRand sample a dataset?

SamRand’s sampling approach differs depending on the settings you use when sampling a dataset. These are, however, dependent on the choice of stratification:

  • No Stratification: SamRand will select rows from the dataset at random without attempting to represent any existing groups within the dataset’s population.

  • Stratification with Unknown Dimensions: SamRand will perform a single-level clustering along the dimension with the least variance (to guarantee diverse strata). Samples are pulled from these two strata based on their proportion to the dataset’s distribution. For instance, a dataset with location as the dimension with the least variance (either X or Y with a 60:40 split) will generate a sample of 6 rows in location X and 4 in location Y if the sample size is 10.

  • Stratification with known Dimensions: If you provide specific dimensions (column indices) when invoking SamRand, it will apply multi-level clustering to generate strata. This means it will split the data by the first dimension, then split the strata resulting from the first split by the second dimension, and so on.

Important Note: Depending on how your dataset is distributed, it is possible that there will be strata with only a single row. SamRand will extract at least one row from each strata. This will inflate sample size, resulting in a sample size larger than what you specified. To reconcile the difference, SamRand will (once it has a representative sample) remove rows at random from that sample until it shrinks down to the desired size. Consequently, rows from larger strata have a higher probability to be removed towards the end of the sampling process.

There is also whether you choose to sample with or without replacement:

  • with Replacement: Rows previously sampled may be sampled again. Which means that the dataset may consist of duplicate rows.

  • without Replacement: Rows previously sampled may not be sampled again. which means that the dataset will not contain duplicates unless the dataset itself contains duplicates.

If there is a sampling strategy you’d like to see implemented or fixed, feel free to open an issue. I will try to get around to it. Alternatively, you can submit a merge request. Stay up-to-date by monitoring SamRand’s issues page.

How Do I Use SamRand?

SamRand supports two modes of use: - as a standalone application, and - as a module within your python script.

What Should My Dataset Look Like?

Right now, SamRand supports two types of datasets, CSV files and JSON files. For now, CSV files are expected to use commas as delimiters, with double quotes around text (default python CSV settings). JSON files are expected to be valid. Examples of both dataset types are included in the test folder of this repository.

As a standalone application

Once installed, you can use SamRand as a standalone application in your terminal of choice. It supports the following arguments:

  • -h, –help: Shows a help message and exits.

  • –dataset <path/to/dataset/file>: The file containing your dataset.

  • –size <integer>: The required sample size (n).

  • –header: When using a CSV dataset file, use this flag to indicate whether the first row is a header.

  • –replacement: Extract samples with replacement. Not including this flag means without replacement (the default behavior).

  • –stratify: Balance the extracted sample so that it reflects the population’s distribution.

  • –strata ‘[0, 1, 2, …]’: When using stratification, use this parameter to indicate which fields should be used as a basis for stratification. Accepts valid JSON arrays of column indices starting with 0.

  • –output: The output format of the samples. Default is JSON. Can be one of [CSV|JSON].

A typical command using SamRand looks like the following example that samples a CSV dataset with a header for 30 samples, then outputs the sample to _stdout_ in CSV format:

$ SamRand --dataset datasets/dataset.csv \
--size 30 \
--header \
--stratify \
--strata '[4, 5]' \
--output CSV

To output the results somewhere other than _stdout_, redirect the output to a file depending on your terminal emulator. For instance, when redirecting the above command’s output to a CSV file in a standard bash session:

$ SamRand --dataset datasets/dataset.csv \
--size 30 \
--header \
--stratify \
--strata '[4, 5]' \
--output CSV > output.csv

As a Python module

You can build a python script and use SamRand within it to sample datasets on the fly to do with as you please. For instance, if you wanted to sample a dataset in your python script, you would import SamRand as a dependency, and give it the necessary information:

import samrand as sr

dataset_path = '/path/to/my/dataset.json'
dataset = sr.reader.read_json(dataset_path)
sample = sr.sampler.sample(dataset, 30, stratify=True, replacement=True)

Further documentation can be found here.

How Do I Install SamRand?

Regardless of whether you want to use it as a standalone application or a module in your project, you can install SamRand via pip as you would any normal python module:

$ pip install samrand

Release files for samrand 0.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 samrand 0.1.0
File Size Uploaded
samrand-0.1.0.tar.gz 10.8 kB Details

Built distribution (wheel)

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

Total release size: 19.9 kB

Release files / samrand-0.1.0.tar.gz

Download URL samrand-0.1.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3a4ebe5abe41b989ba54248c98b6c93f264ff40ff15d4c9ae0c5d63010726b2e
BLAKE2b-256 checksum
How to use checksums
56d26853d762be939b520e0964feff08282258c69c8c0f0e7c235d1e0f181d91
Upload date
Uploaded using Trusted Publishing?
What is 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.8.5

Release files / samrand-0.1.0-py3-none-any.whl

Download URL samrand-0.1.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c6c04867434407092534ff7970a5e980a77eadf013e88ee9b2f6b1e541ce790
BLAKE2b-256 checksum
How to use checksums
87aabd24857d6f9e428f3aac7b62b800522b3e34704458c524b83a72cf0e8cf0
Upload date
Uploaded using Trusted Publishing?
What is 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.8.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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