A data pipeline already runs in Spark, but the model is a scikit-learn regressor. How do you scale batch inference across the cluster?
- Exam domain
- Model Development44% of the exam
- Difficulty
- Medium
- In this mock
- 22 questions on this domainPractise this domain
Why the exam asks this
Model Development (44% of the exam)
Objective in the exam guide: Scale distributed training pipelines using SparkML and pandas Function APIs/UDFs.
Scaling with pandas Function APIs and UDFs is named.
Why B is the answer
Apply it as a pandas UDF, such as mlflow.pyfunc.spark_udf
Pandas UDFs run single-node code on batches across executors. mlflow.pyfunc.spark_udf builds one from a logged model.
Remember: pandas UDFs scale single-node models.
How it works
Pandas UDFs apply Python code across Spark partitions.
In detail
- Pandas UDFs distribute single-node code.
- spark_udf from MLflow models.
Every option, checked
Knowing why each wrong option fails is how you rule it out on exam day.
A. Rewrite the regressor in Spark ML before scoringWrongMost tempting
Rewriting is unnecessary.
Why it tempts: Rewriting in Spark ML seems native.
B. Apply it as a pandas UDF, such as mlflow.pyfunc.spark_udfRight
pandas UDFs scale single-node models.
C. Score one row at a time in a Python loopWrong
Row loops are slow.
D. Collect the full DataFrame to the driver with toPandas() and call predict once on the driver nodeWrong
Collecting to the driver does not scale.
Use case
An illustrative scenario, not a real incident.
Illustrative: 500 million rows are scored with spark_udf in 12 minutes.
See it in different ways
sequenceDiagram participant P0 as Spark DataFrame participant P1 as Executors participant P2 as pandas UDF P0->>P1: partitions P1->>P2: batches as pandas P2-->>P1: predictions
How to read this chart
- 1. Spark DataFrame sends "partitions" to Executors.
- 2. Executors sends "batches as pandas" to pandas UDF.
- 3. Executors gets a reply from pandas UDF: "predictions".
flowchart LR S0["Log model"] --> S1["spark_udf"] --> S2["withColumn predictions"] --> S3["Write"]
How to read this chart
- Read it in order: Log model, then spark_udf, then withColumn predictions, then Write.