Module: Tools::ClassifierMethods
- Included in:
- LinearRegression, Perceptron
- Defined in:
- lib/rubyml/tools.rb
Overview
Methods to test classifier accuracy via K-fold cross validation.
Instance Method Summary collapse
- #correct_count(ypred, ytest, c, t, n) ⇒ Object
- #generate_folds(x, y, num, folds) ⇒ Object
- #generate_test_set(x, y, sin, ein) ⇒ Object
- #generate_train_set(x, y, sin, ein) ⇒ Object
- #handle_epsilon(ypred, ytest, r) ⇒ Object
- #training_accuracy(x, y) ⇒ Object
Instance Method Details
#correct_count(ypred, ytest, c, t, n) ⇒ Object
83 84 85 86 87 88 89 90 |
# 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) ⇒ Object
55 56 57 58 59 60 61 |
# 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) ⇒ Object
69 70 71 72 73 |
# 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) ⇒ Object
63 64 65 66 67 |
# 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) ⇒ Object
75 76 77 78 79 80 81 |
# 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) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# 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 |