Using tsfresh to Convert Forecasting Problems to Regression Problems
tsfresh (Time Series Feature Extraction based on Scalable Hypothesis tests) offers a powerful approach to time series forecasting by transforming what would traditionally be complex forecasting problems into standard regression problems. This transformation makes it possible to leverage traditional machine learning algorithms for time series prediction.
The Conceptual Framework
The fundamental idea behind using tsfresh for forecasting is to:
- Extract meaningful features from historical time series data
- Use these features to train a standard regression model
- Apply the model to predict future values
This approach differs from traditional time series forecasting methods (like ARIMA or exponential smoothing) by focusing on feature extraction rather than modeling the time series directly.
The Rolling Mechanism
The key concept in tsfresh for forecasting is "rolling," which transforms a single time series into multiple overlapping windows. Each window ends at a different time point, with features extracted from the window used to predict the value at the next time step.
Rolling is implemented in tsfresh as follows:
from tsfresh.utilities.dataframe_functions import roll_time_series
df_rolled = roll_time_series(df, column_id="id", column_sort="time")
This function creates a new dataset where each row represents a window of the original time series, identified by a new composite ID[1].
Complete Workflow for Forecasting
Here's a step-by-step process to convert a forecasting problem to a regression problem using tsfresh:
-
Prepare your time series data in a format suitable for tsfresh (typically a pandas DataFrame with id, time, and value columns)
-
Apply rolling to create windows:
from tsfresh.utilities.dataframe_functions import roll_time_series
df_rolled = roll_time_series(df, column_id="id", column_sort="time")
-
Extract features from each window:
from tsfresh import extract_features
df_features = extract_features(df_rolled, column_id="id", column_sort="time")
-
Create target variables (the values you want to predict)
-
Train a regression model using the extracted features as inputs and the target values as outputs:
# Using a scikit-learn regressor
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(df_features, y_target)
-
Make predictions by extracting features from new data and applying the trained model[1].
Example: Stock Price Forecasting
To illustrate, consider a stock price forecasting example:
# Import required libraries
import pandas as pd
from tsfresh.utilities.dataframe_functions import roll_time_series
from tsfresh import extract_features
from sklearn.ensemble import RandomForestRegressor
# Load stock price data
# df contains columns: id (stock symbol), time (date), and value (price)
# Roll the time series using a window of 100 days
df_rolled = roll_time_series(df, column_id="id", column_sort="time")
# Extract features from the rolled data
features = extract_features(df_rolled, column_id="id", column_sort="time",
default_fc_parameters="efficient")
# Prepare target variable (next day's price)
# This would need to be aligned properly with the features
# Train a regression model
model = RandomForestRegressor()
model.fit(features, y_target)
# Make predictions for new data
This approach was demonstrated in a talk by Nils Braun where he predicted whether stock prices would increase or decrease the next day[6].
Using with Scikit-learn Pipeline
You can integrate tsfresh into a scikit-learn pipeline for more streamlined processing:
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestRegressor
from sktime.transformations.panel.tsfresh import TSFreshFeatureExtractor
# Create a pipeline
forecaster = make_pipeline(
TSFreshFeatureExtractor(show_warnings=False, disable_progressbar=True),
RandomForestRegressor()
)
# Train the pipeline
forecaster.fit(X_train, y_train)
# Make predictions
y_pred = forecaster.predict(X_test)
This approach is particularly useful when you want to use cross-validation or hyperparameter tuning[3].
Feature Selection
tsfresh can also select the most relevant features for your forecasting task:
from tsfresh import extract_relevant_features
features = extract_relevant_features(timeseries, y,
column_id="id",
column_sort="time")
This reduces dimensionality and can improve model performance by focusing on predictive features[7].
Advantages of this Approach
- Flexibility: Works with any regression algorithm
- Feature richness: Automatically extracts hundreds of potentially useful features
- Interpretability: The features can provide insights into what drives your time series
- Scalability: Can handle large datasets through parallelization
- Simplicity: Converts complex forecasting into familiar regression problems[7]
By following this approach, you can leverage the power of tsfresh to transform time series forecasting challenges into standard regression problems, making them accessible to a wide range of machine learning techniques.