简介
时间序列分析建模是数据科学和机器学习的一个重要的领域,在电子商务、金融、供应链管理、医学、气象、能源、天文等诸多领域有着广泛的应用。而对于时间序列的分析以及建模目前也有非常多的技术,但相对散乱,本次FaceBook开源了Kats,它是第一个开发标准并连接时间序列分析各个领域的综合Python库,用户可以在这里探索其时间序列数据的基本特征,预测未来值,监视异常,并将其合并到ML模型和pipeline中。
在上个月,FaceBook开园了一款新的分析时间序列数据的library--Kats,Kats是以款:
包括
据我们所知,Kats是第一个用于一般时间序列分析的综合Python库,它提供了经典和高级的时间序列数据建模技术。
Kats
Kats是一个分析时间序列数据的工具箱,特点是:
可以用来执行时间序列分析。
Kats的核心四大功能包括:
代 码
01
预测案例
# !pip install kats
import pandas as pd
from kats.consts import TimeSeriesData
from kats.models.prophet import ProphetModel, ProphetParams
# take `air_passengers` data as an example
air_passengers_df = pd.read_csv(
"./data/air_passengers.csv",
header=0,
names=["time", "passengers"],
)
air_passengers_df.head()
time | passengers | |
---|---|---|
0 | 1949-01-01 | 112 |
1 | 1949-02-01 | 118 |
2 | 1949-03-01 | 132 |
3 | 1949-04-01 | 129 |
4 | 1949-05-01 | 121 |
# convert to TimeSeriesData object
air_passengers_ts = TimeSeriesData(air_passengers_df)
# create a model param instance
params = ProphetParams(seasonality_mode='multiplicative') # additive mode gives worse results
# create a prophet model instance
m = ProphetModel(air_passengers_ts, params)
# fit model simply by calling m.fit()
m.fit()
# make prediction for next 30 month
fcst = m.predict(steps=30, freq="MS")
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
fcst.tail()
time | fcst | fcst_lower | fcst_upper | |
---|---|---|---|---|
25 | 1963-02-01 | 504.226854 | 488.814245 | 518.102516 |
26 | 1963-03-01 | 585.357522 | 569.323618 | 600.734632 |
27 | 1963-04-01 | 563.669453 | 548.299180 | 578.251332 |
28 | 1963-05-01 | 586.795969 | 571.121988 | 602.562586 |
29 | 1963-06-01 | 668.096474 | 650.864940 | 685.632700 |
02
时间序列检测案例
import numpy as np
from kats.consts import TimeSeriesData
from kats.detectors.cusum_detection import CUSUMDetector
# simulate time series with increase
np.random.seed(10)
df_increase = pd.DataFrame(
{
'time': pd.date_range('2019-01-01', '2019-03-01'),
'increase':np.concatenate([np.random.normal(1,0.2,30), np.random.normal(2,0.2,30)]),
}
)
# convert to TimeSeriesData object
timeseries = TimeSeriesData(df_increase)
# run detector and find change points
change_points = CUSUMDetector(timeseries).detector()
df_increase.groupby('time')['increase'].first().plot()
change_points
[(TimeSeriesChangePoint(start_time: 2019-01-30 00:00:00, end_time: 2019-01-30 00:00:00, confidence: 1.0),
<kats.detectors.cusum_detection.CUSUMMetadata at 0x7fa9449fe910>)]
03
时间序列特征
# Initiate feature extraction class
from kats.tsfeatures.tsfeatures import TsFeatures
# take `air_passengers` data as an example
air_passengers_df = pd.read_csv(
"./data/air_passengers.csv",
header=0,
names=["time", "passengers"],
)
# convert to TimeSeriesData object
air_passengers_ts = TimeSeriesData(air_passengers_df)
# calculate the TsFeatures
features = TsFeatures().transform(air_passengers_ts)
features
{'length': 144,
'mean': 280.2986111111111,
'var': 14291.97333140432,
'entropy': 0.4287365561752448,
'lumpiness': 3041164.5629058965,
'stability': 12303.627266589507,
'flat_spots': 2,
'hurst': -0.08023291030513455,
'std1st_der': 27.206287853461966,
'crossing_points': 7,
'binarize_mean': 0.4444444444444444,
'unitroot_kpss': 0.12847508180149445,
'heterogeneity': 126.06450625819339,
'histogram_mode': 155.8,
'linearity': 0.853638165603188,
'trend_strength': 0.9383301875692747,
'seasonality_strength': 0.3299338017939569,
'spikiness': 111.69732482853489,
'peak': 6,
'trough': 3,
'level_shift_idx': 118,
'level_shift_size': 15.599999999999966,
'y_acf1': 0.9480473407524915,
'y_acf5': 3.392072131604336,
'diff1y_acf1': 0.30285525815216935,
'diff1y_acf5': 0.2594591065999471,
'diff2y_acf1': -0.19100586757092733,
'diff2y_acf5': 0.13420736423784568,
'y_pacf5': 1.0032882494015292,
'diff1y_pacf5': 0.21941234780081417,
'diff2y_pacf5': 0.2610103428699484,
'seas_acf1': 0.6629043863684492,
'seas_pacf1': 0.1561695525558896,
'firstmin_ac': 8,
'firstzero_ac': 52,
'holt_alpha': 0.995070674288148,
'holt_beta': 0.0042131109997650676,
'hw_alpha': 0.9999999850988388,
'hw_beta': 6.860710750514223e-16,
'hw_gamma': 1.3205838720422503e-08}
参考文献