beta = $beta; } /** * Return a tuple of the min and max output value for this metric. * * @return \Rubix\ML\Tuple{float,float} */ public function range() : Tuple { return new Tuple(0.0, 1.0); } /** * The estimator types that this metric is compatible with. * * @internal * * @return list */ public function compatibility() : array { return [ EstimatorType::classifier(), EstimatorType::anomalyDetector(), ]; } /** * Score a set of predictions. * * @param list $predictions * @param list $labels * @return float */ public function score(array $predictions, array $labels) : float { PredictionAndLabelCountsAreEqual::with($predictions, $labels)->check(); if (empty($predictions)) { return 0.0; } $classes = array_unique(array_merge($predictions, $labels)); $truePos = $falsePos = $falseNeg = array_fill_keys($classes, 0); foreach ($predictions as $i => $prediction) { $label = $labels[$i]; if ($prediction == $label) { ++$truePos[$prediction]; } else { ++$falsePos[$prediction]; ++$falseNeg[$label]; } } $precision = Stats::mean(array_map([self::class, 'precision'], $truePos, $falsePos)); $recall = Stats::mean(array_map([self::class, 'recall'], $truePos, $falseNeg)); return (1.0 + $this->beta ** 2) * $precision * $recall / (($this->beta ** 2 * $precision + $recall) ?: EPSILON); } /** * Return the string representation of the object. * * @internal * * @return string */ public function __toString() : string { return "F Beta (beta: {$this->beta})"; } }