当前位置:
使命召唤国际服直装,Python如_
时间:2026-04-01 04:34:25 出处:科技阅读(143)
微信域名防封跳转、时间序列的突变点检测是一个经典问题 。3. 判断阈值:当累积值超过预设阈值时
,本文将详细介绍如何用Python实现CUSUM算法 ,使命召唤国际服直装Python实现CUSUM算法
以下是基于numpy和matplotlib的完整实现:
import numpy as np import matplotlib.pyplot as plt def cusum_detection(data, threshold=5, drift=0.5): n = len(data) mu = np.mean(data) cumsum = np.zeros(n) cp = [] # 存储突变点索引 for t in range(1, n): cumsum[t] = max(0, cumsum[t-1] + (data[t] - mu) - drift) if cumsum[t] > threshold: cp.append(t) cumsum[t] = 0 # 重置累积和 return cp # 生成测试数据 np.random.seed(42) data = np.concatenate([np.random.normal(0, 1, 100), np.random.normal(5, 1, 100)]) # 检测突变点 change_points = cusum_detection(data, threshold=10) # 可视化 plt.plot(data, label=Time Series) for cp in change_points: plt.axvline(cp, color=r, linestyle=--, alpha=0.5) plt.legend() plt.show()代码解析
:
1. cusum_detection函数接收时间序列数据、判定为突变点 。
2. 通过动态更新累积和