Public account: You and the cabin by: Peter Editor: Peter

Hello, I’m Peter

Kats (Kits to Analyze Time Series) is a lightweight, easy-to-use, extensible, and versatile framework for timing analysis in Python, open-source by Facebook.

TimeSeriesData is the basic data structure representing univariate and multivariable time series in Kats. There are two initialization methods:

  • TimeSeriesData(df) : Pd. DataFrame object that requires a “time” column and any value column
  • TimeSeriesData(time, value) : where “time” is pd.Series or pd.DatetimeIndex and the value is pd.Series or pd.DataFrame.

The installation

Install on Mac is very simple, direct PIP install; If it is a Windows system, please baidu to solve various dependency problems:

pip install --upgrade pip
pip install kats
Copy the code

GitHub: github.com/facebookres…

API address: facebookresearch. Making. IO/katz/API /

Import libraries

In [1]:

# import libraries
import numpy as np
import pandas as pd
from datetime import datetime, timedelta

import matplotlib.pyplot as plt
%matplotlib inline

import warnings
warnings.filterwarnings('ignore')

from kats.consts import TimeSeriesData
Copy the code

Univariate prediction

Use the built-in air_passengers. CSV data set.

Import data

In [2]:

#./ represents the current directory

air = pd.read_csv("./Kats/kats/data/air_passengers.csv")
air.head()
Copy the code

Field renaming

Make sure you have the time field:

Method 1: Create a TimeSeriesData object

In [7]:

Type (air) # before conversionCopy the code

Out[7]:

pandas.core.frame.DataFrame
Copy the code

In [8]:

Air_ts = TimeSeriesData(AIRCopy the code

In [9]:

Type (air_ts) # after conversionCopy the code

Out[9]:

kats.consts.TimeSeriesData
Copy the code

In [10]:

print(type(air_ts.time))
print(type(air_ts.value))
<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
Copy the code

Method 2: Create TimeSeriesData(time,value)

In [11]:

air_ts_from_series = TimeSeriesData(time=air.time, value=air.value)
air_ts_from_series
Copy the code

TimeSeriesData object operation

The TimeSeriesData object supports many operations like pd.dataframe:

  • Slicing
  • Mathematical calculations
  • Built-in plot() method plotting
  • Some commonly used built-in functions are used

Slice access

Mathematical operations

When two TimeSeries objects are added, the time column must have the same value:

In [16]:

air_ts[2:8] + air_ts_from_series[2:8] 
Copy the code

If the values of the time column are different, an error is reported:

logic

In [18]:

air_ts == air_ts_from_series
Copy the code

Out[18]:

True
Copy the code

Calculate the length of the

In [19]:

len(air_ts)
Copy the code

Out[19]:

144
Copy the code

Commonly used attributes

In [20]:

air_ts.max
Copy the code

Out[20]:

622
Copy the code

In [21]:

air_ts.min
Copy the code

Out[21]:

104
Copy the code

In [22]:

Is_univariate air_ts.is_univariate()Copy the code

Out[22]:

True
Copy the code

In [23]:

Air_ts.is_empty ()Copy the code

Out[23]:

False
Copy the code

In [24]:

Air_ts.is_data_missing ()Copy the code

Out[24]:

False
Copy the code

To a DataFrame object

In [25]:

air_to_df = air_ts.to_dataframe()
air_to_df.head()
Copy the code

Extend extend(emphasis)

Extension to two different TimeSeries objects on axis=0

In [26]:

K2 = air_ts[5:8] # 3 k2 = air_ts[5:8] # 3 k1.extend(k2)Copy the code

Note that the dates of the two objects must be concatenated:

drawing

Plot TimeSeriesData objects in Kats directly using the plot method

In [29]:

air_ts.plot()

plt.show()
Copy the code

Predictions based on Kats

Kats currently supports a variety of prediction algorithms, common ones are:

  • Linear
  • Quadratic
  • ARIMA (Emphasis)
  • SARIMA
  • Holt-Winters
  • Prophet (Emphasis)
  • AR-Net
  • LSTM (Emphasis)
  • Theta
  • VAR

Use fit and predict functions to complete the basic prediction function.

Use the Prophet

Build the process of simulation + prediction:

In [30]:

In the following forecast results, FCST is the mean of the forecast, FCST_lower is the lower limit of the forecast, and FCST_upper is the upper limit of the forecast

Draw a visual graph of the predicted results:

In [31]:

model.plot()

plt.show()
Copy the code

Add in the historical data and let’s make another prediction:

from kats.models.prophet import ProphetModel, ProphetParams

params = ProphetParams(seasonality_mode="multiplicative", interval_width=0.8)
model = ProphetModel(data=air_ts, params=params)
model.fit()

Include_history =True
forecast = model.predict(steps=24, freq="MS",include_history=True)
model.plot()
Copy the code

The black line is the raw data, and the blue line is the prediction

Use the LSTM

Use LSTM model to predict again:

In [33]:

By comparing Prophet and LSTM models, we find that Prophet has a better trend

Multivariate prediction

Import data

Create a TimeSeriesData object

Method 1

In [4]:

multi_ts = TimeSeriesData(df)
Copy the code

In [5]:

type(multi_ts.time)
Copy the code

Out[5]:

pandas.core.series.Series
Copy the code

In [6]:

type(multi_ts.value)
Copy the code

Out[6]:

pandas.core.frame.DataFrame
Copy the code

Way 2

In [7]:

multi_ts_two = TimeSeriesData(time=df.time,value=df[["v1","v2"]])
multi_ts_two
Copy the code

Raw data mapping

In [10]:

multi_ts.plot(cols=["v1","v2"])

plt.show()
Copy the code

Multivariate prediction

The predicted results are as follows:

Shanghai outbreak

Manually collate the data of Shanghai in the last 30 days:

Read data:

drawing

1. Forecast results added daily:

Specific values are:

2. Forecast trend chart of 7-day mean

The specific value is:

We will verify this result later!

Time series data

Main contents to learn in the future:

1. Model framework: Prophet + Kats + ARIMA

2, Recommend an Intel timing analysis course: www.intel.cn/content/www…

3. Books: FPP forecasting: Methods and Practices (2nd edition left, 3rd edition right), 2nd Edition Chinese online address: otexts.com/fppcn/