Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace Phpml;
   6  
   7  class Pipeline implements Estimator
   8  {
   9      /**
  10       * @var Transformer[]
  11       */
  12      private $transformers = [];
  13  
  14      /**
  15       * @var Estimator
  16       */
  17      private $estimator;
  18  
  19      /**
  20       * @param Transformer[] $transformers
  21       */
  22      public function __construct(array $transformers, Estimator $estimator)
  23      {
  24          foreach ($transformers as $transformer) {
  25              $this->addTransformer($transformer);
  26          }
  27  
  28          $this->estimator = $estimator;
  29      }
  30  
  31      public function addTransformer(Transformer $transformer): void
  32      {
  33          $this->transformers[] = $transformer;
  34      }
  35  
  36      public function setEstimator(Estimator $estimator): void
  37      {
  38          $this->estimator = $estimator;
  39      }
  40  
  41      /**
  42       * @return Transformer[]
  43       */
  44      public function getTransformers(): array
  45      {
  46          return $this->transformers;
  47      }
  48  
  49      public function getEstimator(): Estimator
  50      {
  51          return $this->estimator;
  52      }
  53  
  54      public function train(array $samples, array $targets): void
  55      {
  56          foreach ($this->transformers as $transformer) {
  57              $transformer->fit($samples, $targets);
  58              $transformer->transform($samples);
  59          }
  60  
  61          $this->estimator->train($samples, $targets);
  62      }
  63  
  64      /**
  65       * @return mixed
  66       */
  67      public function predict(array $samples)
  68      {
  69          $this->transformSamples($samples);
  70  
  71          return $this->estimator->predict($samples);
  72      }
  73  
  74      private function transformSamples(array &$samples): void
  75      {
  76          foreach ($this->transformers as $transformer) {
  77              $transformer->transform($samples);
  78          }
  79      }
  80  }