= 1.0) { throw new InvalidArgumentException('Ratio must be' . " between 0 and 1, $ratio given."); } $this->simulations = $simulations; $this->ratio = $ratio; $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 RuntimeException * @return float */ public function test(Learner $estimator, Labeled $dataset, Metric $metric) : float { EstimatorIsCompatibleWithMetric::with($estimator, $metric)->check(); if ($dataset->numSamples() * $this->ratio < 1) { throw new RuntimeException('Dataset does not contain' . ' enough records to create a validation set with a' . " hold out ratio of {$this->ratio}."); } $stratify = $dataset->labelType()->isCategorical(); $this->backend->flush(); for ($i = 0; $i < $this->simulations; ++$i) { $dataset->randomize(); [$testing, $training] = $stratify ? $dataset->stratifiedSplit($this->ratio) : $dataset->split($this->ratio); $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 "Monte Carlo (simulations: {$this->simulations}, ratio: {$this->ratio})"; } }