From 89abd38970a6b3ffba345264ffb6fc785b16aad3 Mon Sep 17 00:00:00 2001 From: ckurze Date: Thu, 30 Nov 2023 16:10:48 +0100 Subject: [PATCH] ML/AutoML: Reworked connection string for easier usage --- ..._timeseries_forecasting_with_pycaret.ipynb | 65 +++++++++++++------ 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/topic/machine-learning/automl/automl_timeseries_forecasting_with_pycaret.ipynb b/topic/machine-learning/automl/automl_timeseries_forecasting_with_pycaret.ipynb index ff6d6a47..8d71cf8a 100644 --- a/topic/machine-learning/automl/automl_timeseries_forecasting_with_pycaret.ipynb +++ b/topic/machine-learning/automl/automl_timeseries_forecasting_with_pycaret.ipynb @@ -120,20 +120,48 @@ "sign up for a free account at https://console.cratedb.cloud and \n", "[deploy a cluster].\n", "\n", + "[deploy a cluster]: https://cratedb.com/docs/cloud/en/latest/tutorials/deploy/stripe.html#deploy-cluster\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "To populate your environment with corresponding database access credentials,\n", "create an `.env` file with the following content:\n", "\n", "```env\n", - "CRATE_HOST= # set this to localhost if you're running crate locally\n", - "CRATE_USER= # set this to crate if you're running crate locally\n", - "CRATE_PASSWORD= # set this to \"\" if you're running crate locally\n", - "CRATE_SSL=true # set this to false if you're running crate locally\n", + "# use this string for a connection to CrateDB Cloud\n", + "CONNECTION_STRING=crate://username:password@hostname/?ssl=true \n", + "\n", + "# use this string for a local connection to CrateDB\n", + "# CONNECTION_STRING=crate://crate@localhost/?ssl=false \n", "```\n", "\n", "You can find your CrateDB credentials in the [CrateDB Cloud Console].\n", "\n", - "[CrateDB Cloud Console]: https://cratedb.com/docs/cloud/en/latest/reference/overview.html#cluster\n", - "[deploy a cluster]: https://cratedb.com/docs/cloud/en/latest/tutorials/deploy/stripe.html#deploy-cluster\n" + "[CrateDB Cloud Console]: https://cratedb.com/docs/cloud/en/latest/reference/overview.html#cluster" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "# For CrateDB Cloud, use:\n", + "CONNECTION_STRING = os.environ.get(\n", + " \"CRATEDB_CONNECTION_STRING\",\n", + " \"crate://username:password@hostname/?ssl=true\",\n", + ")\n", + "\n", + "# For an self-deployed CrateDB, e.g. via Docker, please use:\n", + "# CONNECTION_STRING = os.environ.get(\n", + "# \"CRATEDB_CONNECTION_STRING\",\n", + "# \"crate://crate@localhost/?ssl=false\",\n", + "# )" ] }, { @@ -203,8 +231,7 @@ "data[\"date\"] = pd.to_datetime(data[\"date\"])\n", "\n", "# Insert the data into CrateDB\n", - "dburi = f\"crate://{os.environ['CRATE_USER']}:{os.environ['CRATE_PASSWORD']}@{os.environ['CRATE_HOST']}:4200?ssl={os.environ['CRATE_SSL']}\"\n", - "engine = sa.create_engine(dburi, echo=os.environ.get(\"DEBUG\"))\n", + "engine = sa.create_engine(CONNECTION_STRING, echo=os.environ.get(\"DEBUG\"))\n", "\n", "with engine.connect() as conn:\n", " data.to_sql(\n", @@ -253,7 +280,7 @@ "data[\"month\"] = pd.to_datetime(data['month'], unit='ms')\n", "\n", "# We set the MLFLOW_TRACKING_URI to our CrateDB instance. We'll see later why\n", - "os.environ[\"MLFLOW_TRACKING_URI\"] = f\"{dburi}&schema=mlflow\"" + "os.environ[\"MLFLOW_TRACKING_URI\"] = f\"{CONNECTION_STRING}&schema=mlflow\"" ] }, { @@ -1277,7 +1304,6 @@ "output_type": "display_data" }, { - "name": "stdout", "metadata": { "nbreg": { "diff_ignore": [ @@ -1285,13 +1311,13 @@ ] } }, + "name": "stdout", "output_type": "stream", "text": [ "Fitting 3 folds for each of 10 candidates, totalling 30 fits\n" ] }, { - "name": "stderr", "metadata": { "nbreg": { "diff_ignore": [ @@ -1299,6 +1325,7 @@ ] } }, + "name": "stderr", "output_type": "stream", "text": [ "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 24 concurrent workers.\n", @@ -1433,7 +1460,6 @@ "output_type": "display_data" }, { - "name": "stdout", "metadata": { "nbreg": { "diff_ignore": [ @@ -1441,13 +1467,13 @@ ] } }, + "name": "stdout", "output_type": "stream", "text": [ "Fitting 3 folds for each of 10 candidates, totalling 30 fits\n" ] }, { - "name": "stderr", "metadata": { "nbreg": { "diff_ignore": [ @@ -1455,6 +1481,7 @@ ] } }, + "name": "stderr", "output_type": "stream", "text": [ "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 24 concurrent workers.\n", @@ -1583,8 +1610,6 @@ "output_type": "display_data" }, { - "name": "stdout", - "output_type": "stream", "metadata": { "nbreg": { "diff_ignore": [ @@ -1592,12 +1617,13 @@ ] } }, + "name": "stdout", + "output_type": "stream", "text": [ "Fitting 3 folds for each of 10 candidates, totalling 30 fits\n" ] }, { - "name": "stderr", "metadata": { "nbreg": { "diff_ignore": [ @@ -1605,6 +1631,7 @@ ] } }, + "name": "stderr", "output_type": "stream", "text": [ "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 24 concurrent workers.\n", @@ -2086,7 +2113,7 @@ "source": [ "os.environ[\n", " \"MLFLOW_TRACKING_URI\"\n", - "] = f\"crate://{os.environ['CRATE_USER']}:{os.environ['CRATE_PASSWORD']}@{os.environ['CRATE_HOST']}:4200?ssl={os.environ['CRATE_SSL']}&schema=mlflow\"" + "] = f\"{CONNECTION_STRING}&schema=mlflow\"" ] }, { @@ -2182,7 +2209,7 @@ ], "metadata": { "kernelspec": { - "display_name": "crate", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -2196,7 +2223,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.0" + "version": "3.11.4" } }, "nbformat": 4,