From 9c84a8fc1b9dc5769181ce0e9e9f44976884cd41 Mon Sep 17 00:00:00 2001 From: Stefano Savare Date: Thu, 9 Jul 2020 16:50:04 +0200 Subject: [PATCH] Updated requirements Signed-off-by: Stefano Savare --- examples/example.py | 61 ++++++++++++++++++++ gtime/explainability/tests/test_explainer.py | 5 +- gtime/feature_extraction/tests/test_trend.py | 2 +- gtime/hierarchical/tests/test_top_down.py | 2 +- gtime/regressors/tests/test_explainable.py | 3 +- requirements.txt | 2 + 6 files changed, 71 insertions(+), 4 deletions(-) create mode 100755 examples/example.py diff --git a/examples/example.py b/examples/example.py new file mode 100755 index 0000000..e2f10fc --- /dev/null +++ b/examples/example.py @@ -0,0 +1,61 @@ +import pandas as pd +from sklearn.linear_model import LinearRegression + +from gtime.feature_extraction import Shift +from gtime.time_series_models.base import TimeSeriesForecastingModel + + +class TimeSeries(pd.DataFrame): + def plot(self): + pass + + +time_series = TimeSeries() + +# You can plot +time_series.plot() + +# Decomposition +## Un peu bizarre le plot_stl() et deux fois stl_decomposition +time_series = time_series.stl_decomposition() +time_series.plot_stl() +time_series = time_series.recompose() # Choose a good name + +# Box-Cox +time_series = time_series.box_cox(lambda_=0.3) + +# Feature forecasting +features = [("shift", Shift(1), "time_series")] +automatic_features = get_features() # Similar to fast.ai get_transforms() +gar_forecaster = LinearRegression() +# This object TimeSeriesForecastingModel keeps into account all the intermediate steps. +# You don't need to manually deal with train/test split, etc.. +forecasting_model = TimeSeriesForecastingModel( + features=features, horizon=3, model=gar_forecaster +) +forecasting_model = forecasting_model.fit(time_series) +forecasting_model.predict() +forecasting_model.cross_validate() # Is cross validation also on multiple time series? + +# Residuals analysis +forecasting_model.residuals_.acf() + +# Questions +""" +How to implement ARIMA? I think that a GAR forecaster with MA should work, but we should check. +It helps that the user can't customize the feature matrix. + +Exponential Smoothing? Maybe it could work also? Not clear if it is possible with additional +features + +Add a learner object? +""" + +time_series = TimeSeries(pandas_dataframe) + +arima = ARIMA(time_series) +arima.fit(time_series,, +preds = arima.predict(time_series) + + +time_series.to_pandas() diff --git a/gtime/explainability/tests/test_explainer.py b/gtime/explainability/tests/test_explainer.py index e96e6f4..7f09fe6 100644 --- a/gtime/explainability/tests/test_explainer.py +++ b/gtime/explainability/tests/test_explainer.py @@ -1,7 +1,8 @@ import hypothesis.strategies as st import numpy as np +import pandas as pd import pytest -from hypothesis import given +from hypothesis import given, settings from hypothesis.strategies import floats, sampled_from, data, lists, text from lime.explanation import Explanation from shap.explainers.explainer import Explainer @@ -130,6 +131,7 @@ def _check_predict_output( ] ) + @settings(deadline=pd.Timedelta(milliseconds=5000), max_examples=7) @pytest.mark.parametrize( "explainer", lazy_fixtures([lime_explainer, shap_explainer]) ) @@ -153,6 +155,7 @@ def test_error_predict_not_fit(self, explainer, X): class TestLime: + @settings(deadline=pd.Timedelta(milliseconds=10000), max_examples=7) @given(regressor=models(), X_y=numpy_X_y_matrices(min_value=-100, max_value=100)) def test_predict(self, lime_explainer, regressor, X_y): X, y = X_y diff --git a/gtime/feature_extraction/tests/test_trend.py b/gtime/feature_extraction/tests/test_trend.py index 871d8d8..9241edf 100644 --- a/gtime/feature_extraction/tests/test_trend.py +++ b/gtime/feature_extraction/tests/test_trend.py @@ -37,7 +37,7 @@ def test_polynomial_detrend(): columns=[f"0__{feature_name}"], index=time_index, ) - pd.testing.assert_frame_equal(ts_t, expected_ts) + pd.testing.assert_frame_equal(ts_t, expected_ts, check_less_precise=3) def test_exponential_detrend(): diff --git a/gtime/hierarchical/tests/test_top_down.py b/gtime/hierarchical/tests/test_top_down.py index b822477..22aeae3 100644 --- a/gtime/hierarchical/tests/test_top_down.py +++ b/gtime/hierarchical/tests/test_top_down.py @@ -255,7 +255,7 @@ def test_error_predict_not_fitted(self, hierarchical_basic_top_down_model): with pytest.raises(sklearn.exceptions.NotFittedError): hierarchical_basic_top_down_model.predict() - @given(dataframes=n_time_series_with_same_index()) + @given(dataframes=n_time_series_with_same_index(min_n=5)) def test_error_with_bad_predict_key( self, dataframes, hierarchical_basic_top_down_model ): diff --git a/gtime/regressors/tests/test_explainable.py b/gtime/regressors/tests/test_explainable.py index 5141204..d8aeda4 100644 --- a/gtime/regressors/tests/test_explainable.py +++ b/gtime/regressors/tests/test_explainable.py @@ -1,7 +1,7 @@ from typing import List import pytest -from hypothesis import given +from hypothesis import given, settings from sklearn import clone from sklearn.base import BaseEstimator from sklearn.cluster import DBSCAN, KMeans, SpectralClustering @@ -85,6 +85,7 @@ def test_fit_values(self, estimator, explainer_type, X_y): estimator_fit_attributes, cloned_estimator_fit_attributes ) + @settings(deadline=pd.Timedelta(milliseconds=5000), max_examples=7) @pytest.mark.parametrize("explainer_type", ["lime", "shap"]) @given( estimator=regressors(), X_y=numpy_X_y_matrices(min_value=-100, max_value=100) diff --git a/requirements.txt b/requirements.txt index d82e9ac..4a014e7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ scikit-learn>=0.22.0 matplotlib>=3.1.0 lime>=0.2.0.0 shap>=0.35 +holidays>=0.10.2 +lunarcalendar>=0.0.9