IrisTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace MathPHP\Tests\SampleData;
  3. use MathPHP\SampleData;
  4. class IrisTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /** @var SampleData\Iris */
  7. private $iris;
  8. public function setUp(): void
  9. {
  10. $this->iris = new SampleData\Iris();
  11. }
  12. /**
  13. * @test 150 observations
  14. */
  15. public function testDataHas150Observations()
  16. {
  17. // When
  18. $data = $this->iris->getData();
  19. // Then
  20. $this->assertCount(150, $data);
  21. }
  22. /**
  23. * @test 5 variables
  24. */
  25. public function testDataHas5Variables()
  26. {
  27. // When
  28. $data = $this->iris->getData();
  29. // Then
  30. foreach ($data as $observation) {
  31. $this->assertCount(5, $observation);
  32. }
  33. }
  34. /**
  35. * @test Labeled data
  36. * @dataProvider dataProviderForLabeledData
  37. * @param int $i
  38. * @param array $expectedData
  39. */
  40. public function testLabeledData(int $i, array $expectedData)
  41. {
  42. // When
  43. $labeledData = $this->iris->getLabeledData();
  44. // Then
  45. $this->assertEquals($expectedData, $labeledData[$i]);
  46. }
  47. /**
  48. * @return array (model, data)
  49. */
  50. public function dataProviderForLabeledData(): array
  51. {
  52. return [
  53. [
  54. 0,
  55. [
  56. 'sepalLength' => 5.1,
  57. 'sepalWidth' => 3.5,
  58. 'petalLength' => 1.4,
  59. 'petalWidth' => 0.2,
  60. 'species' => 'setosa',
  61. ]
  62. ],
  63. [
  64. 74,
  65. [
  66. 'sepalLength' => 6.4,
  67. 'sepalWidth' => 2.9,
  68. 'petalLength' => 4.3,
  69. 'petalWidth' => 1.3,
  70. 'species' => 'versicolor',
  71. ]
  72. ],
  73. [
  74. 149,
  75. [
  76. 'sepalLength' => 5.9,
  77. 'sepalWidth' => 3.0,
  78. 'petalLength' => 5.1,
  79. 'petalWidth' => 1.8,
  80. 'species' => 'virginica',
  81. ]
  82. ],
  83. ];
  84. }
  85. /**
  86. * @test 150 sepal length observations
  87. */
  88. public function testNumberOfSepalLength()
  89. {
  90. // When
  91. $observations = $this->iris->getSepalLength();
  92. // Then
  93. $this->assertCount(150, $observations);
  94. }
  95. /**
  96. * @test 150 sepal width observations
  97. */
  98. public function testNumberOfSepalWidth()
  99. {
  100. // When
  101. $observations = $this->iris->getSepalWidth();
  102. // Then
  103. $this->assertCount(150, $observations);
  104. }
  105. /**
  106. * @test 150 petal length observations
  107. */
  108. public function testNumberOfPetalLength()
  109. {
  110. // When
  111. $observations = $this->iris->getPetalLength();
  112. // Then
  113. $this->assertCount(150, $observations);
  114. }
  115. /**
  116. * @test 150 petal width observations
  117. */
  118. public function testNumberOfPetalWidth()
  119. {
  120. // When
  121. $observations = $this->iris->getPetalWidth();
  122. // Then
  123. $this->assertCount(150, $observations);
  124. }
  125. /**
  126. * @test 150 species observations
  127. */
  128. public function testNumberOfSpecies()
  129. {
  130. // When
  131. $observations = $this->iris->getSpecies();
  132. // Then
  133. $this->assertCount(150, $observations);
  134. }
  135. }