Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace Phpml\Dataset;
   6  
   7  use Phpml\Exception\InvalidArgumentException;
   8  
   9  class ArrayDataset implements Dataset
  10  {
  11      /**
  12       * @var array
  13       */
  14      protected $samples = [];
  15  
  16      /**
  17       * @var array
  18       */
  19      protected $targets = [];
  20  
  21      /**
  22       * @throws InvalidArgumentException
  23       */
  24      public function __construct(array $samples, array $targets)
  25      {
  26          if (count($samples) !== count($targets)) {
  27              throw new InvalidArgumentException('Size of given arrays does not match');
  28          }
  29  
  30          $this->samples = $samples;
  31          $this->targets = $targets;
  32      }
  33  
  34      public function getSamples(): array
  35      {
  36          return $this->samples;
  37      }
  38  
  39      public function getTargets(): array
  40      {
  41          return $this->targets;
  42      }
  43  
  44      /**
  45       * @param int[] $columns
  46       */
  47      public function removeColumns(array $columns): void
  48      {
  49          foreach ($this->samples as &$sample) {
  50              $this->removeColumnsFromSample($sample, $columns);
  51          }
  52      }
  53  
  54      private function removeColumnsFromSample(array &$sample, array $columns): void
  55      {
  56          foreach ($columns as $index) {
  57              unset($sample[$index]);
  58          }
  59  
  60          $sample = array_values($sample);
  61      }
  62  }