-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Timegapsplit dataframe-agnostic #668
Merged
FBruzzesi
merged 2 commits into
koaning:narwhals-development
from
MarcoGorelli:timegapsplit-agnostic
May 12, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from itertools import combinations | ||
from warnings import warn | ||
|
||
import narwhals as nw | ||
import numpy as np | ||
import pandas as pd | ||
from sklearn.exceptions import NotFittedError | ||
|
@@ -44,8 +45,10 @@ class TimeGapSplit: | |
|
||
Parameters | ||
---------- | ||
date_serie : pd.Series | ||
date_serie : Series | ||
Series with the date, that should have all the indices of X used in the split() method. | ||
If the Series is not pandas-like (for example, if it's a Polars Series, which does not have | ||
an index) then it must the same same length as the `X` and `y` objects passed to `split`. | ||
valid_duration : datetime.timedelta | ||
Retraining period. | ||
train_duration : datetime.timedelta | None, default=None | ||
|
@@ -65,6 +68,21 @@ class TimeGapSplit: | |
|
||
- `"rolling"` window has fixed size and is shifted entirely. | ||
- `"expanding"` left side of window is fixed, right border increases each fold. | ||
|
||
Notes | ||
----- | ||
Native cross-dataframe support is achieved using | ||
[Narwhals](https://narwhals-dev.github.io/narwhals/){:target="_blank"}. | ||
Supported dataframes are: | ||
|
||
- pandas | ||
- Polars (eager) | ||
- Modin | ||
- cuDF | ||
|
||
See [Narwhals docs](https://narwhals-dev.github.io/narwhals/extending/){:target="_blank"} for an up-to-date list | ||
(and to learn how you can add your dataframe library to it!), though note that only those | ||
convertible to `numpy` arrays will work with this class. | ||
""" | ||
|
||
def __init__( | ||
|
@@ -82,11 +100,7 @@ def __init__( | |
if (train_duration is not None) and (train_duration <= gap_duration): | ||
raise ValueError("gap_duration is longer than train_duration, it should be shorter.") | ||
|
||
if not date_serie.index.is_unique: | ||
raise ValueError("date_serie doesn't have a unique index") | ||
|
||
self.date_serie = date_serie.copy() | ||
self.date_serie = self.date_serie.rename("__date__") | ||
self.date_serie = nw.from_native(date_serie, series_only=True).alias("__date__") | ||
self.train_duration = train_duration | ||
self.valid_duration = valid_duration | ||
self.gap_duration = gap_duration | ||
|
@@ -98,13 +112,15 @@ def _join_date_and_x(self, X): | |
index and with the 'numpy index' column (i.e. just a range) that is required for the output and the rest of | ||
sklearn. | ||
|
||
If the user is working with index-less dataframes (e.g. Polars), then `self.date_series` needs to be the same | ||
length as `X`. | ||
|
||
Parameters | ||
---------- | ||
X : pd.DataFrame | ||
X : DataFrame | ||
Dataframe with the data to split | ||
""" | ||
X_index_df = pd.DataFrame(range(len(X)), columns=["np_index"], index=X.index) | ||
X_index_df = X_index_df.join(self.date_serie) | ||
X_index_df = nw.maybe_align_index(self.date_serie, X).to_frame().with_row_index("np_index") | ||
|
||
return X_index_df | ||
|
||
|
@@ -113,7 +129,7 @@ def split(self, X, y=None, groups=None): | |
|
||
Parameters | ||
---------- | ||
X : pd.DataFrame | ||
X : DataFrame | ||
Dataframe with the data to split. | ||
y : array-like | None, default=None | ||
Ignored, present for compatibility. | ||
|
@@ -126,8 +142,9 @@ def split(self, X, y=None, groups=None): | |
Train and test indices of the same fold. | ||
""" | ||
|
||
X = nw.from_native(X, eager_only=True) | ||
X_index_df = self._join_date_and_x(X) | ||
X_index_df = X_index_df.sort_values("__date__", ascending=True) | ||
X_index_df = X_index_df.sort("__date__", descending=False) | ||
|
||
if len(X) != len(X_index_df): | ||
raise AssertionError( | ||
|
@@ -167,31 +184,28 @@ def split(self, X, y=None, groups=None): | |
if current_date + self.train_duration + time_shift + self.gap_duration > date_max: | ||
break | ||
|
||
X_train_df = X_index_df[ | ||
(X_index_df["__date__"] >= start_date) & (X_index_df["__date__"] < current_date + self.train_duration) | ||
] | ||
X_valid_df = X_index_df[ | ||
(X_index_df["__date__"] >= current_date + self.train_duration + self.gap_duration) | ||
& ( | ||
X_index_df["__date__"] | ||
< current_date + self.train_duration + self.valid_duration + self.gap_duration | ||
) | ||
] | ||
X_train_df = X_index_df.filter( | ||
nw.col("__date__") >= start_date, nw.col("__date__") < current_date + self.train_duration | ||
) | ||
X_valid_df = X_index_df.filter( | ||
nw.col("__date__") >= current_date + self.train_duration + self.gap_duration, | ||
nw.col("__date__") < current_date + self.train_duration + self.valid_duration + self.gap_duration, | ||
) | ||
Comment on lines
-170
to
+193
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this diff so pleasing 😍 |
||
|
||
current_date = current_date + time_shift | ||
if self.window == "rolling": | ||
start_date = current_date | ||
yield ( | ||
X_train_df["np_index"].values, | ||
X_valid_df["np_index"].values, | ||
X_train_df["np_index"].to_numpy(), | ||
X_valid_df["np_index"].to_numpy(), | ||
) | ||
|
||
def get_n_splits(self, X=None, y=None, groups=None): | ||
"""Get the number of splits | ||
|
||
Parameters | ||
---------- | ||
X : pd.DataFrame | ||
X : DataFrame | ||
Dataframe with the data to split. | ||
y : array-like | None, default=None | ||
Ignored, present for compatibility. | ||
|
@@ -210,42 +224,52 @@ def summary(self, X): | |
|
||
Parameters | ||
---------- | ||
X : pd.DataFrame | ||
X : DataFrame | ||
Dataframe with the data to split. | ||
|
||
Returns | ||
------- | ||
pd.DataFrame | ||
DataFrame | ||
Summary of all folds. | ||
""" | ||
summary = [] | ||
X = nw.from_native(X, eager_only=True) | ||
X_index_df = self._join_date_and_x(X) | ||
|
||
def get_split_info(X, indices, j, part, summary): | ||
dates = X_index_df.iloc[indices]["__date__"] | ||
summary = { | ||
"Start date": [], | ||
"End date": [], | ||
"Period": [], | ||
"Unique days": [], | ||
"nbr samples": [], | ||
"part": [], | ||
"fold": [], | ||
} | ||
native_namespace = nw.get_native_namespace(X) | ||
|
||
def update_split_info(indices, j, part, summary): | ||
dates = X_index_df["__date__"][indices] | ||
mindate = dates.min() | ||
maxdate = dates.max() | ||
n_unique = dates.n_unique() | ||
|
||
s = pd.Series( | ||
{ | ||
"Start date": mindate, | ||
"End date": maxdate, | ||
"Period": pd.to_datetime(maxdate, format="%Y%m%d") - pd.to_datetime(mindate, format="%Y%m%d"), | ||
"Unique days": len(dates.unique()), | ||
"nbr samples": len(indices), | ||
}, | ||
name=(j, part), | ||
) | ||
summary.append(s) | ||
return summary | ||
summary["Start date"].append(mindate) | ||
summary["End date"].append(maxdate) | ||
summary["Period"].append(maxdate - mindate) | ||
summary["Unique days"].append(n_unique) | ||
summary["nbr samples"].append(len(indices)) | ||
summary["part"].append(part) | ||
summary["fold"].append(j) | ||
|
||
j = 0 | ||
for i in self.split(X): | ||
summary = get_split_info(X, i[0], j, "train", summary) | ||
summary = get_split_info(X, i[1], j, "valid", summary) | ||
for i in self.split(nw.to_native(X)): | ||
update_split_info(native_namespace.Series(i[0]), j, "train", summary) | ||
update_split_info(native_namespace.Series(i[1]), j, "valid", summary) | ||
j = j + 1 | ||
|
||
return pd.DataFrame(summary) | ||
result = nw.from_native(native_namespace.DataFrame(summary)) | ||
result = nw.maybe_set_index(result, ["fold", "part"]) | ||
return nw.to_native(result) | ||
|
||
|
||
def KlusterFoldValidation(**kwargs): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is checked as part of
nw.maybe_align_index