时间序列相信大家都有接触了,废话不说,最近在研究informer 里边涉及一个sktime库 看到后挺方便 放一些学习资料吧(使用sktime进行时间序列分类第2部分_weixin_26704853的博客-CSDN博客这个大佬没有写第一部分 将第一部分分享给大家)
Learn All About Using Sktime For Time Series
Forecasting (Part 1)
What is Sktime?
It is a new open-source
scikit-learns
compatible python library which provides a
unified interface for machine learning
with
time-series
related tasks, these tasks
are closely related to learning-based tasks such as:
Time-Series Forecasting;
Time-Series Regression;
Time-Series Classification.
Sktime
extends the machine learning capabilities of the scikit learns library which is used
to
reduce the complex relation between
Time-Series tasks
into related simpler tasks
and hence it can be solved efficiently and easily.
Performing Reductions with Time-Series
Time-series tasks
are closely related, each with distinct learning settings can be
solved via
Reductions
. Using
Reduction
, any algorithm for a particular task gets
converted into a learning algorithm for the new task and if any changes are made to
the original algorithm, changes are immediately transferred to the new task.
Reductions
usually involve segmenting task to be performed into
simpler sub-tasks
and all the segments solutions are composed to give the solution of the original
task, hence
Reductions
are good ways to reduce confusion between tasks and help
us better understand the relationships between tasks and it can be used to solve
more complicated tasks.
An example of reduction can be solving classical forecasting tasks through time-series
regression via a sliding windows approach and iterating over the forecasting horizon.
Task-Specific Estimators and Transformers
As we all know in
scikit-learn
we use the
fit method
to learn training data for a
model and
predict
to make any predictions based on the model and test-data,
similarly,
estimators
in sktime are task-specific means they extend
scikit-learns
regressor and classifiers to their own time-series as well.
They have their own
estimators
also such as forecasters and supervised forecasters with the same fit and predict
method.
Similar to estimators we have
transformers
in sktime which also consists of a
fit
method to fit and learn the training data and a
transform
method that transforms
the input data.
There are different types of data transformations available for different
types of tasks such as:
1.
Tabular:
Similar to transformers in scikit-learn.
2.
Series-to-Primitives:
Transforms time series for each instance into a primitive
number.
3.
Series-to-Series:
Transforms time series for each instance into a series i.e the output
of the transformation is itself a series instead of a primitive number.
4.
Detrending:
transforms an input time series into a detrended time series in the same
range/domain as the input series.
Time-Series Forecasting with Sktime
Let past observations for a single time-series be represented as:
y = [y(t1), y(t2), ....., y(tn)]
The task here is to learn a forecaster
F
which can make accurate temporal
predictions in future out of all the observations at a given time in point
hj
of the
forecasting horizon, where predictions are represented by:
Y = F(hj)
or
Y = [Y(h1), Y(h2), ....., Y(hj)]
Key points to remember
We can apply any
scikit-learns regressor
to solve our forecasting problem that
means it is
compatible
with scikit-learn python library.
Easy to
tune hyperparameters
like the length of sliding window.
To
tune
and to properly evaluate our model it can
adapt scikit-learn estimators
interface to that of forecasters.
The package is available via PyPI using:
pip install sktime
Importing
all the required packages and the
dataset used for this article is the Box
Jenkins airline data set
, which shows the number of international airline
passengers per month from the year 1949–1960.
import numpy as np
from sktime.datasets import load_airline
from sktime.forecasting.theta import ThetaForecaster
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.performance_metrics.forecasting import smape_loss
y = load_airline()
fig, ax = plot_ys(y)
ax.set(xlabel="Time"
, ylabel="Number of airline passengers"
,
title='Box-Jenkins International Airline passengers per month' )
y = load_airline()
y_train, y_test = temporal_train_test_split(y)
fh = np.arange(1, len(y_test) + 1) # forecasting horizon
forecaster = ThetaForecaster(sp=12) # monthly seasonal periodicity
forecaster.fit(y_train)
y_pred = forecaster.predict(fh)
plot_ys(y_train, y_test, y_pred, labels=["y_train"
,
"y_test"
,
"y_pred"]);
smape_loss(y_test, y_pred)
Note:
Sktime comes with several forecasting algorithms, all of which share a
common interface. These forecasters are trained on a single series of data and make
forecasts for the provided forecasting horizon.
Why cannot we use Scikit-learn for forecasting?
Using scikit-learn for forecasting is possible but it comes with many demerits, let us
see why can't we use scikit-learn for forecasting.
1.
How to apply the regression algorithm?
In order to perform forecasting, we need to apply
regression (also called
reduction)
. If we are using scikit-learn, first we need to transform the data into the
required
tabular format
then
fit the regressor
and finally
generate the forecasts
but this requires a lot of written code which often leads to more error-prone code,
wastes a lot of time and may also involve many implicit hyper-parameters.
2.
How to generate forecasts?
If we are using scikit-learn, we
can not make multi-step ahead forecasts as we are
making a single step ahead forecasts every time using the most recent data.
Note:
Sktime also has a number of statistical forecasting algorithms as well which
are based on implementations in stats models such as:
Exponential smoothing
ARIMA model
Note:
Sktime also provides a set of modular APIs for composite model building for
forecasting:
Ensemble
Pipeline
Reducing
Stacking
Summary
To make forecasts with sktime we need to first build a model or specify a model,
then fit the model to learn training data and then predict to generate future
forecasts for the given forecasting horizon.
Sktime also comes with a number of statistical forecasting algorithms for use
and tools for composite model buildings.
References
1. If you want to get your hands dirty, do check out the documentation of sktime.
2. Markus Löning, Anthony Bagnall, Sajaysurya Ganesh, Viktor Kazakov, Jason
Lines, Franz Király (2019): “sktime: A Unified Interface for Machine Learning
with Time Series”
3. https://github.com/alan-turing-institute/sktime
4. https://awesomeopensource.com/project/alan-turing-institute/sktime
参考这个佬使用sktime进行时间序列分类第2部分_weixin_26704853的博客-CSDN博客
找这个佬的第一部分没找到 自己去网站找了一下 分享给大家 文件是一个PDF(也可以去外网访问,需要tizi)