使用sktime进行时间序列分类(接力)

时间序列相信大家都有接触了,废话不说,最近在研究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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sktime提供了许多用于时间序列预测的模型和工具,例如ARIMA、SARIMA、ETS等。绘制时间序列预测图形的方法通常包括将实际数据和预测数据一起绘制,并使用不同的颜色或线型区分它们。 下面是一个简单的示例,演示如何使用sktime和Matplotlib绘制时间序列预测图形: ```python import matplotlib.pyplot as plt from sktime.forecasting.arima import AutoARIMA from sktime.datasets import load_airline # 加载航空公司乘客数据集 y = load_airline() # 拆分数据集 y_train, y_test = y[:120], y[120:] # 训练自动ARIMA模型 forecaster = AutoARIMA(sp=12, suppress_warnings=True) forecaster.fit(y_train) # 预测未来12个月的数据 y_pred = forecaster.predict(fh=[i for i in range(1, 13)]) # 绘制实际数据和预测数据的线图 plt.plot(y_train, label='train') plt.plot(y_test, label='test') plt.plot(y_pred, label='forecast') plt.title('Airline Passengers') plt.xlabel('Time') plt.ylabel('Passengers') plt.legend() plt.show() ``` 在这个示例中,我们首先使用sktime加载了航空公司乘客数据集,并将其拆分为训练集和测试集。然后,我们使用自动ARIMA模型来训练数据,并使用该模型预测未来12个月的数据。最后,我们将实际数据、测试数据和预测数据一起绘制在同一张图上,并使用不同的颜色区分它们。 您可以根据需要自定义这些图形,例如添加网格线、更改线条颜色或线型等。Matplotlib提供了许多选项,可以让您创建漂亮的时间序列预测图形。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值