Files
AIPortPilot/backend/app/services/predictor.py
T
selfrelease fad458b2a7 docs(uiux): UIUX 设计方案大改 + 5 份作业指导书对齐 + 开发任务文档
- UIUX 文档:填充 19 个缺口(多主体画像/健康度/AI+看板/增长域/洞察域/创始人端/OODA/助推/商密)
- UIUX 文档:插入 6 个新章节(十四~十九),旧章节重编号为二十~三十一,更新目录和交叉引用
- 作业指导书 x5:导航改为 6 域分组,新增 Context Bar/工作模式/Insight Rail/决策线程/多工作区等 UI 概念
- 新建 docs/2-task-uiux.md:50 个代码落地开发任务,按 P0-P6 分优先级 + 8 Sprint 规划
- 后端/前端:大量新增模型、路由、组件(来自之前 Phase 开发)
2026-07-19 11:53:38 +08:00

58 lines
1.5 KiB
Python

"""趋势预测 + 异常检测。"""
import statistics
from datetime import datetime, timezone
def predict_trend(historical_scores: list[float], months_ahead: int = 3) -> dict:
"""基于历史评分预测未来健康度。"""
if len(historical_scores) < 2:
return {"predicted": [], "confidence": 0.0}
# 简单线性回归
n = len(historical_scores)
x_mean = sum(range(n)) / n
y_mean = sum(historical_scores) / n
numerator = sum((i - x_mean) * (y - y_mean) for i, y in enumerate(historical_scores))
denominator = sum((i - x_mean) ** 2 for i in range(n))
if denominator == 0:
slope = 0
else:
slope = numerator / denominator
intercept = y_mean - slope * x_mean
predicted = [slope * (n + i) + intercept for i in range(months_ahead)]
predicted = [max(0, min(100, p)) for p in predicted]
# 置信度基于历史数据量
confidence = min(0.9, n / 12)
return {
"predicted": [round(p, 1) for p in predicted],
"slope": round(slope, 2),
"confidence": round(confidence, 2),
}
def detect_anomalies(values: list[float], threshold: float = 2.0) -> list[int]:
"""统计方法识别指标突变。"""
if len(values) < 3:
return []
mean = statistics.mean(values)
stdev = statistics.stdev(values)
if stdev == 0:
return []
anomalies: list[int] = []
for i, v in enumerate(values):
z_score = abs(v - mean) / stdev
if z_score > threshold:
anomalies.append(i)
return anomalies