Skip to content

Commit 718a737

Browse files
ckurzeamotl
authored andcommitted
ML/AutoML: Harmonize notebooks, and easier connection string handling
1 parent 5804890 commit 718a737

File tree

2 files changed

+69
-28
lines changed

2 files changed

+69
-28
lines changed

topic/machine-learning/automl/automl_classification_with_pycaret.ipynb

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,27 @@
115115
"source": [
116116
"## Getting started\n",
117117
"\n",
118-
"First, install the required dependencies. \n",
119-
"\n",
120-
"```bash\n",
121-
"pip install -r requirements.txt\n",
122-
"```\n",
118+
"First, install the required dependencies. "
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 1,
124+
"metadata": {},
125+
"outputs": [],
126+
"source": [
127+
"#!pip install -r requirements.txt\n",
123128
"\n",
129+
"# In an environment like Google Colab, please use the absolute URL to the requirements.txt file.\n",
130+
"# Note: Some inconsistencies of dependencies might get reported. They can usually be ignored.\n",
131+
"# Restart the runtime, if asked by Colab.\n",
132+
"#!pip install -r https://raw.githubusercontent.com/crate/cratedb-examples/main/topic/machine-learning/automl/requirements.txt"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
124139
"**Note:** As of time of this writing, PyCaret requires Python 3.8, 3.9 or 3.10.\n",
125140
"\n",
126141
"Second, you will need a CrateDB instance to store and serve the data. The easiest\n",
@@ -131,31 +146,53 @@
131146
"create an `.env` file with the following content:\n",
132147
"\n",
133148
"```env\n",
134-
"CRATE_HOST=<your-crate-host> # set this to localhost if you're running crate locally\n",
135-
"CRATE_USER=<your-crate-user> # set this to crate if you're running crate locally\n",
136-
"CRATE_PASSWORD=<your-crate-password> # set this to \"\" if you're running crate locally\n",
137-
"CRATE_SSL=true # set this to false if you're running crate locally\n",
149+
"# use this string for a connection to CrateDB Cloud\n",
150+
"CONNECTION_STRING=crate://username:password@hostname/?ssl=true \n",
151+
"\n",
152+
"# use this string for a local connection to CrateDB\n",
153+
"# CONNECTION_STRING=crate://crate@localhost/?ssl=false\n",
138154
"```\n",
139155
"\n",
140156
"You can find your CrateDB credentials in the [CrateDB Cloud Console].\n",
141157
"\n",
142158
"[CrateDB Cloud Console]: https://cratedb.com/docs/cloud/en/latest/reference/overview.html#cluster\n",
143-
"[deploy a cluster]: https://cratedb.com/docs/cloud/en/latest/tutorials/deploy/stripe.html#deploy-cluster\n",
144-
"\n",
145-
"### Creating demo data\n",
159+
"[deploy a cluster]: https://cratedb.com/docs/cloud/en/latest/tutorials/deploy/stripe.html#deploy-cluster"
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": 2,
165+
"metadata": {},
166+
"outputs": [],
167+
"source": [
168+
"import os\n",
146169
"\n",
147-
"For convenience, this notebook comes with an accompanying CSV dataset which you\n",
148-
"can quickly import into the database. Upload the CSV file to your CrateDB cloud\n",
149-
"cluster, as described [here](https://cratedb.com/docs/cloud/en/latest/reference/overview.html#import).\n",
150-
"To follow this notebook, choose `pycaret_churn` for your table name.\n",
170+
"# For CrateDB Cloud, use:\n",
171+
"CONNECTION_STRING = os.environ.get(\n",
172+
" \"CRATEDB_CONNECTION_STRING\",\n",
173+
" \"crate://username:password@hostname/?ssl=true\",\n",
174+
")\n",
151175
"\n",
152-
"This will automatically create a new database table and import the data."
176+
"# For an self-deployed CrateDB, e.g. via Docker, please use:\n",
177+
"# CONNECTION_STRING = os.environ.get(\n",
178+
"# \"CRATEDB_CONNECTION_STRING\",\n",
179+
"# \"crate://crate@localhost/?ssl=false\",\n",
180+
"# )"
153181
]
154182
},
155183
{
156184
"cell_type": "markdown",
157185
"metadata": {},
158186
"source": [
187+
"### Creating demo data\n",
188+
"\n",
189+
"For convenience, this notebook comes with an accompanying CSV dataset which you\n",
190+
"can quickly import into the database. Upload the CSV file to your CrateDB cloud\n",
191+
"cluster, as described [here](https://cratedb.com/docs/cloud/en/latest/reference/overview.html#import).\n",
192+
"To follow this notebook, choose `pycaret_churn` for your table name.\n",
193+
"\n",
194+
"This will automatically create a new database table and import the data.\n",
195+
"\n",
159196
"### Alternative data import using code\n",
160197
"\n",
161198
"If you prefer to use code to import your data, please execute the following lines which read the CSV\n",
@@ -175,8 +212,7 @@
175212
"if os.path.exists(\".env\"):\n",
176213
" dotenv.load_dotenv(\".env\", override=True)\n",
177214
"\n",
178-
"dburi = f\"crate://{os.environ['CRATE_USER']}:{os.environ['CRATE_PASSWORD']}@{os.environ['CRATE_HOST']}:4200?ssl={os.environ['CRATE_SSL']}\"\n",
179-
"engine = sa.create_engine(dburi, echo=os.environ.get('DEBUG'))\n",
215+
"engine = sa.create_engine(CONNECTION_STRING, echo=os.environ.get('DEBUG'))\n",
180216
"df = pd.read_csv(\"https://github.com/crate/cratedb-datasets/raw/main/machine-learning/automl/churn-dataset.csv\")\n",
181217
"\n",
182218
"with engine.connect() as conn:\n",
@@ -214,8 +250,7 @@
214250
"if os.path.exists(\".env\"):\n",
215251
" dotenv.load_dotenv(\".env\", override=True)\n",
216252
"\n",
217-
"dburi = f\"crate://{os.environ['CRATE_USER']}:{os.environ['CRATE_PASSWORD']}@{os.environ['CRATE_HOST']}:4200?ssl={os.environ['CRATE_SSL']}\"\n",
218-
"engine = sa.create_engine(dburi, echo=os.environ.get('DEBUG'))\n",
253+
"engine = sa.create_engine(CONNECTION_STRING, echo=os.environ.get('DEBUG'))\n",
219254
"\n",
220255
"with engine.connect() as conn:\n",
221256
" with conn.execute(sa.text(\"SELECT * FROM pycaret_churn\")) as cursor:\n",
@@ -224,7 +259,7 @@
224259
"# We set the MLFLOW_TRACKING_URI to our CrateDB instance. We'll see later why\n",
225260
"os.environ[\n",
226261
" \"MLFLOW_TRACKING_URI\"\n",
227-
"] = f\"{dburi}&schema=mlflow\""
262+
"] = f\"{CONNECTION_STRING}&schema=mlflow\""
228263
]
229264
},
230265
{
@@ -966,8 +1001,10 @@
9661001
"# - \"n_select\" defines how many models are selected.\n",
9671002
"# - \"exclude\" defines which models are excluded from the comparison.\n",
9681003
"\n",
1004+
"# Note: This is only relevant if we are executing automated tests\n",
9691005
"if \"PYTEST_CURRENT_TEST\" in os.environ:\n",
9701006
" best_models = compare_models(sort=\"AUC\", include=[\"lr\", \"knn\"], n_select=3)\n",
1007+
"# If we are not in an automated test, compare the available models\n",
9711008
"else:\n",
9721009
" # For production scenarios, it might be worth to include \"lightgbm\" again.\n",
9731010
" best_models = compare_models(sort=\"AUC\", exclude=[\"lightgbm\"], n_select=3)"
@@ -3406,7 +3443,7 @@
34063443
"source": [
34073444
"os.environ[\n",
34083445
" \"MLFLOW_TRACKING_URI\"\n",
3409-
"] = f\"crate://{os.environ['CRATE_USER']}:{os.environ['CRATE_PASSWORD']}@{os.environ['CRATE_HOST']}:4200?ssl={os.environ['CRATE_SSL']}&schema=mlflow\""
3446+
"] = f\"{CONNECTION_STRING}&schema=mlflow\""
34103447
]
34113448
},
34123449
{
@@ -3484,7 +3521,7 @@
34843521
],
34853522
"metadata": {
34863523
"kernelspec": {
3487-
"display_name": "crate",
3524+
"display_name": "Python 3 (ipykernel)",
34883525
"language": "python",
34893526
"name": "python3"
34903527
},
@@ -3498,7 +3535,7 @@
34983535
"name": "python",
34993536
"nbconvert_exporter": "python",
35003537
"pygments_lexer": "ipython3",
3501-
"version": "3.10.0"
3538+
"version": "3.11.4"
35023539
}
35033540
},
35043541
"nbformat": 4,

