机器学习模型预测结果稳定性监控方案
核心监控指标配置
1. 预测值分布监控
import numpy as np
from scipy import stats
def monitor_prediction_distribution(y_true, y_pred):
# 计算预测值的均值和标准差
pred_mean = np.mean(y_pred)
pred_std = np.std(y_pred)
# 检测分布异常(使用Z-score)
z_scores = np.abs((y_pred - pred_mean) / pred_std)
outliers = np.where(z_scores > 3)[0]
return {
'mean': pred_mean,
'std': pred_std,
'outlier_count': len(outliers),
'distribution_change': len(outliers) > len(y_pred) * 0.05 # 超过5%视为异常
}
2. 模型性能指标监控
from sklearn.metrics import mean_squared_error, mean_absolute_error
def monitor_model_performance(y_true, y_pred):
return {
'mse': mean_squared_error(y_true, y_pred),
'mae': mean_absolute_error(y_true, y_pred),
'rmse': np.sqrt(mean_squared_error(y_true, y_pred))
}
告警配置方案
阈值告警规则:
- MSE异常:当MSE > 历史均值 * 2 且持续3个周期
- 预测分布偏移:当预测值标准差变化率 > 20% 且连续2天
自动化告警脚本:
#!/bin/bash
# monitoring.sh
# 检查模型性能是否异常
if [ $(cat /tmp/mse_history | tail -n 1) -gt $(cat /tmp/mse_history | head -n 1) ]; then
echo "MSE异常增长,触发告警" | mail -s "ML模型异常" admin@company.com
fi
监控面板配置:
- 预测分布图(30分钟更新)
- 性能指标趋势图(每小时更新)
- 异常检测结果统计(实时)

讨论