-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathPredict_Sklearn_Wine.py
90 lines (56 loc) · 2.07 KB
/
Predict_Sklearn_Wine.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
# Databricks notebook source
# MAGIC %md ## Sklearn model predict only
# MAGIC
# MAGIC ##### Overview
# MAGIC * Predicts using Sklearn, Pyfunc and Spark UDF flavors
# MAGIC * See notebook [Sklearn_Wine]($Sklearn_Wine)
# MAGIC
# MAGIC ##### Widgets
# MAGIC * `Model URI` - `models:/Sklearn_Wine/5` or `runs:/2620b314f33449078a6cb1a770a82de2/model`
# MAGIC * `Table` - if not specified, will load data from default sklearn location
# COMMAND ----------
# MAGIC %md ### Setup
# COMMAND ----------
# MAGIC %run ./Common
# COMMAND ----------
dbutils.widgets.text("1. Model URI", "models:/andre_catalog.ml_models2.sklearn_wine_best@champ")
model_uri = dbutils.widgets.get("1. Model URI")
dbutils.widgets.text("2. Table", "andre_catalog.ml_data.winequality_white")
table_name = dbutils.widgets.get("2. Table")
print("model_uri:", model_uri)
print("table_name:", table_name)
set_model_registry(model_uri)
assert_widget(model_uri, "Model URI")
# COMMAND ----------
# MAGIC %md ### Prepare data
# COMMAND ----------
if table_name:
data = spark.table(table_name).toPandas()
else:
data = WineQuality.load_pandas_data()
data_to_predict = WineQuality.prep_prediction_data(data)
display(data_to_predict)
# COMMAND ----------
# MAGIC %md ### Predict as Sklearn flavor
# COMMAND ----------
import mlflow
model = mlflow.sklearn.load_model(model_uri)
predictions = model.predict(data_to_predict)
type(predictions), predictions.shape
# COMMAND ----------
display(pd.DataFrame(predictions, columns=[WineQuality.colPrediction]))
# COMMAND ----------
# MAGIC %md ### Predict as Pyfunc flavor
# COMMAND ----------
model = mlflow.pyfunc.load_model(model_uri)
predictions = model.predict(data_to_predict)
type(predictions), predictions.shape
# COMMAND ----------
display(pd.DataFrame(predictions, columns=[WineQuality.colPrediction]))
# COMMAND ----------
# MAGIC %md ### Predict as Spark UDF
# COMMAND ----------
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)