数据平稳性检测
程序员文章站
2023-12-31 18:02:34
...
转自 Python 数据分析与数据挖掘
目的:确定数据序列是否有随机趋势或确定趋势,防止伪回归
如果不平稳,则对其进行差分处理一直到平稳为止
代码:
#平稳性检测
from statsmodels.tsa.stattools import adfuller as ADF
diff = 0
adf = ADF(data['CWXT_DB:184:D:\\']) #取得平稳性检测指标
while adf[1] > 0.05:#当不平稳的时候
diff = diff + 1#差分数比如原始序列1,2,3 差分一次为2-1=1,3-2=1,差分两次为1-1=0
adf = ADF(data['CWXT_DB:184:D:\\'].diff(diff).dropna())#diff 表示差分,参数为次数,dropna表示去除NA值 缺失值
print(u'原始序列经过%s阶差分后归于平稳,p值为%s' %(diff, adf[1]))