sen趋势分析和mk检验 利用Python进行显著性重分类
时间: 2025-06-22 22:39:25 浏览: 19
### 使用Python实现Sen趋势分析与Mann-Kendall (MK) 检验
对于时间序列数据分析中的Sen趋势分析和Mann-Kendall检验,可以借助`scipy.stats`库来完成Theil-Sen斜率估计以及通过专门的包如`pymannkendall`来进行Mann-Kendall测试。下面展示了一个完整的流程,包括数据准备、执行统计检测、解释结果并基于显著性水平对原始数据进行重分类。
#### 数据准备
假设有一个表示某变量随时间变化的时间序列列表或数组形式的数据集:
```python
import numpy as np
from scipy import stats
import pymannkendall as mk
time_series_data = [0.3, 0.45, 0.67, ... , 0.89] # 用户需替换实际数值
```
#### 执行Theil-Sen Slope Estimation
为了计算Sen's slope(即Theil-Sen median slope),可调用SciPy提供的函数:
```python
sen_slope_result = stats.theilslopes(time_series_data)
print(f"The estimated Sen's slope is {sen_slope_result[0]} with intercept at origin of {sen_slope_result[1]}")
```
此部分代码会输出所估算出来的Sen’s slope及其对应的截距值[^1].
#### 进行Mann-Kendall Test
接着应用Mann-Kendall test评估是否存在单调的趋势:
```python
mk_test_result = mk.original_test(time_series_data)
if mk_test_result.trend == 'increasing':
print('There exists an increasing trend.')
elif mk_test_result.trend == 'decreasing':
print('There exists a decreasing trend.')
else:
print('No significant trend found.')
print(f"P-value from MK test: {mk_test_result.p}")
```
上述脚本不仅报告了趋势的方向还提供了P值用于判断该结论是否具有统计学意义[^2].
#### 显著性重分类
最后一步是对原有时序数据按照设定好的阈值条件实施重新分类操作。这里假定当且仅当P<0.05时认为存在明显的变化倾向,则可以根据这个标准调整原有标签:
```python
significance_threshold = 0.05
def reclassify(data_point, p_value):
if p_value < significance_threshold and data_point >= sen_slope_result[1]:
return "Significant Increase"
elif p_value < significance_threshold and data_point <= sen_slope_result[1]:
return "Significant Decrease"
else:
return "Non-significant"
reclassified_labels = list(map(lambda dp: reclassify(dp, mk_test_result.p), time_series_data))
```
这段程序定义了一种简单的逻辑去区分哪些观测属于重要增长/减少类别而其他则标记为无重要意义的情况。
阅读全文
相关推荐

















