-
Notifications
You must be signed in to change notification settings - Fork 9
Add AutoML timeseries example using PyCaret #139
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b6c1362
add basic timeseries pycaret example with molls project setup
andnig c11ef28
unify pycaret examples to automl directory
andnig 1487af4
remove evaluate_model as this function works mainly in jupyter
andnig 8795424
path fixes
andnig 77c64a3
add pytest_current_test env var to py script
andnig a4aa9ce
fix: use 3 models also during test run
andnig bf592be
using different models for test runs
andnig de00cf9
Fix testing of `automl_timeseries_forecasting_with_pycaret.ipynb`
amotl 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 hidden or 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 hidden or 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 hidden or 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,3 +3,6 @@ | |
| __pycache__ | ||
| .coverage | ||
| coverage.xml | ||
| mlruns/ | ||
| archive/ | ||
| logs.log | ||
File renamed without changes.
This file contains hidden or 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 hidden or 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 hidden or 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
2,204 changes: 2,204 additions & 0 deletions
2,204
topic/machine-learning/automl/automl_timeseries_forecasting_with_pycaret.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
topic/machine-learning/automl/automl_timeseries_forecasting_with_pycaret.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import pandas as pd | ||
| import sqlalchemy as sa | ||
| import os | ||
| import mlflow_cratedb # Required to enable the CrateDB MLflow adapter. | ||
| from dotenv import load_dotenv | ||
| from mlflow.sklearn import log_model | ||
|
|
||
| from pycaret.time_series import ( | ||
| setup, | ||
| compare_models, | ||
| tune_model, | ||
| blend_models, | ||
| finalize_model, | ||
| save_model, | ||
| ) | ||
|
|
||
| if os.path.isfile(".env"): | ||
| load_dotenv(".env", override=True) | ||
|
|
||
| # Configure database connection string. | ||
| dburi = f"crate://{os.environ['CRATE_USER']}:{os.environ['CRATE_PASSWORD']}@{os.environ['CRATE_HOST']}:4200?ssl={os.environ['CRATE_SSL']}" | ||
| engine = sa.create_engine(dburi, echo=os.environ.get("DEBUG")) | ||
| os.environ["MLFLOW_TRACKING_URI"] = f"{dburi}&schema=mlflow" | ||
|
|
||
|
|
||
| def prepare_data(): | ||
| target_data = pd.read_csv( | ||
| "https://data.4tu.nl/file/539debdb-a325-412d-b024-593f70cba15b/a801f5d4-5dfe-412a-ace2-a64f93ad0010" | ||
| ) | ||
| related_data = pd.read_csv( | ||
| "https://data.4tu.nl/file/539debdb-a325-412d-b024-593f70cba15b/f2bd27bd-deeb-4933-bed7-29325ee05c2e", | ||
| header=None, | ||
| ) | ||
| related_data.columns = ["item", "org", "date", "unit_price"] | ||
| data = target_data.merge(related_data, on=["item", "org", "date"]) | ||
| data["total_sales"] = data["unit_price"] * data["quantity"] | ||
| data["date"] = pd.to_datetime(data["date"]) | ||
|
|
||
| # Insert the data into CrateDB | ||
| engine = sa.create_engine(dburi, echo=os.environ.get("DEBUG")) | ||
|
|
||
| with engine.connect() as conn: | ||
| data.to_sql( | ||
| "sales_data_for_forecast", | ||
| conn, | ||
| index=False, | ||
| chunksize=1000, | ||
| if_exists="replace", | ||
| ) | ||
|
|
||
| # Refresh table to make sure the data is available for querying - as CrateDB | ||
| # is eventually consistent | ||
| conn.execute(sa.text("REFRESH TABLE sales_data_for_forecast;")) | ||
|
|
||
|
|
||
| def fetch_data(): | ||
| query = """ | ||
| SELECT | ||
| DATE_TRUNC('month', DATE) AS MONTH, | ||
| SUM(total_sales) AS total_sales | ||
| from sales_data_for_forecast | ||
| group by month | ||
| order by month | ||
| """ | ||
|
|
||
| with engine.connect() as conn: | ||
| with conn.execute(sa.text(query)) as cursor: | ||
| data = pd.DataFrame(cursor.fetchall(), columns=cursor.keys()) | ||
|
|
||
| data["month"] = pd.to_datetime(data["month"], unit="ms") | ||
| return data | ||
|
|
||
|
|
||
| def run_experiment(data): | ||
| setup(data = data, fh=15, target="total_sales", index="month", log_experiment=True) | ||
| if "PYTEST_CURRENT_TEST" in os.environ: | ||
| best_models = compare_models(sort="MASE", | ||
| include=["arima", "ets", "exp_smooth"], | ||
| n_select=3) | ||
| else: | ||
| best_models = compare_models(sort="MASE", n_select=3) | ||
|
|
||
| tuned_models = [tune_model(model) for model in best_models] | ||
| blend = blend_models(estimator_list=tuned_models) | ||
| best_model = blend | ||
| final_model = finalize_model(best_model) | ||
| os.makedirs("model", exist_ok=True) | ||
|
|
||
| save_model(final_model, "model/timeseriesforecast_model") | ||
|
|
||
| log_model( | ||
| sk_model=final_model, | ||
| artifact_path="model/timeseriesforecast_model", | ||
| registered_model_name="timeseriesforecast_model", | ||
| ) | ||
|
|
||
|
|
||
| def main(): | ||
| """ | ||
| Provision dataset, and run experiment. | ||
| """ | ||
| prepare_data() | ||
| df = fetch_data() | ||
| run_experiment(df) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
File renamed without changes.
This file contains hidden or 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
File renamed without changes.
This file contains hidden or 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
File renamed without changes.
Binary file removed
BIN
-16.5 KB
...ml_classification_with_pycaret_files/automl_classification_with_pycaret_5_0.png
Binary file not shown.
Binary file removed
BIN
-188 KB
...ml_classification_with_pycaret_files/automl_classification_with_pycaret_7_0.png
Binary file not shown.
Binary file removed
BIN
-18.6 KB
...ml_classification_with_pycaret_files/automl_classification_with_pycaret_9_0.png
Binary file not shown.
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.
Is it correct that this line has been removed within the other notebook?
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.
Yes, this is ipywidget which looks and behaves extremely bad in non notebook environments.
I hope you don't mind, that I forward-fixed this thing in an actually unrelated file.