API Reference
This page documents the public API of safefeat.
Example
1) Basic window features (count, sum, mean)
import pandas as pd
from safefeat import build_features, WindowAgg
spine = pd.DataFrame({
"entity_id": ["u1"],
"cutoff_time": ["2024-01-10"],
})
events = pd.DataFrame({
"entity_id": ["u1", "u1", "u1", "u1"],
"event_time": ["2024-01-05", "2024-01-06", "2023-01-01", "2024-01-20"],
"amount": [10.0, 20.0, 999.0, 999.0],
})
X = build_features(
spine=spine,
tables={"events": events},
spec=[
WindowAgg(
table="events",
windows=["7D", "30D"],
metrics={
"*": ["count"],
"amount": ["sum", "mean"],
},
)
],
event_time_cols={"events": "event_time"},
)
print(X)
Expected output :
| entity_id | cutoff_time | events__n_events__7d | events__amount__sum__7d | events__amount__mean__7d | events__n_events__30d | events__amount__sum__30d | events__amount__mean__30d |
| --------- | ----------- | -------------------- | ----------------------- | ------------------------ | --------------------- | ------------------------ | ------------------------- |
| u1 | 2024-01-10 | 2 | 30.0 | 15.0 | 2 | 30.0 | 15.0 |
build_features
safefeat.core.build_features
build_features(
spine,
tables,
spec,
*,
entity_col="entity_id",
cutoff_col="cutoff_time",
event_time_cols=None,
allowed_lag="0s",
return_report=False,
)
Build leakage-safe features from event tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spine
|
DataFrame
|
DataFrame containing entity identifiers and cutoff times. |
required |
tables
|
dict[str, DataFrame]
|
Mapping of table name to event DataFrame. |
required |
spec
|
FeatureSpec or list[WindowAgg]
|
Feature specification describing windows and aggregations. |
required |
entity_col
|
str
|
Name of entity identifier column. |
"entity_id"
|
cutoff_col
|
str
|
Name of cutoff timestamp column. |
"cutoff_time"
|
event_time_cols
|
dict[str, str]
|
Mapping of table name to event timestamp column. |
None
|
allowed_lag
|
str
|
Allowed tolerance for future timestamps (pandas timedelta string). |
"0s"
|
return_report
|
bool
|
If True, return a tuple |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame or (DataFrame, AuditReport)
|
Feature matrix aligned to the spine. If |
Source code in src/safefeat/core.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
Feature Specification
WindowAgg
safefeat.spec.WindowAgg
dataclass
Specification for aggregating events within a time window.
Attributes:
| Name | Type | Description |
|---|---|---|
table |
str
|
Name of the events table to read (key in the |
windows |
List[str]
|
List of window lengths expressed in pandas timedelta strings (e.g.
|
metrics |
Dict[str, List[str]]
|
Mapping from a column name to a list of aggregations to compute. Use
|
Source code in src/safefeat/spec.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |