-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature-store-with-uc-basic-example.py
356 lines (244 loc) · 13.3 KB
/
feature-store-with-uc-basic-example.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
# Databricks notebook source
# MAGIC %md # Basic example for Feature Engineering in Unity Catalog
# MAGIC This notebook illustrates how you can use Databricks Feature Engineering in Unity Catalog to create, store, and manage Unity Catalog Features to train ML models and make batch predictions, including with features whose value is only available at the time of prediction. In this example, the goal is to predict the wine quality using a ML model with a variety of static wine features and a realtime input.
# MAGIC
# MAGIC This notebook shows how to:
# MAGIC - Create a feature table and use it to build a training dataset for a machine learning model.
# MAGIC - Modify the feature table and use the updated table to create a new version of the model.
# MAGIC - Use the Databricks Features UI to determine how features relate to models.
# MAGIC - Perform batch scoring using automatic feature lookup.
# MAGIC
# MAGIC
# COMMAND ----------
# MAGIC %pip install databricks-feature-engineering
# COMMAND ----------
# MAGIC %run ./funcs
# COMMAND ----------
dbutils.library.restartPython()
# COMMAND ----------
import pandas as pd
from pyspark.sql.functions import monotonically_increasing_id, expr, rand
import uuid
from databricks.feature_engineering import FeatureEngineeringClient
from databricks.feature_store import feature_table, FeatureLookup
import mlflow
import mlflow.sklearn
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
# COMMAND ----------
# MAGIC %md ## Load dataset
# MAGIC The code in the following cell loads the dataset and does some minor data preparation: creates a unique ID for each observation and removes spaces from the column names. The unique ID column (`wine_id`) is the primary key of the feature table and is used to lookup features.
# COMMAND ----------
raw_data = spark.read.load("/databricks-datasets/wine-quality/winequality-red.csv",format="csv",sep=";",inferSchema="true",header="true" )
# Run functions
renamed_df = renameColumns(raw_data)
df = addIdColumn(renamed_df, 'wine_id')
# Drop target column ('quality') as it is not included in the feature table
features_df = df.drop('quality')
display(features_df)
# COMMAND ----------
# MAGIC %md ## Create a new catalog or reuse an existing catalog
# MAGIC To create a new catalog, you must have the `CREATE CATALOG` privilege on the metastore.
# MAGIC To use an existing catalog, you must have the `USE CATALOG` privilege on the catalog.
# COMMAND ----------
# Create a new catalog with:
# spark.sql("CREATE CATALOG IF NOT EXISTS ml")
# spark.sql("USE CATALOG ml")
# Or reuse existing catalog:
spark.sql("USE CATALOG ml")
# COMMAND ----------
# MAGIC %md ## Create a new schema in the catalog
# MAGIC To create a new schema in the catalog, you must have the `CREATE SCHEMA` privilege on the catalog.
# COMMAND ----------
spark.sql("CREATE SCHEMA IF NOT EXISTS wine_db")
spark.sql("USE SCHEMA wine_db")
# Create a unique table name for each run. This prevents errors if you run the notebook multiple times.
table_name = f"ml.wine_db.wine_db_" + str(uuid.uuid4())[:6]
print(table_name)
# COMMAND ----------
# MAGIC %md ## Create the feature table
# COMMAND ----------
# MAGIC %md The first step is to create a FeatureEngineeringClient.
# COMMAND ----------
fe = FeatureEngineeringClient()
# You can get help in the notebook for feature engineering client API functions:
# help(fe.<function_name>)
# For example:
# help(fe.create_table)
# COMMAND ----------
# MAGIC %md Create the feature table. For a complete API reference, see ([AWS](https://docs.databricks.com/machine-learning/feature-store/python-api.html)|[Azure](https://learn.microsoft.com/en-us/azure/databricks/machine-learning/feature-store/python-api)|[GCP](https://docs.gcp.databricks.com/machine-learning/feature-store/python-api.html)).
# COMMAND ----------
fe.create_table(
name=table_name,
primary_keys=["wine_id"],
df=features_df,
schema=features_df.schema,
description="wine features"
)
# COMMAND ----------
# MAGIC %md You can also use `create_table` without providing a dataframe, and then later populate the feature table using `fe.write_table`.
# MAGIC
# MAGIC Example:
# MAGIC
# MAGIC ```
# MAGIC fe.create_table(
# MAGIC name=table_name,
# MAGIC primary_keys=["wine_id"],
# MAGIC schema=features_df.schema,
# MAGIC description="wine features"
# MAGIC )
# MAGIC
# MAGIC fe.write_table(
# MAGIC name=table_name,
# MAGIC df=features_df,
# MAGIC mode="merge"
# MAGIC )
# MAGIC ```
# COMMAND ----------
# MAGIC %md ## Train a model with Feature Engineering in Unity Catalog
# COMMAND ----------
# MAGIC %md The feature table does not include the prediction target. However, the training dataset needs the prediction target values. There may also be features that are not available until the time the model is used for inference.
# MAGIC
# MAGIC This example uses the feature **`real_time_measurement`** to represent a characteristic of the wine that can only be observed at inference time. This feature is used in training and the feature value for a wine is provided at inference time.
# COMMAND ----------
## inference_data_df includes wine_id (primary key), quality (prediction target), and a real time feature
inference_data_df = df.select("wine_id", "quality", (10 * rand()).alias("real_time_measurement"))
display(inference_data_df)
# COMMAND ----------
# MAGIC %md Use a `FeatureLookup` to build a training dataset that uses the specified `lookup_key` to lookup features from the feature table and the online feature `real_time_measurement`. If you do not specify the `feature_names` parameter, all features except the primary key are returned.
# COMMAND ----------
def load_data(table_name, lookup_key):
# In the FeatureLookup, if you do not provide the `feature_names` parameter, all features except primary keys are returned
model_feature_lookups = [FeatureLookup(table_name=table_name, lookup_key=lookup_key)]
# fe.create_training_set looks up features in model_feature_lookups that match the primary key from inference_data_df
training_set = fe.create_training_set(df=inference_data_df, feature_lookups=model_feature_lookups, label="quality", exclude_columns="wine_id")
training_pd = training_set.load_df().toPandas()
# Create train and test datasets
X = training_pd.drop("quality", axis=1)
y = training_pd["quality"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
return X_train, X_test, y_train, y_test, training_set
# Create the train and test datasets
X_train, X_test, y_train, y_test, training_set = load_data(table_name, "wine_id")
X_train.head()
# COMMAND ----------
from mlflow.tracking.client import MlflowClient
client = MlflowClient()
try:
client.delete_registered_model("wine_model") # Delete the model if already created
except:
None
# COMMAND ----------
# MAGIC %md
# MAGIC The code in the next cell trains a scikit-learn RandomForestRegressor model and logs the model with the Feature Engineering in UC.
# MAGIC
# MAGIC The code starts an MLflow experiment to track training parameters and results. Note that model autologging is disabled (`mlflow.sklearn.autolog(log_models=False)`); this is because the model is logged using `fe.log_model`.
# COMMAND ----------
# Disable MLflow autologging and instead log the model using Feature Engineering in UC
mlflow.sklearn.autolog(log_models=False)
def train_model(X_train, X_test, y_train, y_test, training_set, fe):
## fit and log model
with mlflow.start_run() as run:
rf = RandomForestRegressor(max_depth=3, n_estimators=20, random_state=42)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
mlflow.log_metric("test_mse", mean_squared_error(y_test, y_pred))
mlflow.log_metric("test_r2_score", r2_score(y_test, y_pred))
fe.log_model(
model=rf,
artifact_path="wine_quality_prediction",
flavor=mlflow.sklearn,
training_set=training_set,
registered_model_name="wine_model",
)
train_model(X_train, X_test, y_train, y_test, training_set, fe)
# COMMAND ----------
# MAGIC %md
# MAGIC
# MAGIC To view the logged model, navigate to the MLflow Experiments page for this notebook. To access the Experiments page, click the Experiments icon on the left navigation bar: <img src="https://docs.databricks.com/_static/images/icons/experiments-icon.png"/>
# MAGIC
# MAGIC Find the notebook experiment in the list. It has the same name as the notebook, in this case, "Basic example for Feature Engineering in Unity Catalog".
# MAGIC
# MAGIC Click the experiment name to display the experiment page. The packaged Feature Engineering in UC model, created when you called `fe.log_model` appears in the **Artifacts** section of this page. You can use this model for batch scoring.
# MAGIC
# MAGIC <img src="https://docs.databricks.com/_static/images/machine-learning/feature-store/basic-fs-nb-artifact.png"/>
# MAGIC
# MAGIC The model is also automatically registered in the Model Registry.
# COMMAND ----------
# MAGIC %md ## Batch scoring
# MAGIC Use `score_batch` to apply a packaged Feature Engineering in UC model to new data for inference. The input data only needs the primary key column `wine_id` and the realtime feature `real_time_measurement`. The model automatically looks up all of the other feature values from the feature tables.
# MAGIC
# COMMAND ----------
## For simplicity, this example uses inference_data_df as input data for prediction
batch_input_df = inference_data_df.drop("quality") # Drop the label column
predictions_df = fe.score_batch(model_uri="models:/wine_model/latest", df=batch_input_df)
display(predictions_df["wine_id", "prediction"])
# COMMAND ----------
# MAGIC %md ## Modify feature table
# MAGIC Suppose you modify the dataframe by adding a new feature. You can use `fe.write_table` with `mode="merge"` to update the feature table.
# COMMAND ----------
## Modify the dataframe containing the features
so2_cols = ["free_sulfur_dioxide", "total_sulfur_dioxide"]
new_features_df = (features_df.withColumn("average_so2", expr("+".join(so2_cols)) / 2))
display(new_features_df)
# COMMAND ----------
# MAGIC %md Update the feature table using `fe.write_table` with `mode="merge"`.
# COMMAND ----------
fe.write_table(
name=table_name,
df=new_features_df,
mode="merge"
)
# COMMAND ----------
# MAGIC %md To read feature data from the feature tables, use `fe.read_table()`.
# COMMAND ----------
# Displays most recent version of the feature table
# Note that features that were deleted in the current version still appear in the table but with value = null.
display(fe.read_table(name=table_name))
# COMMAND ----------
# MAGIC %md ## Train a new model version using the updated feature table
# COMMAND ----------
def load_data(table_name, lookup_key):
model_feature_lookups = [FeatureLookup(table_name=table_name, lookup_key=lookup_key)]
# fe.create_training_set will look up features in model_feature_lookups with matched key from inference_data_df
training_set = fe.create_training_set(df=inference_data_df, feature_lookups=model_feature_lookups, label="quality", exclude_columns="wine_id")
training_pd = training_set.load_df().toPandas()
# Create train and test datasets
X = training_pd.drop("quality", axis=1)
y = training_pd["quality"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
return X_train, X_test, y_train, y_test, training_set
X_train, X_test, y_train, y_test, training_set = load_data(table_name, "wine_id")
X_train.head()
# COMMAND ----------
# MAGIC %md
# MAGIC Build a training dataset that will use the indicated `key` to lookup features.
# COMMAND ----------
def train_model(X_train, X_test, y_train, y_test, training_set, fe):
## fit and log model
with mlflow.start_run() as run:
rf = RandomForestRegressor(max_depth=3, n_estimators=20, random_state=42)
rf.fit(X_train, y_train)
y_pred = rf.predict(X_test)
mlflow.log_metric("test_mse", mean_squared_error(y_test, y_pred))
mlflow.log_metric("test_r2_score", r2_score(y_test, y_pred))
fe.log_model(
model=rf,
artifact_path="feature-store-model",
flavor=mlflow.sklearn,
training_set=training_set,
registered_model_name="wine_model",
)
train_model(X_train, X_test, y_train, y_test, training_set, fe)
# COMMAND ----------
# MAGIC %md Apply the latest version of the registered MLflow model to features using **`score_batch`**.
# COMMAND ----------
## For simplicity, this example uses inference_data_df as input data for prediction
batch_input_df = inference_data_df.drop("quality") # Drop the label column
predictions_df = fe.score_batch(model_uri=f"models:/wine_model/latest", df=batch_input_df)
display(predictions_df["wine_id","prediction"])
# COMMAND ----------
# MAGIC %md ## Control permissions for and delete feature tables
# MAGIC - To control who has access to a Unity Catalog feature table, use the **Permissions** button on the Catalog Explorer table details page.
# MAGIC - To delete a Unity Catalog feature table, click the kebab menu on the Catalog Explorer table details page and select **Delete**. When you delete a Unity Catalog feature table using the UI, the corresponding Delta table is also deleted.