-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformers.py
284 lines (204 loc) · 7.93 KB
/
transformers.py
1
2
3
4
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""scikit-learn transformers for the data.
```python
from latent_calendar.datasets import load_online_transactions
df = load_online_transactions()
transformers = create_raw_to_vocab_transformer(id_col="Customer ID", timestamp_col="InvoiceDate")
df_wide = transformers.fit_transform(df)
```
"""
from typing import List, Optional, Union
from datetime import datetime
import pandas as pd
from pandas.core.indexes.accessors import DatetimeProperties
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
from latent_calendar.const import (
FULL_VOCAB,
HOURS_IN_DAY,
MINUTES_IN_DAY,
SECONDS_IN_DAY,
MICROSECONDS_IN_DAY,
)
def prop_into_day(dt: Union[datetime, DatetimeProperties]) -> Union[float, pd.Series]:
"""Returns the proportion into the day from datetime like object.
0.0 is midnight and 1.0 is midnight again.
Args:
dt: datetime like object
Returns:
numeric value(s) between 0.0 and 1.0
"""
prop_hour = dt.hour / HOURS_IN_DAY
prop_minute = dt.minute / MINUTES_IN_DAY
prop_second = dt.second / SECONDS_IN_DAY
prop_microsecond = dt.microsecond / MICROSECONDS_IN_DAY
return prop_hour + prop_minute + prop_second + prop_microsecond
class CalandarTimestampFeatures(BaseEstimator, TransformerMixin):
"""Day of week and prop into day columns creation."""
def __init__(
self,
timestamp_col: str,
) -> None:
self.timestamp_col = timestamp_col
def fit(self, X: pd.DataFrame, y=None):
return self
def transform(self, X: pd.DataFrame, y=None) -> pd.DataFrame:
"""Create 2 new columns."""
if not hasattr(X[self.timestamp_col], "dt"):
raise RuntimeError(
f"Column {self.timestamp_col!r} is not a datetime column. Use df[{self.timestamp_col!r}] = pd.to_datetime(df[{self.timestamp_col!r}]) first."
)
X = X.copy()
X["prop_into_day_start"] = prop_into_day(X[self.timestamp_col].dt)
X["day_of_week"] = X[self.timestamp_col].dt.dayofweek
X["hour"] = X["prop_into_day_start"] * 24
tmp_columns = ["prop_into_day_start"]
self.created_columns = ["day_of_week", "hour"]
X = X.drop(columns=tmp_columns)
self.columns = list(X.columns)
return X
def get_feature_names_out(self, input_features=None):
return self.columns.extend(self.created_columns)
class HourDiscretizer(BaseEstimator, TransformerMixin):
"""Discretize the hour column."""
def __init__(self, col: str = "hour") -> None:
self.col = col
def fit(self, X: pd.DataFrame, y=None):
return self
def transform(self, X: pd.DataFrame, y=None):
X[self.col] = (X[self.col] // 1).astype(int)
self.columns = list(X.columns)
return X
def get_feature_names_out(self, input_features=None):
return self.columns
class VocabTransformer(BaseEstimator, TransformerMixin):
"""Create a vocab column from the day of week and hour columns."""
def __init__(
self, day_of_week_col: str = "day_of_week", hour_col: str = "hour"
) -> None:
self.day_of_week_col = day_of_week_col
self.hour_col = hour_col
def fit(self, X: pd.DataFrame, y=None):
return self
def transform(self, X: pd.DataFrame, y=None) -> pd.DataFrame:
X["vocab"] = (
X[self.day_of_week_col]
.astype(str)
.str.zfill(2)
.str.cat(X[self.hour_col].astype(str).str.zfill(2), sep=" ")
)
self.columns = list(X.columns)
return X
def get_feature_names_out(self, input_features=None):
return self.columns
def create_timestamp_feature_pipeline(
timestamp_col: str,
) -> Pipeline:
"""Create a pipeline that creates features from the timestamp column.
Args:
timestamp_col: The name of the timestamp column.
Returns:
A pipeline that creates features from the timestamp column.
Example:
Create features for the online transactions dataset.
```python
from latent_calendar.datasets import load_online_transactions
df = load_online_transactions()
transformers = create_timestamp_feature_pipeline(timestamp_col="InvoiceDate")
df_features = transformers.fit_transform(df)
```
"""
vocab_col = "hour"
return Pipeline(
[
(
"timestamp_features",
CalandarTimestampFeatures(timestamp_col=timestamp_col),
),
("binning", HourDiscretizer(col=vocab_col)),
("vocab_creation", VocabTransformer(hour_col=vocab_col)),
]
).set_output(transform="pandas")
class VocabAggregation(BaseEstimator, TransformerMixin):
"""NOTE: The index of the grouping stays."""
def __init__(self, groups: List[str], cols: Optional[List[str]] = None) -> None:
self.groups = groups
self.cols = cols
def fit(self, X: pd.DataFrame, y=None):
return self
def transform(self, X: pd.DataFrame, y=None):
stats = {}
if self.cols is not None:
stats.update({col: (col, "sum") for col in self.cols})
df_agg = (
X.assign(num_events=1)
.groupby(self.groups)
.agg(num_events=("num_events", "sum"), **stats)
)
self.columns = list(df_agg.columns)
return df_agg
def get_feature_names_out(self, input_features=None):
return self.columns
class LongToWide(BaseEstimator, TransformerMixin):
def __init__(self, col: str = "num_events", as_int: bool = True) -> None:
self.col = col
self.as_int = as_int
def fit(self, X: pd.DataFrame, y=None):
return self
def transform(self, X: pd.DataFrame, y=None) -> pd.DataFrame:
"""Unstack the assumed last index as vocab column."""
X_T = X.loc[:, self.col].unstack().T
X_T.index = X_T.index.get_level_values(-1)
X_T = X_T.reindex(FULL_VOCAB)
X_res = X_T.T.fillna(value=0)
if self.as_int:
X_res = X_res.astype(int)
return X_res
def get_feature_names_out(self, input_features=None):
return FULL_VOCAB
class RawToVocab(BaseEstimator, TransformerMixin):
"""Transformer timestamp level data into id level data with vocab columns."""
def __init__(
self,
id_col: str,
timestamp_col: str,
additional_groups: Optional[List[str]] = None,
cols: Optional[List[str]] = None,
) -> None:
self.id_col = id_col
self.timestamp_col = timestamp_col
self.additional_groups = additional_groups
self.cols = cols
def fit(self, X: pd.DataFrame, y=None):
# New features at same index level
self.features = create_timestamp_feature_pipeline(
self.timestamp_col,
)
groups = [self.id_col]
if self.additional_groups is not None:
if not isinstance(self.additional_groups, list):
raise ValueError(
f"additional_groups should be list not {type(self.additional_groups)}"
)
groups.extend(self.additional_groups)
groups.append("vocab")
# Reaggregation
self.aggregation = VocabAggregation(groups=groups, cols=self.cols)
# Unstacking
self.widden = LongToWide(col="num_events")
# Since nothing needs to be "fit"
return self
def transform(self, X: pd.DataFrame, y=None) -> pd.DataFrame:
X_trans = self.features.transform(X)
X_agg = self.aggregation.transform(X_trans)
return self.widden.transform(X_agg)
def create_raw_to_vocab_transformer(
id_col: str,
timestamp_col: str,
additional_groups: Optional[List[str]] = None,
) -> RawToVocab:
"""Wrapper to create the transformer from the configuration options."""
return RawToVocab(
id_col=id_col,
timestamp_col=timestamp_col,
additional_groups=additional_groups,
)