To create and use tsfresh for extracting features from OHLC (Open, High, Low, Close) data, you need to follow a series of steps that primarily include preprocessing the data, extracting relevant features, and using those features for further analysis or modeling. Here's a structured guide to help you with this:
1. Install Required Packages
First, ensure you have the necessary packages installed:
pip install tsfresh pandas numpy
2. Import Libraries and Load Data
Import the required libraries and load your OHLC data. Here is an example with a sample DataFrame:
import pandas as pd
from tsfresh import extract_features
# Sample OHLC data
data = {
'timestamp': ['2023-01-01 00:00', '2023-01-01 01:00', '2023-01-01 02:00', '2023-01-01 03:00'],
'open': [100, 102, 103, 104],
'high': [105, 106, 107, 108],
'low': [99, 100, 101, 102],
'close': [104, 105, 106, 107]
}
df = pd.DataFrame(data)
df['timestamp'] = pd.to_datetime(df['timestamp'])
3. Preprocess Your Data
Ensure that your OHLC data is in the correct format. tsfresh requires a specific format where id and kind are used to distinguish different time series and types of observations.
# Creating an index to act as the `id` for each segment (typically each asset, OHLC type, etc.)
df['id'] = 1 # Here, we're assuming only one time series
df = df.set_index('timestamp')
# We need to melt the DataFrame to create the `kind` column, which will label the type of OHLC data
df_melted = df.melt(id_vars=['id'], var_name='kind', value_name='value', col_level=0)
print(df_melted.head())
4. Extract Features Using tsfresh
Now that the data is in the correct format, we can use tsfresh to extract various features:
# The timestamp index should be reset to a column named 'time'
df_melted = df_melted.reset_index()
# Extract features
extracted_features = extract_features(
df_melted,
column_id='id',
column_sort='timestamp',
column_kind='kind',
column_value='value'
)
print(extracted_features.head())
5. Use Extracted Features for Analysis
The resulting DataFrame extracted_features contains a wide range of time-series features that you can use for further analysis, including predictive modeling, anomaly detection, and other tasks.
Full Example Code
Here's the full example stitched together:
import pandas as pd
import numpy as np
from tsfresh import extract_features
# Sample OHLC data
data = {
'timestamp': ['2023-01-01 00:00', '2023-01-01 01:00', '2023-01-01 02:00', '2023-01-01 03:00'],
'open': [100, 102, 103, 104],
'high': [105, 106, 107, 108],
'low': [99, 100, 101, 102],
'close': [104, 105, 106, 107]
}
df = pd.DataFrame(data)
df['timestamp'] = pd.to_datetime(df['timestamp'])
# Creating an index to act as the `id` for each segment (typically each asset, OHLC type, etc.)
df['id'] = 1 # Here, we're assuming only one time series
df = df.set_index('timestamp')
# We need to melt the DataFrame to create the `kind` column, which will label the type of OHLC data
df_melted = df.melt(id_vars=['id'], var_name='kind', value_name='value', col_level=0)
# The timestamp index should be reset to a column named 'time'
df_melted = df_melted.reset_index()
# Extract features
extracted_features = extract_features(
df_melted,
column_id='id',
column_sort='timestamp',
column_kind='kind',
column_value='value'
)
print(extracted_features.head())
Conclusion
This example should give you a good starting point for using tsfresh with OHLC data. It covers the essential steps needed to preprocess your data, format it appropriately, and apply the tsfresh library to extract meaningful features for further analysis.