k = $k; $this->backend = new Serial(); } /** * Test the estimator with the supplied dataset and return a validation score. * * @param Learner $estimator * @param Labeled $dataset * @param Metric $metric * @throws InvalidArgumentException * @return float */ public function test(Learner $estimator, Labeled $dataset, Metric $metric) : float { EstimatorIsCompatibleWithMetric::with($estimator, $metric)->check(); $dataset->randomize(); $folds = $dataset->labelType()->isCategorical() ? $dataset->stratifiedFold($this->k) : $dataset->fold($this->k); $this->backend->flush(); for ($i = 0; $i < $this->k; ++$i) { $training = Labeled::quick(); foreach ($folds as $j => $fold) { if ($i !== $j) { $training = $training->merge($fold); } } $testing = $folds[$i]; $this->backend->enqueue( new TrainAndValidate($estimator, $training, $testing, $metric) ); } $scores = $this->backend->process(); return Stats::mean($scores); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return "K Fold (k: {$this->k})"; } }