-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathSklearn_Wine_UC.py
390 lines (276 loc) · 10.5 KB
/
Sklearn_Wine_UC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# Databricks notebook source
# MAGIC %md ## Sklearn Wine Quality MLflow model - Unity Catalog
# MAGIC * Trains and saves model as Sklearn flavor
# MAGIC * Predicts using Sklearn, Pyfunc and UDF flavors
# MAGIC * Registers model in Unity Catalog model registry
# MAGIC
# MAGIC #### Widgets
# MAGIC * 01. Run name
# MAGIC * 02. Experiment name: if not set, use notebook experiment
# MAGIC * 03. Registered model: if set, register as model
# MAGIC * 04. Model alias
# MAGIC * 06. Input example
# MAGIC * 07. Log input - see [mlflow.data](https://mlflow.org/docs/latest/python_api/mlflow.data.html)
# MAGIC * 08. SHAP
# MAGIC * 09. Delta table: if not set, read CSV file from DBFS
# MAGIC * 10. Max depth
# MAGIC
# MAGIC #### Sample widget values
# MAGIC
# MAGIC * Model: andre_catalog.ml_models.Sklearn_Wine_best
# MAGIC * Experiment: /Users/[email protected]/experiments/best/Sklearn_Wine_repo_uc
# MAGIC * Delta tables:
# MAGIC * andre_catalog.ml_data.winequality_white
# MAGIC * andre_catalog.ml_data.winequality_red
# MAGIC
# MAGIC Last uddated: _2024-06-04_
# COMMAND ----------
# MAGIC %md ### Setup
# COMMAND ----------
import mlflow
mlflow.set_registry_uri("databricks-uc")
# COMMAND ----------
# MAGIC %run ./Common
# COMMAND ----------
dbutils.widgets.text("01. Run name", "")
dbutils.widgets.text("02. Experiment name", "")
dbutils.widgets.text("03. Registered model", "andre_catalog.ml_models2.sklearn_wine_best")
dbutils.widgets.text("04. Model alias","")
dbutils.widgets.text("05. Delta table", "")
dbutils.widgets.dropdown("06. Input example", "yes", ["yes", "no"])
dbutils.widgets.dropdown("07. Log input", "yes", ["yes", "no"])
dbutils.widgets.dropdown("08. Log evaluation metrics", "no", ["yes", "no"])
dbutils.widgets.dropdown("09. SHAP","no", ["yes", "no"])
dbutils.widgets.text("10. Max depth", "1")
run_name = dbutils.widgets.get("01. Run name")
experiment_name = dbutils.widgets.get("02. Experiment name")
model_name = dbutils.widgets.get("03. Registered model")
model_alias = dbutils.widgets.get("04. Model alias")
delta_table = dbutils.widgets.get("05. Delta table")
input_example = dbutils.widgets.get("06. Input example") == "yes"
log_input = dbutils.widgets.get("07. Log input") == "yes"
log_evaluation_metrics = dbutils.widgets.get("08. Log evaluation metrics") == "yes"
log_shap = dbutils.widgets.get("09. SHAP") == "yes"
max_depth = to_int(dbutils.widgets.get("10. Max depth"))
run_name = run_name or None
experiment_name = experiment_name or None
model_name = model_name or None
model_alias = model_alias or None
input_example = input_example or None
print("run_name: ", run_name)
print("experiment_name:", experiment_name)
print("model_name: ", model_name)
print("model_alias: ", model_alias)
print("input_example: ", input_example)
print("log_input: ", log_input)
print("log_evaluation_metrics:", log_evaluation_metrics)
print("log_shap: ", log_shap)
print("delta_table:", delta_table)
print("max_depth:", max_depth)
# COMMAND ----------
if experiment_name:
mlflow.set_experiment(experiment_name)
exp = mlflow.get_experiment_by_name(experiment_name)
print("Experiment ID:", exp.experiment_id)
client.set_experiment_tag(exp.experiment_id, "version_mlflow", mlflow.__version__)
client.set_experiment_tag(exp.experiment_id, "timestamp", now)
# COMMAND ----------
# MAGIC %md ### Prepare data
# COMMAND ----------
df, data_source = WineQuality.get_data(delta_table)
data_source
# COMMAND ----------
pdf_data = df.toPandas()
display(pdf_data.head(10))
# COMMAND ----------
X_train, X_test, y_train, y_test = WineQuality.prep_training_data(pdf_data)
# COMMAND ----------
# MAGIC %md ### Set run name
# COMMAND ----------
def set_run_name_to_current_time(run_name):
if run_name:
return run_name
else:
return f"{now} - {mlflow.__version__}"
_run_name = set_run_name_to_current_time(run_name)
run_name, _run_name
# COMMAND ----------
def set_run_name_to_run_id(run):
print("Old runName:", run.data.tags.get("mlflow.runName"))
client.set_tag(run_id, "mlflow.runName", run_id)
run = client.get_run(run_id)
print("New runName:", run.data.tags.get("mlflow.runName"))
# COMMAND ----------
# MAGIC %md ### Train
# COMMAND ----------
import numpy as np
import pandas as pd
import sklearn
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from mlflow.models.signature import infer_signature
# COMMAND ----------
import os, platform
with mlflow.start_run(run_name=_run_name) as run:
run_id = run.info.run_id
print("MLflow:")
print(" run_id:", run_id)
print(" experiment_id:", run.info.experiment_id)
print("Parameters:")
print(" max_depth:", max_depth)
mlflow.set_tag("run_name", _run_name)
mlflow.set_tag("timestamp", now)
mlflow.set_tag("version.mlflow", mlflow.__version__)
mlflow.set_tag("version.sklearn", sklearn.__version__)
mlflow.set_tag("version.DATABRICKS_RUNTIME_VERSION", os.environ.get("DATABRICKS_RUNTIME_VERSION",None))
mlflow.set_tag("version.python", platform.python_version())
mlflow.set_tag("input_example", input_example)
mlflow.set_tag("log_input", log_input)
mlflow.set_tag("data_source", data_source)
mlflow.log_param("max_depth", max_depth)
model = DecisionTreeRegressor(max_depth=max_depth)
model.fit(X_train, y_train)
mlflow.set_tag("algorithm", type(model))
predictions = model.predict(X_test)
signature = infer_signature(X_train, predictions)
print("signature:", signature)
print("input_example:", input_example)
log_data_input(run, log_input, data_source, X_train)
model_info = mlflow.sklearn.log_model(model, "model", signature=signature, input_example=X_test)
dump_obj(model_info)
rmse = np.sqrt(mean_squared_error(y_test, predictions))
r2 = r2_score(y_test, predictions)
print("Metrics:")
print(" rmse:",rmse)
print(" r2:",r2)
mlflow.log_metric("rmse", rmse)
mlflow.log_metric("r2", r2)
if log_evaluation_metrics:
model_uri = mlflow.get_artifact_uri("model")
print("model_uri:",model_uri)
test_data = pd.concat([X_test, y_test], axis=1)
result = mlflow.evaluate(
model_uri,
test_data,
targets = "quality",
model_type ="regressor",
evaluators = "default",
feature_names = list(pdf_data.columns),
evaluator_config={"explainability_nsamples": 1000},
)
if log_shap:
mlflow.shap.log_explanation(model.predict, X_train)
# COMMAND ----------
dump_obj(model_info)
# COMMAND ----------
if not run_name:
set_run_name_to_run_id(run)
# COMMAND ----------
# MAGIC %md ### Register model aka create model version
# COMMAND ----------
if model_name:
activate_unity_catalog()
version = register_model_uc(run, model_name, model_alias, description=run_name)
print(f"Registered model '{model_name}' as version {version.version}")
# COMMAND ----------
# MAGIC %md ### Display UI links
# COMMAND ----------
display_run_uri(run.info.experiment_id, run_id)
# COMMAND ----------
display_experiment_id_info(run.info.experiment_id)
# COMMAND ----------
model_name
# COMMAND ----------
if model_name:
display_registered_model_version_uri(model_name, version.version)
# COMMAND ----------
# MAGIC %md ### Show input data
# COMMAND ----------
run = client.get_run(run_id)
if hasattr(run, "inputs") and run.inputs:
for input in run.inputs:
print(input)
# COMMAND ----------
run_id, run.info.run_id
# COMMAND ----------
# MAGIC %md ### Predict with `runs:/` URI
# COMMAND ----------
model_uri = f"runs:/{run_id}/model"
model_uri
# COMMAND ----------
# MAGIC %md #### Predict as sklearn
# COMMAND ----------
model = mlflow.sklearn.load_model(model_uri)
data_to_predict = WineQuality.prep_prediction_data(pdf_data)
predictions = model.predict(data_to_predict)
display(pd.DataFrame(predictions, columns=[WineQuality.colPrediction]))
# COMMAND ----------
type(predictions)
# COMMAND ----------
# MAGIC %md #### Predict as Pyfunc
# COMMAND ----------
model = mlflow.pyfunc.load_model(model_uri)
predictions = model.predict(data_to_predict)
display(pd.DataFrame(predictions,columns=[WineQuality.colPrediction]))
# COMMAND ----------
type(predictions)
# COMMAND ----------
# MAGIC %md #### Predict as Spark UDF
# COMMAND ----------
df_to_predict = spark.createDataFrame(data_to_predict)
# COMMAND ----------
udf = mlflow.pyfunc.spark_udf(spark, model_uri)
predictions = df_to_predict.withColumn("prediction", udf(*df_to_predict.columns)).select("prediction")
display(predictions)
# COMMAND ----------
type(predictions)
# COMMAND ----------
# MAGIC %md ### Predict with `models:/` URI
# COMMAND ----------
if model_name:
model_uri = f"models:/{model_name}/{version.version}"
model_uri
else:
print("No registered model specified")
# COMMAND ----------
# MAGIC %md #### Predict as Pyfunc
# COMMAND ----------
if model_name:
model = mlflow.pyfunc.load_model(model_uri)
predictions = model.predict(data_to_predict)
display(pd.DataFrame(predictions,columns=[WineQuality.colPrediction]))
# COMMAND ----------
# MAGIC %md #### Predict as Spark UDF
# COMMAND ----------
if model_name:
##df = spark.createDataFrame(data_to_predict)
##udf = mlflow.pyfunc.spark_udf(spark, model_uri)
##predictions = df.withColumn("prediction", udf(*df.columns)).select("prediction")
## display(predictions)
udf = mlflow.pyfunc.spark_udf(spark, model_uri)
predictions = df_to_predict.withColumn("prediction", udf(*df_to_predict.columns)).select("prediction")
display(predictions)
# COMMAND ----------
# MAGIC %md ### Predict with `models:/` URI and alias
# COMMAND ----------
# MAGIC %md #### Predict with `pyfunc`
# COMMAND ----------
if model_alias:
model_uri = f"models:/{model_name}@{model_alias}"
print("model_uri:", model_uri)
model = mlflow.pyfunc.load_model(model_uri)
predictions = model.predict(data_to_predict)
display(pd.DataFrame(predictions,columns=[WineQuality.colPrediction]))
else:
print("No model alias")
# COMMAND ----------
# MAGIC %md #### Predict with `pyfunc.spark_udf`
# COMMAND ----------
if model_alias:
model_uri = f"models:/{model_name}@{model_alias}"
print("model_uri:", model_uri)
udf = mlflow.pyfunc.spark_udf(spark, model_uri)
predictions = df_to_predict.withColumn("prediction", udf(*df_to_predict.columns)).select("prediction")
display(predictions)
else:
print("No model alias")