= 1.0) { throw new InvalidArgumentException('Ratio must be' . " between 0 and 1, $ratio given."); } $this->ratio = $ratio; } /** * Test the estimator with the supplied dataset and return a validation score. * * @param \Rubix\ML\Learner $estimator * @param \Rubix\ML\Datasets\Labeled $dataset * @param \Rubix\ML\CrossValidation\Metrics\Metric $metric * @throws \Rubix\ML\Exceptions\RuntimeException * @return float */ public function test(Learner $estimator, Labeled $dataset, Metric $metric) : float { EstimatorIsCompatibleWithMetric::with($estimator, $metric)->check(); $dataset->randomize(); [$testing, $training] = $dataset->labelType()->isCategorical() ? $dataset->stratifiedSplit($this->ratio) : $dataset->split($this->ratio); if ($testing->empty()) { throw new RuntimeException('Dataset does not contain' . ' enough records to create a validation set with a' . " hold out ratio of {$this->ratio}."); } $estimator->train($training); $predictions = $estimator->predict($testing); return $metric->score($predictions, $testing->labels()); } /** * Return the string representation of the object. * * @return string */ public function __toString() : string { return "Hold Out (ratio: {$this->ratio})"; } }