evaluator_test.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. require 'svm_toolkit'
  2. require 'minitest/autorun'
  3. include SvmToolkit
  4. class TestEvaluator < MiniTest::Test
  5. def test_evaluate_accuracy
  6. performance = Evaluator::OverallAccuracy.new
  7. assert_equal(0.0, performance.value)
  8. performance.add_result(0, 0)
  9. assert_equal(100.0, performance.value)
  10. performance.add_result(1, 0)
  11. assert_equal(50.0, performance.value)
  12. performance2 = Evaluator::OverallAccuracy.new
  13. assert_equal(true, performance.better_than?(performance2))
  14. performance2.add_result(1, 1)
  15. performance2.add_result(1, 1)
  16. assert_equal(false, performance.better_than?(performance2))
  17. end
  18. def test_compute_geometric_mean
  19. performance = Evaluator::GeometricMean.new
  20. performance.add_result(0, 0)
  21. assert_equal(1.0, performance.value)
  22. performance.add_result(0, 1)
  23. performance.add_result(1, 1)
  24. assert_in_delta(0.707, performance.value, 0.01)
  25. performance.add_result(1, 1)
  26. performance.add_result(1, 0)
  27. assert_in_delta(0.577, performance.value, 0.001)
  28. end
  29. def test_compute_class_precision
  30. performance1 = Evaluator::ClassPrecision(0).new
  31. performance1.add_result(0, 0)
  32. performance1.add_result(0, 0)
  33. performance1.add_result(0, 0)
  34. performance1.add_result(0, 1)
  35. performance1.add_result(1, 0)
  36. performance1.add_result(1, 0)
  37. performance1.add_result(1, 1)
  38. # 3 correct out of 5 predicted 0s
  39. assert_in_delta(0.6, performance1.value, 0.001)
  40. end
  41. def test_compute_class_precision_2
  42. performance2 = Evaluator::ClassPrecision(1).new
  43. performance2.add_result(0, 0)
  44. performance2.add_result(0, 0)
  45. performance2.add_result(0, 0)
  46. performance2.add_result(0, 1)
  47. performance2.add_result(1, 0)
  48. performance2.add_result(1, 0)
  49. performance2.add_result(1, 1)
  50. # 1 correct out of 2 predicted 1s
  51. assert_in_delta(0.5, performance2.value, 0.001)
  52. end
  53. def test_compute_class_recall
  54. performance1 = Evaluator::ClassRecall(0).new
  55. performance1.add_result(0, 0)
  56. performance1.add_result(0, 0)
  57. performance1.add_result(0, 0)
  58. performance1.add_result(0, 1)
  59. performance1.add_result(1, 0)
  60. performance1.add_result(1, 0)
  61. performance1.add_result(1, 1)
  62. # 3 correct out of the 4 actual 0s
  63. assert_in_delta(0.75, performance1.value, 0.001)
  64. end
  65. def test_compute_class_recall_2
  66. performance2 = Evaluator::ClassRecall(1).new
  67. performance2.add_result(0, 0)
  68. performance2.add_result(0, 0)
  69. performance2.add_result(0, 0)
  70. performance2.add_result(0, 1)
  71. performance2.add_result(1, 0)
  72. performance2.add_result(1, 0)
  73. performance2.add_result(1, 1)
  74. # 1 correct out of the 3 actual 1s
  75. assert_in_delta(0.333, performance2.value, 0.001)
  76. end
  77. def test_compute_mcc
  78. performance = Evaluator::MatthewsCorrelationCoefficient(1).new
  79. performance.add_result(0, 0)
  80. assert_equal(0.0, performance.value)
  81. performance.add_result(0, 1)
  82. assert_equal(0.0, performance.value)
  83. performance.add_result(1, 0)
  84. assert_equal(-0.5, performance.value)
  85. performance.add_result(1, 1)
  86. assert_equal(0.0, performance.value)
  87. performance.add_result(1, 1)
  88. assert_in_delta(0.167, performance.value, 0.001)
  89. performance.add_result(0, 0)
  90. assert_in_delta(0.333, performance.value, 0.001)
  91. end
  92. end