Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to realize data sliding window Operation in time Series Prediction

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use python to achieve data sliding window operation in time series prediction", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use python to achieve data sliding window operation in time series prediction" this article.

Writing background

Rookies for data analysis, the level is limited, mistakes are inevitable, welcome to correct.

What is a data sliding window?

Generally speaking, machine learning involves the steps of dividing the training set and the test set. In particular, when making data prediction, the basis of prediction (that is, historical data) is generally called X, and the data to be predicted is called y. Firstly, the original data is divided into two training data sets, train_X and train_y, and two test data sets, test_X and test_y.

For the prediction of time series data, it is often to establish the future data to predict the next moment by several historical data. at this time, in order to make full use of all the data, we should slide the original data set, as shown in the following figure.

What is shown here is a time series with multiple features, in which each row of data belongs to the same time. Suppose that we want to take the three characteristics of H (humidity), PT (pressure) and PE (power) as the prediction basis, and take the known data of the current and the last three moments to predict the PE (power) of the next time, then the sliding window for the X data set should be shown above, and the sliding window for the y data set should be shown as the following figure.

An example of a sliding window is given below.

Code implementation

Sliding window function

Def sliding_window (DataSet, X_width, y_width, gap = 1, multi_vector = None, X_data = True):''DataSet has to be as a DataFrame' if X_data: if multi_vector: a B = DataSet.shape else: a = DataSet.shape [0] b = 1 c = (a-X_width-y_width-a%gap) / gap X = np.reshape B) for i in range (len (DataSet)-X_width-y_width): I + = 1 if I > c: break j = I * gap tmp = DataSet.iloc 0) return X else: if multi_vector: print ('y_data-error:expect 1D, given% dD'%DataSet.shape [1]) return Else: a = DataSet.shape [0] c = (a-X_width-y_width-a%gap) / gap y = np.reshape (DataSet.iloc0] .values, (1 Y_width)) for i in range (len (DataSet)-X_width-y_width): I + = 1 if I > c: break j = I * gap + X_width tmp = DataSet.iloc [JRV j + YJI widthWidth:]. Values tmp = np.reshape (tmp, (1memywidth)) y = np.concatenate ([y] Tmp]) return y single characteristic time series

Single characteristic time series refers to one-dimensional time series with only one characteristic, such as stock closing price, wind speed data of wind farm, daily turnover and so on. The sliding window operation for single feature time series is as follows:

# DataSet training dataset # Historical data length used by X_width # data length to be predicted by y_width whether X_data is an X dataset train_X = sliding_window (DataSet, X_width, y_width) train_y = sliding_window (DataSet, X_width, y_width, X_data = None)

Assuming that the training data set is a sequence of 100 to 1, and 24 data are used to predict one data in the future, then the sliding window operation transforms the original data like this:

Multi-feature time series

Multi-feature time series refers to more than one feature of time series, such as H, PT and PE series mentioned above. This kind of data is generally used in situations where the data to be predicted are highly correlated with multiple features, such as wind speed prediction embedded in meteorological data, closing price prediction embedded in stock market data, and so on. The sliding window operation of multi-feature time series is as follows:

# DataSet training dataset # Historical data length used by X_width # data length to be predicted by y_width whether multi_vector is multi-feature # X_data is X dataset train_X = sliding_window (DataSet, X_width, y_width, multi_vector = True) test_y = sliding_window (DataSet, X_width, y_width, multi_vector = True, X_data = None)

Assuming that the training data set is a sequence of 100 to 3, and 24 data are used to predict one data in the future, then the sliding window operation transforms the original data like this:

Matters needing attention

DataSet must be in DataFrame format.

The dataset can only be one-dimensional.

The above is all the contents of the article "how to use python to achieve data sliding window operation in time series prediction". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report