Skip to content

Datetime encoder - #239

Merged
GaelVaroquaux merged 52 commits into
skrub-data:masterfrom
LeoGrin:datetime_encoder
Jul 1, 2022
Merged

GaelVaroquaux merged 52 commits into
skrub-data:masterfrom
LeoGrin:datetime_encoder

Conversation

@LeoGrin

@LeoGrin LeoGrin commented Feb 19, 2022

Copy link
Copy Markdown
Contributor

Creates a new encoder which transform datetime columns into several numerical features (year, month, day...). Solves the second part of #233.

@LilianBoulard LilianBoulard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's bunch of small adjustments, but overall this is very good ! Thank you :)

Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread CHANGES.rst Outdated
Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/datetime_encoder.py
Comment thread dirty_cat/datetime_encoder.py
Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/datetime_encoder.py Outdated
LeoGrin and others added 2 commits March 2, 2022 17:56
Applying @Lilian suggested changes

Co-authored-by: Lilian <lilian@boulard.fr>
@LeoGrin

LeoGrin commented Mar 2, 2022

Copy link
Copy Markdown
Contributor Author

Thank you for the review @LilianBoulard !

@GaelVaroquaux GaelVaroquaux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

I left a few comments, but none of them are major.

The new object should be added to the list of encoders on the first page (doc/index.rst).

We also need an example demonstrating the new object.

Comment thread CHANGES.rst Outdated
Comment thread dirty_cat/datetime_encoder.py
Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/datetime_encoder.py Outdated
"millisecond", "microsecond", "nanosecond"}, default="hour"
Extract up to this granularity, and gather the rest into the "other" feature.
For instance, if you specify "day", only "year", "month", "day" and "other" features will be created.
The "other" feature will be a numerical value expressed in the "extract_until" unit.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My gut feeling is that the "other" feature would make the learning easier if it were the full time to epoch. Else, the model may need to learning the weird algebra: 24H, 30 days a months but not quite, 365 days in a year and sometimes not.
Probably, having both features would be useful....

@LeoGrin LeoGrin Mar 8, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm interesting. I get you point, but here's why I chose to do it this way:

  • I fear that full_time_to_epoch would be very collinear with the highest-level feature, hurting interpretability. For a worst case scenario, we can imagine a dataset of different hours during the same day for different years.
  • It’s true that with my choice, the learner has to learn the weird algebra. But with your proposition, the learner would still need to learn this weird algebra if it wants to use my other feature (which may be more important for prediction since it’s information which isn’t contained in the other variables), because other = full_time_to_epoch - (year - 1970) * 365 * 24 * 3600 + …

Maybe using both could indeed make the learning easier, but wouldn't you worry about collinearity?
All in all I'm not sure.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do machine learning. I don't worry about collinearity :).

It does not hurt for prediction. I can hurt for interpretability.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reason for favoring the full_time_to_epoch is that I think that, in general, it is more likely to be useful than the other end, and we are more likely to have to reconstruct it.

Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/datetime_encoder.py Outdated
Comment thread dirty_cat/test/test_datetime_encoder.py Outdated
Comment thread dirty_cat/test/test_datetime_encoder.py Outdated
@GaelVaroquaux

GaelVaroquaux commented Mar 8, 2022 via email

Copy link
Copy Markdown
Member

@LeoGrin

LeoGrin commented Jun 28, 2022

Copy link
Copy Markdown
Contributor Author

Additions:

  • Replaced the "other" feature by the "full" features, which contains the full time to epoch (see above discussion with @GaelVaroquaux)
  • Made the DatetimeEncoder the default for datetime columns in the SuperVectorizer
  • Added a simple example
  • Small fixes

@LeoGrin

LeoGrin commented Jun 28, 2022

Copy link
Copy Markdown
Contributor Author

Apparently using fetch_traffic_violations() in the example takes too much memory for circleCI

@LeoGrin

LeoGrin commented Jun 28, 2022

Copy link
Copy Markdown
Contributor Author

I've changed the example to use another dataset, I think the PR is ready for review @LilianBoulard @GaelVaroquaux @jovan-stojanovic

@GaelVaroquaux GaelVaroquaux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few changes requested. Most are minor. I hope that the prediction example won't ask for too much work.

Comment thread examples/06_datetime_encoder.py Outdated
Comment thread examples/06_datetime_encoder.py Outdated
breaks down each datetime features into several numerical features, by extracting relevant information from the
datetime features, such as the month, the day of the week, the hour of the day, etc. Used in
the SuperVectorizer, which automatically detects the datetime features, the DatetimeEncoder allows
to handle datetime features easily.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to do a small prediction model at the end, add a section, to showcase this in a prediction pipeline?

For didactic purposes it would be important to have not only the cross-validation (using https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.TimeSeriesSplit.html ) but also the plot of the extrapolated time series (compared to the real one)

Comment thread dirty_cat/datetime_encoder.py Outdated
@LeoGrin

LeoGrin commented Jun 29, 2022

Copy link
Copy Markdown
Contributor Author

Thank you for the comments! I've added a new section to the example

@GaelVaroquaux

Copy link
Copy Markdown
Member

Oups, I just saw that there is a "examples/.DS_Store" that needs to be removed. You can add it in the gitignore if it's helpful.

@GaelVaroquaux GaelVaroquaux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still cosmetic comments on the example :)

Comment thread examples/06_datetime_encoder.py Outdated
Comment thread examples/06_datetime_encoder.py Outdated
Comment thread examples/06_datetime_encoder.py Outdated
Comment thread examples/06_datetime_encoder.py Outdated

@GaelVaroquaux GaelVaroquaux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks a lot!

@GaelVaroquaux
GaelVaroquaux merged commit c71498e into skrub-data:master Jul 1, 2022
@GaelVaroquaux

Copy link
Copy Markdown
Member

Hum, I merged this, but I hadn't seen that the lines are too long (I'll fix it, I wanted to do cosmetics on the example anyhow).

It does have a practical consequence: it forces horizontal scrolling in the example, which is bad for readability.

@GaelVaroquaux

GaelVaroquaux commented Oct 11, 2022 via email

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants