Skip to content

RillonDodgers/data_sampler_rb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DataSampler

DataSampler is a simple Ruby gem for sampling data from an array of hashes based on specified keys and a sampling percentage.

Usage

require 'data_sampler'

data = [
  { color: "Red", name: "Apple", smell: "Sweet" },
  { color: "Yellow", name: "Banana", smell: "Fruity" },
  { color: "Orange", name: "Orange", smell: "Citrusy" },
  # more data here...
]

sampler = DataSampler.new(data)

Sampling Data

Use the sample_by method to sample data based on specified keys and a sampling percentage:

sampled_data = sampler.sample_by([:color], 50)  # Sample by color with 50% sampling rate
puts sampled_data.inspect

Example

The result should be two items in the sample, one of the red fruits and the blackberry

require 'data_sampler'

data = [
  { color: "Red", name: "Apple", smell: "Sweet" },
  { color: "Red", name: "Cherry", smell: "Sweet" },
  { color: "Red", name: "Strawberries", smell: "Sweet" },
  { color: "Red", name: "Dragon Fruit", smell: "Sweet" },
  { color: "Red", name: "Watermelon", smell: "Sweet" },
  { color: "Black", name: "Blackberry", smell: "Earthy" }  # This should not be sampled
]

sampler = DataSampler.new(data)
sampled_data = sampler.sample_by([:color, :smell], 20)  # 20% sampling rate

puts sampled_data.inspect

About

Ruby gem for doing a randomized sample of data in an array

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages