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.
<?php

declare(strict_types=1);

namespace Phpml;

< class Pipeline implements Estimator
> use Phpml\Exception\InvalidOperationException; > > class Pipeline implements Estimator, Transformer
{ /** * @var Transformer[] */ private $transformers = []; /**
< * @var Estimator
> * @var Estimator|null
*/ private $estimator; /** * @param Transformer[] $transformers */
< public function __construct(array $transformers, Estimator $estimator) < { < foreach ($transformers as $transformer) { < $this->addTransformer($transformer); < } < < $this->estimator = $estimator; < } < < public function addTransformer(Transformer $transformer): void < { < $this->transformers[] = $transformer; < } < < public function setEstimator(Estimator $estimator): void
> public function __construct(array $transformers, ?Estimator $estimator = null)
{
> $this->transformers = array_map(static function (Transformer $transformer): Transformer { $this->estimator = $estimator; > return $transformer; } > }, $transformers);
/** * @return Transformer[] */ public function getTransformers(): array { return $this->transformers; }
< public function getEstimator(): Estimator
> public function getEstimator(): ?Estimator
{ return $this->estimator; } public function train(array $samples, array $targets): void {
> if ($this->estimator === null) { foreach ($this->transformers as $transformer) { > throw new InvalidOperationException('Pipeline without estimator can\'t use train method'); $transformer->fit($samples, $targets); > } $transformer->transform($samples); >
< $transformer->transform($samples);
> $transformer->transform($samples, $targets);
$this->estimator->train($samples, $targets); } /** * @return mixed */ public function predict(array $samples) {
< $this->transformSamples($samples);
> $this->transform($samples); > > if ($this->estimator === null) { > throw new InvalidOperationException('Pipeline without estimator can\'t use predict method'); > }
return $this->estimator->predict($samples); }
< private function transformSamples(array &$samples): void
> public function fit(array $samples, ?array $targets = null): void > { > foreach ($this->transformers as $transformer) { > $transformer->fit($samples, $targets); > $transformer->transform($samples, $targets); > } > } > > public function transform(array &$samples, ?array &$targets = null): void
{ foreach ($this->transformers as $transformer) {
< $transformer->transform($samples);
> $transformer->transform($samples, $targets);
} } }