test_plotting.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import numpy as np
  2. import pytest
  3. from sklearn.utils._plotting import _interval_max_min_ratio, _validate_score_name
  4. def metric():
  5. pass # pragma: no cover
  6. def neg_metric():
  7. pass # pragma: no cover
  8. @pytest.mark.parametrize(
  9. "score_name, scoring, negate_score, expected_score_name",
  10. [
  11. ("accuracy", None, False, "accuracy"), # do not transform the name
  12. (None, "accuracy", False, "Accuracy"), # capitalize the name
  13. (None, "accuracy", True, "Negative accuracy"), # add "Negative"
  14. (None, "neg_mean_absolute_error", False, "Negative mean absolute error"),
  15. (None, "neg_mean_absolute_error", True, "Mean absolute error"), # remove "neg_"
  16. ("MAE", "neg_mean_absolute_error", True, "MAE"), # keep score_name
  17. (None, None, False, "Score"), # default name
  18. (None, None, True, "Negative score"), # default name but negated
  19. ("Some metric", metric, False, "Some metric"), # do not transform the name
  20. ("Some metric", metric, True, "Some metric"), # do not transform the name
  21. (None, metric, False, "Metric"), # default name
  22. (None, metric, True, "Negative metric"), # default name but negated
  23. ("Some metric", neg_metric, False, "Some metric"), # do not transform the name
  24. ("Some metric", neg_metric, True, "Some metric"), # do not transform the name
  25. (None, neg_metric, False, "Negative metric"), # default name
  26. (None, neg_metric, True, "Metric"), # default name but negated
  27. ],
  28. )
  29. def test_validate_score_name(score_name, scoring, negate_score, expected_score_name):
  30. """Check that we return the right score name."""
  31. assert (
  32. _validate_score_name(score_name, scoring, negate_score) == expected_score_name
  33. )
  34. # In the following test, we check the value of the max to min ratio
  35. # for parameter value intervals to check that using a decision threshold
  36. # of 5. is a good heuristic to decide between linear and log scales on
  37. # common ranges of parameter values.
  38. @pytest.mark.parametrize(
  39. "data, lower_bound, upper_bound",
  40. [
  41. # Such a range could be clearly displayed with either log scale or linear
  42. # scale.
  43. (np.geomspace(0.1, 1, 5), 5, 6),
  44. # Checking that the ratio is still positive on a negative log scale.
  45. (-np.geomspace(0.1, 1, 10), 7, 8),
  46. # Evenly spaced parameter values lead to a ratio of 1.
  47. (np.linspace(0, 1, 5), 0.9, 1.1),
  48. # This is not exactly spaced on a log scale but we will benefit from treating
  49. # it as such for visualization.
  50. ([1, 2, 5, 10, 20, 50], 20, 40),
  51. ],
  52. )
  53. def test_inverval_max_min_ratio(data, lower_bound, upper_bound):
  54. assert lower_bound < _interval_max_min_ratio(data) < upper_bound