Transformers
scikit-learn transformers for the data.
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)
CalendarTimestampFeatures
Bases: BaseEstimator
, TransformerMixin
Day of week and prop into day columns creation.
Source code in latent_calendar/transformers.py
HourDiscretizer
Bases: BaseEstimator
, TransformerMixin
Discretize the hour column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
col
|
str
|
The name of the column to discretize. |
'hour'
|
minutes
|
int
|
The number of minutes to discretize by. |
60
|
Source code in latent_calendar/transformers.py
LongToWide
Bases: BaseEstimator
, TransformerMixin
Unstack the assumed last index as vocab column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
col
|
str
|
The name of the column to unstack. |
'num_events'
|
as_int
|
bool
|
Whether to cast the values to int. |
True
|
minutes
|
int
|
The number of minutes to discretize by. |
60
|
multiindex
|
bool
|
Whether the columns are a multiindex. |
True
|
Source code in latent_calendar/transformers.py
transform(X, y=None)
Unstack the assumed last index as vocab column.
Source code in latent_calendar/transformers.py
RawToVocab
Bases: BaseEstimator
, TransformerMixin
Transformer timestamp level data into id level data with vocab columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id_col
|
str
|
The name of the id column. |
required |
timestamp_col
|
str
|
The name of the timestamp column. |
required |
minutes
|
int
|
The number of minutes to discretize by. |
60
|
additional_groups
|
list[str] | None
|
Additional columns to group by. |
None
|
cols
|
list[str] | None
|
Additional columns to sum. |
None
|
as_multiindex
|
bool
|
Whether to return columns as a multiindex. |
True
|
widen
|
bool
|
Whether to widen the data at the end. Only supported for DataFrames with index. |
True
|
Source code in latent_calendar/transformers.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
|
VocabAggregation
Bases: BaseEstimator
, TransformerMixin
NOTE: The index of the grouping stays for pandas DataFrames.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
groups
|
list[str]
|
The columns to group by. |
required |
cols
|
list[str] | None
|
Additional columns to sum. |
None
|
Source code in latent_calendar/transformers.py
VocabTransformer
Bases: BaseEstimator
, TransformerMixin
Create a vocab column from the day of week and hour columns.
Source code in latent_calendar/transformers.py
CalandarTimestampFeatures(*arg, **kwargs)
Alias for CalendarTimestampFeatures.
This is to avoid breaking changes in the API.
Source code in latent_calendar/transformers.py
create_raw_to_vocab_transformer(id_col, timestamp_col, minutes=60, additional_groups=None, as_multiindex=True, widen=True)
Wrapper to create the transformer from the configuration options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id_col
|
str
|
The name of the id column. |
required |
timestamp_col
|
str
|
The name of the timestamp column. |
required |
minutes
|
int
|
The number of minutes to discretize by. |
60
|
additional_groups
|
list[str] | None
|
Additional columns to group by. |
None
|
as_multiindex
|
bool
|
Whether to return columns as a multiindex. |
True
|
widen
|
bool
|
Whether to widen the data at the end. Only supported for DataFrames with index. |
True
|
Returns:
Type | Description |
---|---|
RawToVocab
|
A transformer that transforms timestamp level data into id level data with vocab columns. |
Source code in latent_calendar/transformers.py
create_timestamp_feature_pipeline(timestamp_col, discretize=True, minutes=60, create_vocab=True, output='pandas')
Create a pipeline that creates features from the timestamp column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timestamp_col
|
str
|
The name of the timestamp column. |
required |
discretize
|
bool
|
Whether to discretize the hour column. |
True
|
minutes
|
int
|
The number of minutes to discretize by. Ignored if discretize is False. |
60
|
create_vocab
|
bool
|
Whether to create the vocab column. |
True
|
output
|
str
|
The output type of the pipeline. Default is "pandas" |
'pandas'
|
Returns:
Type | Description |
---|---|
Pipeline
|
A pipeline that creates features from the timestamp column. |
Example
Create features for the online transactions dataset.
Source code in latent_calendar/transformers.py
prop_into_day(dt)
Returns the proportion into the day from datetime like object.
0.0 is midnight and 1.0 is midnight again.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dt
|
ExprDateTimeNamespace
|
datetime like object |
required |
Returns:
Type | Description |
---|---|
Expr
|
numeric value(s) between 0.0 and 1.0 |
Source code in latent_calendar/transformers.py
raw_to_aggregate(df, id_col, timestamp_col, minutes=60, additional_groups=None, cols=None)
Aggregate raw timestamp level data into
This function uses narwhals and will work on any supported DataFrame implementation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
IntoFrameT
|
The input data. |
required |
id_col
|
str
|
The name of the id column. |
required |
timestamp_col
|
str
|
The name of the timestamp column. |
required |
minutes
|
int
|
The number of minutes to discretize by. |
60
|
additional_groups
|
list[str] | None
|
Additional columns to group by. |
None
|
cols
|
list[str] | None
|
Additional columns to sum. |
None
|
Returns:
Type | Description |
---|---|
IntoFrameT
|
A DataFrame with aggregated data. |
Example
Aggregate DataFrame in a polars LazyFrame
import polars as pl
from latent_calendar.datasets import load_online_transactions
from latent_calendar import raw_to_aggregate
df = load_online_transactions()
df_lazy = pl.LazyFrame(df)
df_agg = raw_to_aggregate(
df=df_lazy,
id_col="Country",
timestamp_col="InvoiceDate",
)
df_agg.collect()
shape: (1_088, 4)
┌────────────────┬─────────────┬──────┬────────────┐
│ Country ┆ day_of_week ┆ hour ┆ num_events │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i8 ┆ i64 ┆ i32 │
╞════════════════╪═════════════╪══════╪════════════╡
│ Belgium ┆ 2 ┆ 15 ┆ 1 │
│ Germany ┆ 0 ┆ 8 ┆ 112 │
│ EIRE ┆ 4 ┆ 16 ┆ 18 │
│ Italy ┆ 0 ┆ 11 ┆ 1 │
│ Canada ┆ 4 ┆ 12 ┆ 1 │
│ … ┆ … ┆ … ┆ … │
│ Finland ┆ 3 ┆ 19 ┆ 17 │
│ Australia ┆ 1 ┆ 14 ┆ 8 │
│ Portugal ┆ 1 ┆ 11 ┆ 23 │
│ United Kingdom ┆ 0 ┆ 11 ┆ 17949 │
│ Iceland ┆ 2 ┆ 14 ┆ 29 │
└────────────────┴─────────────┴──────┴────────────┘
Source code in latent_calendar/transformers.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
|