FaceBook 開源全網第一個時序王器 --Kats

作者:傑少,煉丹筆記特邀嘉賓

時間序列王器 - Kats

簡介

時間序列分析建模是數據科學和機器學習的一個重要的領域,在電子商務、金融、供應鏈管理、醫學、氣象、能源、天文等諸多領域有着廣泛的應用。而對於時間序列的分析以及建模目前也有非常多的技術,但相對散亂,本次 FaceBook 開源了 Kats,它是第一個開發標準並連接時間序列分析各個領域的綜合 Python 庫,用戶可以在這裏探索其時間序列數據的基本特徵,預測未來值,監視異常,並將其合併到 ML 模型和 pipeline 中

在上個月,FaceBook 開園了一款新的分析時間序列數據的 library--Kats,Kats 是以款:

包括

據我們所知,Kats 是第一個用於一般時間序列分析的綜合 Python 庫,它提供了經典和高級的時間序列數據建模技術。

Kats

1. Kats 的優勢

Kats 是一個分析時間序列數據的工具箱,特點是:

可以用來執行時間序列分析。

2. 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()

R5QHxr

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}

參考文獻

  1. https://engineering.fb.com/2021/06/21/open-source/kats/

  2. https://github.com/facebookresearch/Kats

  3. Kats - Time Series Forecasting By Facebook

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/irdul_yTVm3k-i9CL5Mw1w