topic/machine-learning/automl/automl_timeseries_forecasting_with_pycaret.ipynb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@
112112
},
113113
{
114114
"cell_type": "code",
115-
"execution_count": null,
115+
"execution_count": 1,
116116
"metadata": {},
117117
"outputs": [],
118118
"source": [
119119
"#!pip install -r requirements.txt\n",
120120
"\n",
121-
"# In an environment like Google Colab, please use the abolute path of requirements.txt:\n",
121+
"# In an environment like Google Colab, please use the absolute URL to the requirements.txt file.\n",
122+
"# Note: Some inconsistencies of dependencies might get reported. They can usually be ignored.\n",
123+
"# Restart the runtime, if asked by Colab.\n",
122124
"#!pip install -r https://raw.githubusercontent.com/crate/cratedb-examples/main/topic/machine-learning/automl/requirements.txt"
123125
]
124126
},
@@ -1080,10 +1082,12 @@
10801082
"# all available models are included by default)\n",
10811083
"# - \"fold\" defines the number of folds to use for cross-validation.\n",
10821084
"\n",
1085+
"# Note: This is only relevant if we are executing automated tests\n",
10831086
"if \"PYTEST_CURRENT_TEST\" in os.environ:\n",
10841087
" best_models = compare_models(sort=\"MASE\",\n",
10851088
" include=[\"ets\", \"et_cds_dt\", \"naive\"],\n",
10861089
" n_select=3)\n",
1090+
"# If we are not in an automated test, compare all available models\n",
10871091
"else:\n",
10881092
" best_models = compare_models(sort=\"MASE\", n_select=3)"
10891093
]
@@ -1831,7 +1835,7 @@
18311835
"The missing step is to identify the best model from all the conducted experiments.\n",
18321836
"This is done by simply looking the the Mean MASE outputs of all the model training\n",
18331837
"and tuning steps above. Looking at all the outputs, the best performing model is\n",
1834-
"the blended model with a MASE of 0.7783."
1838+
"the blended model with a MASE of approx. 0.77."
18351839
]
18361840
},
18371841
{

0 commit comments

Comments
 (0)