test_metrics.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from ds4ml.metrics import (pairwise_mutual_information,
  2. relative_error, error_rate)
  3. def test_pairwise_mutual_information():
  4. from pandas import DataFrame
  5. from .testdata import adults01
  6. frame = DataFrame(adults01)
  7. mi = pairwise_mutual_information(frame)
  8. from numpy import allclose, alltrue
  9. assert allclose(mi, mi.T)
  10. assert alltrue(mi >= 0.0)
  11. assert alltrue(mi <= 1.0)
  12. def test_error_rate():
  13. from sklearn.metrics import accuracy_score
  14. from numpy.random import randint
  15. from numpy import isclose
  16. y_true = [2, 0, 2, 2, 0, 1]
  17. y_pred = [0, 0, 2, 2, 0, 2]
  18. # its confusion matrix:
  19. # 2 0 0
  20. # 0 0 1
  21. # 1 0 2
  22. assert isclose(error_rate(y_true, y_pred), 0.3333)
  23. for _ in range(10):
  24. y_true = randint(0, 20, 100)
  25. y_pred = randint(0, 20, 100)
  26. assert isclose(error_rate(y_true, y_pred),
  27. 1 - accuracy_score(y_true, y_pred))
  28. def test_relative_error():
  29. from numpy import array, random
  30. a = array([0, 1, 0])
  31. b = array([1, 1, 0])
  32. assert relative_error(a, b) == 0.3333
  33. for _ in range(10):
  34. size = random.randint(5, 100)
  35. x = random.choice(20, size)
  36. y = random.choice(20, size)
  37. assert 0.0 <= relative_error(x, y) <= 1.0
  38. x = random.rand(size)
  39. y = random.rand(size)
  40. assert 0.0 <= relative_error(x, y) <= 1.0