You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_roc.py 3.0 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. # """test_roc"""
  16. import numpy as np
  17. import pytest
  18. from mindspore import Tensor
  19. from mindspore.nn.metrics import ROC
  20. def test_roc():
  21. """test_roc_binary"""
  22. x = Tensor(np.array([[3, 0, 1], [1, 3, 0], [1, 0, 2]]))
  23. y = Tensor(np.array([[0, 2, 1], [1, 2, 1], [0, 0, 1]]))
  24. metric = ROC(pos_label=1)
  25. metric.clear()
  26. metric.update(x, y)
  27. fpr, tpr, thresholds = metric.eval()
  28. assert np.equal(fpr, np.array([0, 0.4, 0.4, 0.6, 1])).all()
  29. assert np.equal(tpr, np.array([0, 0, 0.25, 0.75, 1])).all()
  30. assert np.equal(thresholds, np.array([4, 3, 2, 1, 0])).all()
  31. def test_roc2():
  32. """test_roc_multiclass"""
  33. x = Tensor(np.array([[0.28, 0.55, 0.15, 0.05], [0.10, 0.20, 0.05, 0.05], [0.20, 0.05, 0.15, 0.05],
  34. [0.05, 0.05, 0.05, 0.75]]))
  35. y = Tensor(np.array([0, 1, 2, 3]))
  36. metric = ROC(class_num=4)
  37. metric.clear()
  38. metric.update(x, y)
  39. fpr, tpr, thresholds = metric.eval()
  40. list1 = [np.array([0., 0., 0.33333333, 0.66666667, 1.]), np.array([0., 0.33333333, 0.33333333, 1.]),
  41. np.array([0., 0.33333333, 1.]), np.array([0., 0., 1.])]
  42. list2 = [np.array([0., 1., 1., 1., 1.]), np.array([0., 0., 1., 1.]),
  43. np.array([0., 1., 1.]), np.array([0., 1., 1.])]
  44. list3 = [np.array([1.28, 0.28, 0.2, 0.1, 0.05]), np.array([1.55, 0.55, 0.2, 0.05]),
  45. np.array([1.15, 0.15, 0.05]), np.array([1.75, 0.75, 0.05])]
  46. assert fpr[0].shape == list1[0].shape
  47. assert np.equal(tpr[1], list2[1]).all()
  48. assert np.equal(thresholds[2], list3[2]).all()
  49. def test_roc_update1():
  50. x = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]]))
  51. metric = ROC()
  52. metric.clear()
  53. with pytest.raises(ValueError):
  54. metric.update(x)
  55. def test_roc_update2():
  56. x = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]]))
  57. y = Tensor(np.array([1, 0]))
  58. metric = ROC()
  59. metric.clear()
  60. with pytest.raises(ValueError):
  61. metric.update(x, y)
  62. def test_roc_init1():
  63. with pytest.raises(TypeError):
  64. ROC(pos_label=1.2)
  65. def test_roc_init2():
  66. with pytest.raises(TypeError):
  67. ROC(class_num="class_num")
  68. def test_roc_runtime():
  69. metric = ROC()
  70. metric.clear()
  71. with pytest.raises(RuntimeError):
  72. metric.eval()