UNPKG

@arizeai/phoenix-evals

Version:
44 lines 1.95 kB
import { computePrecisionRecallFScore, formatBetaForMetricName, getAverageMetricNameSuffix, } from "./classificationMetrics.js"; import { createClassificationMetricEvaluator } from "./createClassificationMetricEvaluator.js"; /** * Wraps `computePrecisionRecallFScore` so repeated calls with the same * `example` object (by reference) reuse the first computed result, instead * of recomputing the full confusion matrix once per evaluator. */ function createCachedComputer() { const cache = new WeakMap(); return (example, options) => { const cached = cache.get(example); if (cached) { return cached; } const result = computePrecisionRecallFScore(example, options); cache.set(example, result); return result; }; } /** * Creates matching precision, recall, and F-beta evaluators from a single set * of options, so all three are computed with the same `average`, `beta`, * `positiveLabel`, and `zeroDivision` settings. When the same `expected`/ * `output` example object is passed to all three evaluators, the underlying * confusion matrix is only computed once and shared across them. * * @example * ```typescript * const { precision, recall, fScore } = createPrecisionRecallFScoreEvaluators({ * average: "weighted", * }); * ``` */ export function createPrecisionRecallFScoreEvaluators(options = {}) { const { beta = 1 } = options; const suffix = getAverageMetricNameSuffix(options); const compute = createCachedComputer(); return { precision: createClassificationMetricEvaluator(`precision${suffix}`, "precision", options, compute), recall: createClassificationMetricEvaluator(`recall${suffix}`, "recall", options, compute), fScore: createClassificationMetricEvaluator(`${formatBetaForMetricName(beta)}${suffix}`, "fScore", options, compute), }; } //# sourceMappingURL=createPrecisionRecallFScoreEvaluators.js.map