Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace Phpml\CrossValidation;
   6  
   7  use Phpml\Dataset\Dataset;
   8  use Phpml\Exception\InvalidArgumentException;
   9  
  10  abstract class Split
  11  {
  12      /**
  13       * @var array
  14       */
  15      protected $trainSamples = [];
  16  
  17      /**
  18       * @var array
  19       */
  20      protected $testSamples = [];
  21  
  22      /**
  23       * @var array
  24       */
  25      protected $trainLabels = [];
  26  
  27      /**
  28       * @var array
  29       */
  30      protected $testLabels = [];
  31  
  32      public function __construct(Dataset $dataset, float $testSize = 0.3, ?int $seed = null)
  33      {
  34          if ($testSize <= 0 || $testSize >= 1) {
  35              throw new InvalidArgumentException('testsize must be between 0.0 and 1.0');
  36          }
  37  
  38          $this->seedGenerator($seed);
  39  
  40          $this->splitDataset($dataset, $testSize);
  41      }
  42  
  43      public function getTrainSamples(): array
  44      {
  45          return $this->trainSamples;
  46      }
  47  
  48      public function getTestSamples(): array
  49      {
  50          return $this->testSamples;
  51      }
  52  
  53      public function getTrainLabels(): array
  54      {
  55          return $this->trainLabels;
  56      }
  57  
  58      public function getTestLabels(): array
  59      {
  60          return $this->testLabels;
  61      }
  62  
  63      abstract protected function splitDataset(Dataset $dataset, float $testSize): void;
  64  
  65      protected function seedGenerator(?int $seed = null): void
  66      {
  67          if ($seed === null) {
  68              mt_srand();
  69          } else {
  70              mt_srand($seed);
  71          }
  72      }
  73  }