Sub topic 17
Where AI and Machine Learning Fit
This chapter assumes you have read the earlier chapters on the lakehouse, the workspace, compute, notebooks, Delta Lake, and Unity Catalog. You should be comfortable querying a table by its three-level name. You do not need prior machine learning experience, but you should know what a model is: a function learned from data that turns inputs into predictions.
The goal here is orientation. AI and machine learning (ML) on Databricks are not a separate product bolted on the side. They use the same workspace, the same compute, and the same governed tables you have already seen. This chapter shows where those pieces connect.
The core idea: ML uses the same foundations
A common ML problem is that data lives in one system, training happens in another, and models are stored in a third. Each handoff copies data and loses track of who can see what. The lakehouse design aims to remove those handoffs.
On Databricks, the pieces map like this:
| ML need | Platform piece you already know |
|---|---|
| Training data and features | Delta tables in Unity Catalog |
| Development environment | Notebooks in the workspace |
| Processing power | Clusters (all-purpose or job compute) |
| Scheduling retraining | Jobs |
| Access control | Unity Catalog privileges |
| Model storage | Registered models in a Unity Catalog schema |
The last row is the main addition. Unity Catalog can hold models as well as tables, so a model has a three-level name such as main.tutorial.churn_model and is governed with the same kind of GRANT statements.
The ML lifecycle on the platform
A typical ML project moves through these stages. Each one uses a capability of the platform.
Preparing data
Feature preparation is ordinary data engineering. You read a Delta table with Spark or SQL, clean it, and compute columns the model will use. Because the source is a governed table, the lineage and permissions of the original data stay attached to your work.
Training and tracking experiments
Training runs on compute you attach to a notebook. For small data, a single-node cluster running scikit-learn is enough. For larger data, Spark can distribute the work. Databricks integrates MLflow, an open source tool that records each training run: its parameters, metrics, and resulting model files. This lets you compare runs instead of guessing which notebook cell produced the best result.
Registering and deploying models
When a model is good enough, you register it. A registered model has numbered versions, so you can promote a new version without overwriting the old one. Deployment then takes one of two forms:
- Batch scoring: a job reads a table, applies the model, and writes predictions to another Delta table. This is the simplest form and fits nightly or hourly needs.
- Online serving: the model is exposed as a REST endpoint that returns a prediction for each request. This fits applications that need an answer in milliseconds to seconds.
A small worked example
The following notebook code trains a simple model on a small dataset and logs it with MLflow. It uses scikit-learn and the MLflow package, which are included in Databricks Runtime for Machine Learning. On other runtimes you may need to install them first. It generates its own data, so it runs without any tables.
import mlflow
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
rng = np.random.default_rng(seed=42)
X = rng.uniform(0, 10, size=(200, 1))
y = 3.0 * X[:, 0] + 2.0 + rng.normal(0, 1.0, size=200)
with mlflow.start_run(run_name="linear_baseline"):
model = LinearRegression().fit(X, y)
mse = mean_squared_error(y, model.predict(X))
mlflow.log_param("model_type", "LinearRegression")
mlflow.log_metric("mse", mse)
mlflow.sklearn.log_model(model, "model")
print(f"Logged run with MSE {mse:.3f}")After this cell runs, the run appears under the notebook's experiment in the workspace. You can open it, view the metric, and compare it with later runs.
To use real data, replace the generated arrays with a table. For example, you could call spark.table("main.tutorial.orders").toPandas() on a small table. Convert to pandas only when the data fits in the memory of the driver, the machine that coordinates the cluster.
Generative AI and large language models
Databricks also supports generative AI work. The same governance idea applies: you can call hosted foundation models, build retrieval systems over your own tables and documents, and serve custom models, with access managed through the platform. You can also query models from SQL using AI functions, which let an analyst apply a model to a column without writing Python.
The exact set of hosted models, function names, and serving options changes often. Check the current Databricks documentation before you depend on a specific one. What stays stable is the pattern: your data stays in governed tables, the model is a governed object, and compute is the same family of clusters, warehouses, and serverless options.
Choosing compute for ML work
Compute choices from the earlier chapter still apply.
- All-purpose compute suits interactive exploration and development in a notebook.
- Job compute suits scheduled training or batch scoring. It starts for a run and ends afterward, which is usually cheaper.
- SQL warehouses suit SQL workloads, including SQL-based AI functions, but they do not run arbitrary Python training code.
- GPU-enabled clusters are available for deep learning. They cost more per hour, so shut them down when you finish.
Common errors and gotchas
- Collecting too much data to the driver. Calling
toPandas()on a large table can crash the driver with an out-of-memory error. Filter or sample first, or use a distributed approach. - Missing permissions on models. Registering or loading a model needs privileges on the catalog and schema, just like tables. A
PERMISSION_DENIEDerror often means a missingUSE SCHEMAor a missing privilege to create models in the schema. - Leaving experiments untracked. If you do not log runs, you cannot reproduce which data and parameters produced a model. Log parameters and metrics from the start.
- Training and serving drift. If you compute features one way during training and another way at serving time, predictions degrade quietly. Reuse the same feature code in both places.
- Forgetting cost. Large clusters and GPUs bill by the hour through DBUs, so idle development clusters add up. Set auto-termination.
Summary
AI and ML on Databricks reuse the platform you have already learned. Delta tables provide governed data, notebooks provide the workspace, clusters provide compute, jobs provide scheduling, and Unity Catalog governs both data and models. MLflow tracks experiments, and registered models can be served in batch or online.
Exercise
Your team trains a model nightly and writes predictions to a Delta table that analysts read the next morning. Which compute type should run the training, and which deployment form is this?
Show answer
Use job compute, because it starts for the scheduled run and shuts down afterward, which costs less than a long-running all-purpose cluster. This is batch scoring, since predictions are written to a table rather than returned per request from an endpoint.
Exercise
A colleague gets a permission error when loading a registered model named main.tutorial.churn_model, even though they can query tables in main.tutorial. Name one likely cause and one fix.
Show answer
The colleague likely lacks a privilege on the model itself, such as permission to execute or use it, because models are governed objects separate from tables. A fix is for the model owner or an administrator to grant the needed privilege on the model, and to confirm the colleague has USE CATALOG and USE SCHEMA on its parents.