Machine Learning Model Development in Azure Databricks Notebooks
Azure Databricks notebooks provide robust environments for building and deploying machine learning models. Below are two detailed examples of model notebooks, highlighting their workflows and key features.
1. Scikit-learn Classification Model Notebook
This notebook demonstrates building a classification model using scikit-learn, with integration into Unity Catalog and MLflow.
Key Features
- Data Ingestion: Loads CSV files from
databricks-datasets and cleans column names[7].
- Model Training: Uses scikit-learn to train a classifier on wine quality data.
- MLflow Integration: Registers models in Unity Catalog for version control and reproducibility[7].
Workflow Steps
- Create Notebook:
# Create a new notebook and connect to ML runtime
import mlflow
mlflow.set_registry_uri("databricks-uc") # Configure MLflow with Unity Catalog
- Load and Clean Data:
white_wine = spark.read.csv("/databricks-datasets/wine-quality/winequality-white.csv", sep=';', header=True)
# Clean column names by replacing spaces with underscores
for c in white_wine.columns:
white_wine = white_wine.withColumnRenamed(c, c.replace(" ", "_"))
- Train and Register Model:
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
# Convert Spark DataFrame to Pandas
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier()
model.fit(X_train, y_train)
# Register model in MLflow
mlflow.sklearn.log_model(model, "wine_quality_model")
Output:
- Trained model stored in Unity Catalog with version history[7].
- Model metrics (accuracy, precision) tracked via MLflow[7].
2. End-to-End ML Pipeline Notebook
This notebook showcases a complete ML workflow, including hyperparameter tuning, model registration, and inference using Spark UDFs.
Key Features
- Hyperparameter Optimization: Uses MLflow’s
mlflow.projects for parallel tuning[13].
- Model Inference: Deploys registered models as Spark UDFs for batch scoring[13].
- Visualization: Integrates data exploration and model performance plots.
Workflow Steps
- Data Preparation:
# Load data and create Unity Catalog tables
spark.sql(f"CREATE TABLE {CATALOG_NAME}.{SCHEMA_NAME}.red_wine USING delta")
red_wine.write.saveAsTable(f"{CATALOG_NAME}.{SCHEMA_NAME}.red_wine")
- Model Training with MLflow:
with mlflow.start_run():
# Train model and log parameters/metrics
model = train_model(X_train, y_train)
mlflow.log_param("max_depth", 5)
mlflow.log_metric("accuracy", 0.92)
mlflow.sklearn.log_model(model, "wine_quality_model")
- Inference with Spark UDF:
# Load registered model and create UDF
model = mlflow.pyfunc.load_model("models:/wine_quality_model/1")
predict_udf = udf(lambda x: model.predict(x), DoubleType())
# Apply UDF to new data
results = spark.createDataFrame([(1.0, 2.0, 3.0)], ["feature1", "feature2", "feature3"]).withColumn("prediction", predict_udf(array([1.0, 2.0, 3.0])))
Output:
- Model registered in Unity Catalog with versioning[13].
- Inference results stored in Delta Lake tables for downstream analytics[13].
Comparison of Model Notebooks
| Feature | Scikit-learn Notebook [7] | End-to-End Pipeline Notebook [13] |
|---|
| Focus | Single-model training | Full ML lifecycle |
| Hyperparameter Tuning | Manual | Automated via MLflow Projects |
| Inference | Local scoring | Spark UDF for distributed scoring |
| Data Storage | Unity Catalog tables | Delta Lake tables |
Both notebooks leverage Databricks’ collaborative features, including real-time coauthoring, version history, and integration with Git repositories[1][4].