module Tools::ClassifierMethods

Methods to test classifier accuracy via K-fold cross validation.

Public Instance Methods

correct_count(ypred, ytest, c, t, n) click to toggle source
# File lib/rubyml/tools.rb, line 83
def correct_count(ypred, ytest, c, t, n)
  count = 0.0
  ypred.row_count.times do |r|
    count += handle_epsilon(ypred, ytest, r)
  end
  p "Fold #{n} Accuracy: #{(count / ypred.row_count * 100.0).round(3)}%"
  [c + count, t + ypred.row_count]
end
generate_folds(x, y, num, folds) click to toggle source
# File lib/rubyml/tools.rb, line 55
def generate_folds(x, y, num, folds)
  sin = String(num * (x.row_count / folds))
  ein = String([(num + 1) * (x.row_count / folds), x.row_count].min)
  train = generate_train_set(x, y, sin, ein)
  test = generate_test_set(x, y, sin, ein)
  train + test
end
generate_test_set(x, y, sin, ein) click to toggle source
# File lib/rubyml/tools.rb, line 69
def generate_test_set(x, y, sin, ein)
  xtest = x[sin + ':' + ein, ':']
  ytest = y[sin + ':' + ein, ':']
  [xtest, ytest]
end
generate_train_set(x, y, sin, ein) click to toggle source
# File lib/rubyml/tools.rb, line 63
def generate_train_set(x, y, sin, ein)
  xtrain = x[':' + sin, ':'].vstack(x[ein + ':', ':'])
  ytrain = y[':' + sin, ':'].vstack(y[ein + ':', ':'])
  [xtrain, ytrain]
end
handle_epsilon(ypred, ytest, r) click to toggle source
# File lib/rubyml/tools.rb, line 75
def handle_epsilon(ypred, ytest, r)
  if @epsilon
    ((ypred[r, 0] - ytest[r, 0]).abs < @epsilon ? 1.0 : 0.0)
  else
    (ypred[r, 0] == ytest[r, 0] ? 1.0 : 0.0)
  end
end
training_accuracy(x, y) click to toggle source
# File lib/rubyml/tools.rb, line 92
def training_accuracy(x, y)
  correct = 0.0
  total = 0.0
  @folds.times do |n|
    xtrain, ytrain, xtest, ytest = generate_folds(x, y, n, @folds)
    fit(xtrain, ytrain)
    ypred = predict(xtest)
    correct, total = correct_count(ypred, ytest, correct, total, n)
  end
  (correct / total).round(5)
end