Module: CrossValidation::Partitioner
- Defined in:
- lib/cross_validation/partitioner.rb
Overview
Provides helper methods for data partitioning.
Class Method Summary collapse
-
.exclude_index(ary, i) ⇒ Array
Returns a flattened copy of the original array without an element at index
i
. -
.subset(ary, k) ⇒ Array
Splits the array into
k
-sized subsets.
Class Method Details
.exclude_index(ary, i) ⇒ Array
Returns a flattened copy of the original array without an element at index i
.
30 31 32 |
# File 'lib/cross_validation/partitioner.rb', line 30 def self.exclude_index(ary, i) ary.rotate(i).drop(1).flatten end |
.subset(ary, k) ⇒ Array
Splits the array into k
-sized subsets.
For example, calling this method for the array %w(foo bar baz qux) with k=2 results in an array of arrays: [[foo, bar], [baz, qux]].
16 17 18 19 20 21 22 |
# File 'lib/cross_validation/partitioner.rb', line 16 def self.subset(ary, k) if ary.length % k > 0 fail ArgumentError, "Can't create equal subsets when k=#{k}" end ary.each_slice(k).to_a end |