feature_engineering
predspot.feature_engineering
¶
Feature Engineering Module¶
Turns the spatio-temporal series produced by a mapping (see
crime_mapping) into lagged features, one row per
(t, places). Each feature class applies a time series transformation to
the history of every place and then builds lags lagged columns from it:
AR— the raw series (autoregressive features);Diff— first differences;Seasonality— the seasonal component of an STL decomposition;Trend— the trend component of an STL decomposition.
The output always contains one extra row for the period right after the last observed one, so that the fitted model can forecast the next period.
TimeSeriesFeatures
¶
Bases: BaseEstimator, TransformerMixin
Base class for lagged time series features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lags
|
int
|
Number of lagged columns to create (> 1). |
required |
tfreq
|
str
|
Time frequency of the series ( |
None
|
Source code in src/predspot/feature_engineering.py
apply_ts_decomposition
abstractmethod
¶
Transform the time series of one place before lagging it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ts
|
Series
|
Series of one place indexed by time. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Transformed series. |
Source code in src/predspot/feature_engineering.py
fit
¶
make_lag_df
¶
Build the lagged feature columns of one time series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ts
|
Series
|
Series indexed by time. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in src/predspot/feature_engineering.py
transform
¶
Compute lagged features for every place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stseries
|
Series
|
Series indexed by |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Features indexed by |
Source code in src/predspot/feature_engineering.py
AR
¶
Bases: TimeSeriesFeatures
Autoregressive features: lags of the raw series.
Source code in src/predspot/feature_engineering.py
Diff
¶
Bases: TimeSeriesFeatures
Lags of the first difference of the series.
Source code in src/predspot/feature_engineering.py
Seasonality
¶
Bases: _STLFeatures
Lags of the seasonal component of an STL decomposition (period = lags).
Source code in src/predspot/feature_engineering.py
Trend
¶
Bases: _STLFeatures
Lags of the trend component of an STL decomposition (period = lags).
Source code in src/predspot/feature_engineering.py
FeatureScaling
¶
Bases: TransformerMixin, BaseEstimator
Wrap a scikit-learn scaler so that it returns DataFrames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
estimator
|
TransformerMixin
|
Any scikit-learn transformer (e.g. |
required |
Source code in src/predspot/feature_engineering.py
infer_offset
¶
Infer the pandas.DateOffset between consecutive periods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_index
|
DatetimeIndex
|
Unique, sorted period labels. |
required |
Returns:
| Type | Description |
|---|---|
DateOffset
|
The offset separating consecutive periods. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the frequency cannot be inferred (e.g. fewer than 3
periods); pass |