{ "slug": "balanced-accuracy-macro-recall", "title": "Balanced Accuracy 类别不平衡二分类平衡准确率", "difficulty": "Easy", "difficulty_breakdown": { "concept": 2, "implementation": 2, "math_derivation": 2, "boundary_complexity": 2 }, "category": "ML", "tags": [ "Metrics", "BalancedAccuracy", "ImbalancedLearning", "TorchLeet" ], "description_md": "### 题目描述\n计算类别不平衡二分类任务中的敏感度(Sensitivity ($\\text{TPR} = \\frac{TP}{TP + FN}$))、特异度(Specificity ($\\text{TNR} = \\frac{TN}{TN + FP}$))以及平衡准确率 $\\text{BalancedAcc} = \\frac{1}{2}(\\text{TPR} + \\text{TNR})$,返回 `(tpr, tnr, balanced_acc)`。\n\n### 函数签名\n```python\ndef balanced_accuracy_binary(\n tp: int,\n tn: int,\n fp: int,\n fn: int,\n eps: float = 1e-12,\n) -> Tuple[float, float, float]:\n```\n\n### 示例\n```python\n# 输入\ntp = 0\ntn = 990\nfp = 0\nfn = 10\neps = 1e-12\n\n# 输出 (tuple)\n(0.0, 1.0, 0.5)\n```\n\n### 参数规格与约束\n- **`tp`**:`int`\n- **`tn`**:`int`\n- **`fp`**:`int`\n- **`fn`**:`int`\n- **`eps`**:`float`(默认 `1e-12`)\n- **返回值**:`Tuple[float, float, float]`,输出规格 `Tuple` (`float` (示例值: `0.0`), `float` (示例值: `1.0`), `float` (示例值: `0.5`))\n- **精度容差**:`atol=0.0001, rtol=0.0001`\n> 题目来源:改编自 [TorchLeet](https://github.com/Exorust/TorchLeet)(MIT License),已改写为函数式评测接口与中文题面。", "entry_point": "balanced_accuracy_binary", "starter_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import List, Tuple, Dict, Optional\n\ndef balanced_accuracy_binary(\n tp: int,\n tn: int,\n fp: int,\n fn: int,\n eps: float = 1e-12,\n) -> Tuple[float, float, float]:\n # 请在此处编写你的实现代码\n pass\n", "reference_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import List, Tuple, Dict, Optional\n\ndef balanced_accuracy_binary(tp: int, tn: int, fp: int, fn: int, eps: float = 1e-12) -> Tuple[float, float, float]:\n tpr = float(tp) / (float(tp + fn) + eps)\n tnr = float(tn) / (float(tn + fp) + eps)\n return tpr, tnr, 0.5 * (tpr + tnr)\n", "test_code": "import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import List, Tuple, Dict, Optional\n\ncases = [\n (\"多数类全预测为负时 BalancedAcc 仅为 0.5\", 0, 990, 0, 10),\n (\"完美预测 1.0\", 50, 500, 0, 0),\n (\"均衡错误\", 40, 80, 20, 10),\n (\"高召回低特异度\", 95, 50, 50, 5),\n]\nfor idx, (name, *args) in enumerate(cases, 1):\n t0 = time.time()\n try:\n exp = ref_obj(*args)\n act = user_obj(*args)\n match, err, max_diff = compare_outputs(act, exp, atol=atol, rtol=rtol)\n passed, msg = match, err\n except Exception as e:\n passed, msg = False, f\"Runtime Exception: {str(e)}\"\n act, exp, max_diff = None, None, None\n cases_results.append({\n \"case_id\": idx, \"case_name\": name, \"passed\": passed,\n \"time_ms\": round((time.time() - t0) * 1000, 2), \"message\": msg,\n \"user_output_summary\": summarize_obj(act) if act is not None else None,\n \"expected_summary\": summarize_obj(exp) if exp is not None else None,\n \"max_diff\": max_diff,\n })\n", "test_cases": [ { "name": "多数类全预测为负时 BalancedAcc 仅为 0.5", "input_desc": "真实参数契约用例 (tp, tn, fp, fn, eps)", "params": [ { "name": "tp", "type": "int", "value": "0" }, { "name": "tn", "type": "int", "value": "990" }, { "name": "fp", "type": "int", "value": "0" }, { "name": "fn", "type": "int", "value": "10" }, { "name": "eps", "type": "float", "value": "1e-12" } ], "raw_inputs": { "tp": "0", "tn": "990", "fp": "0", "fn": "10", "eps": "1e-12" } }, { "name": "完美预测 1.0", "input_desc": "真实参数契约用例 (tp, tn, fp, fn, eps)", "params": [ { "name": "tp", "type": "int", "value": "50" }, { "name": "tn", "type": "int", "value": "500" }, { "name": "fp", "type": "int", "value": "0" }, { "name": "fn", "type": "int", "value": "0" }, { "name": "eps", "type": "float", "value": "1e-12" } ], "raw_inputs": { "tp": "50", "tn": "500", "fp": "0", "fn": "0", "eps": "1e-12" } }, { "name": "均衡错误", "input_desc": "真实参数契约用例 (tp, tn, fp, fn, eps)", "params": [ { "name": "tp", "type": "int", "value": "40" }, { "name": "tn", "type": "int", "value": "80" }, { "name": "fp", "type": "int", "value": "20" }, { "name": "fn", "type": "int", "value": "10" }, { "name": "eps", "type": "float", "value": "1e-12" } ], "raw_inputs": { "tp": "40", "tn": "80", "fp": "20", "fn": "10", "eps": "1e-12" } } ], "time_limit": 5.0, "memory_limit_mb": 512, "atol": 0.0001, "rtol": 0.0001, "forbidden_modules": [ "os", "sys", "subprocess", "socket", "shutil", "pathlib", "ctypes", "multiprocessing" ], "forbidden_calls": [ "eval", "exec", "compile", "open", "__import__", "input", "breakpoint" ], "is_featured": false, "editorial_md": "#### 💡 核心算法原理与数学推导\n**Balanced Accuracy 类别不平衡二分类平衡准确率**(`balanced_accuracy_binary`)聚焦于 **ML** 中的关键计算逻辑:\n\n计算类别不平衡二分类任务中的敏感度(Sensitivity ($\\text{TPR} = \\frac{TP}{TP + FN}$))、特异度(Specificity ($\\text{TNR} = \\frac{TN}{TN + FP}$))以及平衡准确率 $\\text{BalancedAcc} = \\frac{1}{2}(\\text{TPR} + \\text{TNR})$,返回 `(tpr, tnr, balanced_acc)`。\n\n在实现中,函数接收参数 `tp` (`int`)、`tn` (`int`)、`fp` (`int`)、`fn` (`int`)、`eps` (`float`),目标是计算出类型与形状严格符合 `Tuple[float, float, float]` 契约的结果。\n\n#### 📐 输入输出张量变换与步骤拆解\n- **输入基准规格**:`tp` 规格为 `int` (示例值: `0`),`tn` 规格为 `int` (示例值: `990`),`fp` 规格为 `int` (示例值: `0`),`fn` 规格为 `int` (示例值: `10`)。\n- **期望输出规格**:`Tuple` (`float` (示例值: `0.0`), `float` (示例值: `0.999999999999999`), `float` (示例值: `0.4999999999999995`))。\n\n**具体推导与计算步骤如下:**\n1. 计算中间量 `tpr`:通过表达式 `float(tp) / (float(tp + fn) + eps)` 完成张量或数值变换。\n2. 计算中间量 `tnr`:通过表达式 `float(tn) / (float(tn + fp) + eps)` 完成张量或数值变换。\n3. 返回最终结果:输出 `tpr, tnr, 0.5 * (tpr + tnr)`,严格对齐函数签名要求的返回类型与维度。\n\n#### 📊 复杂度分析\n- **时间复杂度**:$O(1)$,仅涉及标量代数运算或闭式公式求值,执行耗时与规模参数大小无关。\n- **空间复杂度**:$O(1)$,仅使用常数级标量局部变量,无额外张量显存分配。\n\n#### ⚠️ 关键实现细节与易错点\n- **除零保护与数值平滑 (`eps`)**:在执行分母归一化、标准差缩放或对数运算时,需加上 `eps` 或执行 `clamp_min` 防止除零产生 `NaN` / `Inf`。\n- **边界输入鲁棒性**:注意单样本(`Batch=1`)、零值输入或极端超参数下的计算行为,禁止硬编码特定测试用例常量。\n- **向量化算子优先**:优先使用 PyTorch 原生向量化张量算子,避免低效的逐元素循环。\n\n#### 💻 官方标准参考实现\n```python\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport math\nfrom typing import List, Tuple, Dict, Optional\n\ndef balanced_accuracy_binary(tp: int, tn: int, fp: int, fn: int, eps: float = 1e-12) -> Tuple[float, float, float]:\n tpr = float(tp) / (float(tp + fn) + eps)\n tnr = float(tn) / (float(tn + fp) + eps)\n return tpr, tnr, 0.5 * (tpr + tnr)\n```" }