Skip to content
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

Add dates_needed_for_prediction to TimeSeriesPipelineBase #3906

Merged
merged 17 commits into from
Jan 12, 2023

Conversation

jeremyliweishih
Copy link
Collaborator

@jeremyliweishih jeremyliweishih commented Jan 4, 2023

Adds dates_needed_for_prediction for time series pipelines. Erring on the side of caution and asking for a longer date period and will optimize further if necessary.

@jeremyliweishih jeremyliweishih force-pushed the js_dates_needed_for_prediction branch from e45c38a to b58d431 Compare January 4, 2023 15:30
@codecov
Copy link

codecov bot commented Jan 4, 2023

Codecov Report

Merging #3906 (074eb2a) into main (505d4cc) will increase coverage by 0.1%.
The diff coverage is 100.0%.

@@           Coverage Diff           @@
##            main   #3906     +/-   ##
=======================================
+ Coverage   99.7%   99.7%   +0.1%     
=======================================
  Files        346     346             
  Lines      36669   36707     +38     
=======================================
+ Hits       36536   36574     +38     
  Misses       133     133             
Impacted Files Coverage Δ
evalml/pipelines/time_series_pipeline_base.py 100.0% <100.0%> (ø)
.../tests/pipeline_tests/test_time_series_pipeline.py 99.9% <100.0%> (+0.1%) ⬆️
evalml/utils/gen_utils.py 99.3% <100.0%> (+0.1%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

self.forecast_horizon
+ self.max_delay # include start delay for featurization
+ self.gap # add first gap for the actual gap from the end date
+ self.gap, # add another gap to ensure training data is greater than gap
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We have a limitation s.t we require the training data (using the dates from this method) to be larger than the gap. Thus, I'm directly adding the gap here so that we ensure our training data is always long enough. This can be optimized further by the dates needed to push the length of the training data over the gap but decided to err on the side of caution for the first pass. I believe its safer to ask the user for more data unless we know that it is hard for the user to provide us such data.

Copy link
Contributor

Choose a reason for hiding this comment

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

A frequency of 'MS' (month start) here would be interpreted as milliseconds, right? https://numpy.org/doc/stable/reference/arrays.datetime.html#datetime-units

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think Frank is right here in that MS would be milliseconds. From my understanding, I think numpy defaults to milliseconds while pandas maps to month start for this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah gotcha - I'll change to using a datetime timedelta!

ts_data,
time_series_regression_pipeline_class,
):
X, X_t, y = ts_data()
Copy link
Contributor

Choose a reason for hiding this comment

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

Based on Frank's comment above, it'd probably be a good idea to test a few different frequencies here!

self.forecast_horizon
+ self.max_delay # include start delay for featurization
+ self.gap # add first gap for the actual gap from the end date
+ self.gap, # add another gap to ensure training data is greater than gap
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think Frank is right here in that MS would be milliseconds. From my understanding, I think numpy defaults to milliseconds while pandas maps to month start for this.

evalml/pipelines/time_series_pipeline_base.py Outdated Show resolved Hide resolved
+ self.max_delay # include start delay for featurization
+ self.gap # add first gap for the actual gap from the end date
+ self.gap
+ 1 # for the + 1 in the time series featurizer
Copy link
Contributor

Choose a reason for hiding this comment

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

:-/ Is it safe to assume that all pipelines will have the time series featurizer?

evalml/pipelines/time_series_pipeline_base.py Outdated Show resolved Hide resolved
Comment on lines +1779 to +1805
dates = pd.date_range(
beginning_date,
end_date,
freq=pipeline.frequency.split("-")[0],
)
X_train = pd.DataFrame(index=[i + 1 for i in range(len(dates))])
feature = pd.Series([i + 1 for i in range(len(dates))], index=X_train.index)

X_train["feature"] = pd.Series(feature.values, index=X_train.index)
X_train["date"] = pd.Series(dates.values, index=X_train.index)
y_train = pd.Series(X_train["feature"].values, index=X_train.index)

X_test = pd.DataFrame({"feature": [54], "date": [prediction_date]})

assert X_train.shape[0] == len(dates)
assert not X_train.isnull().any().any()
assert len(y_train) == X_train.shape[0]
assert X_test.shape[0] == 1

X_train.ww.init()
X_test.ww.init()

X_train.ww.set_time_index("date")
X_test.ww.set_time_index("date")

_ = pipeline.predict(X_test, X_train=X_train, y_train=y_train).all()
assert not mock_predict.call_args[0][0].empty
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain why we need all this? I would think we might want to keep the test a little more minimalist rather than do a predict. Like don't we just want to test the dates_needed_for_prediction function itself?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

During implementation I created date ranges that were mathematically correct but didn't work entirely with our featurization so added this last part to ensure!

Comment on lines +1772 to +1777
assert beginning_date <= end_date
assert end_date < prediction_date
date_diff = pipeline.forecast_horizon + pipeline.max_delay + pipeline.gap
assert end_date == beginning_date + pd.tseries.frequencies.to_offset(
f"{date_diff}{pipeline.frequency}",
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to reparametrize this so that the pytest parameters contain the answers to the incoming inputs and just test them against each other?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it'll get pretty messy including the answers with the different combinations of parameters and frequencies above. I could do so but I would need to narrow down the scope of combinations.

@jeremyliweishih jeremyliweishih enabled auto-merge (squash) January 12, 2023 17:02
@jeremyliweishih jeremyliweishih merged commit 372c070 into main Jan 12, 2023
@jeremyliweishih jeremyliweishih deleted the js_dates_needed_for_prediction branch January 12, 2023 17:16
@chukarsten chukarsten mentioned this pull request Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants