Module: CrossValidation::Partitioner

Defined in:
lib/cross_validation/partitioner.rb

Overview

Provides helper methods for data partitioning.

Class Method Summary collapse

Class Method Details

.exclude_index(ary, i) ⇒ Array

Returns a flattened copy of the original array without an element at index i.

Parameters:

  • ary (Array)

    subsets to work with (e.g., array of arrays)

  • i (Fixnum)

    index to remove

Returns:

  • (Array)


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]].

Parameters:

  • ary (Array)

    documents to work with

  • k (Fixnum)

    size of each subset

Returns:

  • (Array)

    array of arrays

Raises:

  • (ArgumentError)

    if the length of the documents array is not evenly divisible by k



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