社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  机器学习算法

【机器学习】FaceBook开源全网第一个时序王器--Kats。

机器学习初学者 • 5 年前 • 697 次点击  

作者:杰少



时间序列王器-Kats


简介

时间序列分析建模是数据科学和机器学习的一个重要的领域,在电子商务、金融、供应链管理、医学、气象、能源、天文等诸多领域有着广泛的应用。而对于时间序列的分析以及建模目前也有非常多的技术,但相对散乱,本次FaceBook开源了Kats,它是第一个开发标准并连接时间序列分析各个领域的综合Python库,用户可以在这里探索其时间序列数据的基本特征,预测未来值,监视异常,并将其合并到ML模型和pipeline中。

在上个月,FaceBook开园了一款新的分析时间序列数据的library--Kats,Kats是以款:

  • 轻量级的、易于使用的、通用的时间序列分析框架;

包括

  • 预测、异常检测、多元分析和特征提取/嵌入。

据我们所知,Kats是第一个用于一般时间序列分析的综合Python库,它提供了经典和高级的时间序列数据建模技术。

Kats

1. Kats的优势

Kats是一个分析时间序列数据的工具箱,特点是:

  • 轻量级;
  • 易于使用;
  • 通用;

可以用来执行时间序列分析。

2. Kats的功能

Kats的核心四大功能包括:

  • 模型预测:Kats提供了一套完整的预测工具,包括10+个单独的预测模型、ensembling、自监督学习(meta-learning)模型、backtesting、超参数调整和经验预测区间。

  • 检测:Kats支持检测时间序列数据的各种模式的功能,包括季节性、异常值、变化点和缓慢的趋势变化检测。

  • 特征提取与嵌入:Kats中的时间序列特征(TSFeature)提取模块可以产生65个具有明确统计定义的特征,这些特征可以应用于大多数机器学习(ML)模型,如分类和回归。

  • 使用的功能: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()

timepassengers
01949-01-01112
11949-02-01118
21949-03-01132
31949-04-01129
41949-05-01121
# 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()

timefcstfcst_lowerfcst_upper
251963-02-01504.226854488.814245518.102516
261963-03-01585.357522569.323618600.734632
271963-04-01563.669453548.299180578.251332
281963-05-01586.795969571.121988602.562586
291963-06-01668.096474650.864940685.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),
)]

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


往期精彩回顾




本站qq群851320808,加入微信群请扫码:
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/117488