An ML Pipeline in Spark is a sequence of data processing steps and machine learning algorithms combined into one workflow. It helps organize and automate the process of preparing data, training models, and making predictions.
Click to reveal answer
beginner
Name the main components of a Spark ML Pipeline.
The main components are: Transformers (which change data), Estimators (which learn from data), and the Pipeline itself (which chains these steps together).
Click to reveal answer
beginner
What is the role of a Transformer in a Spark ML Pipeline?
A Transformer takes a DataFrame as input and returns a new DataFrame with added or changed columns. For example, it can convert text into numbers or apply a trained model to make predictions.
Click to reveal answer
intermediate
How does an Estimator differ from a Transformer in Spark ML Pipelines?
An Estimator is an algorithm that learns from data and produces a Transformer. For example, a machine learning algorithm like LogisticRegression is an Estimator that fits a model and returns a Transformer to make predictions.
Click to reveal answer
beginner
Why use ML Pipelines in Spark instead of separate steps?
ML Pipelines make the workflow easier to manage, repeat, and share. They help avoid mistakes by chaining steps automatically and allow easy tuning and testing of different models.
Click to reveal answer
What does a Transformer do in a Spark ML Pipeline?
ATransforms a DataFrame by adding or changing columns
BLearns from data and creates a model
CSplits data into training and testing sets
DVisualizes the data
✗ Incorrect
A Transformer takes a DataFrame and returns a new DataFrame with changes, like adding features or predictions.
Which component in Spark ML Pipelines fits a model to data?
ATransformer
BEstimator
CPipeline
DEvaluator
✗ Incorrect
An Estimator learns from data and produces a Transformer model.
What is the purpose of a Pipeline in Spark ML?
ATo visualize model results
BTo store data permanently
CTo chain multiple data processing and ML steps together
DTo clean data manually
✗ Incorrect
A Pipeline chains Transformers and Estimators into one workflow.
Which of these is NOT a typical step in a Spark ML Pipeline?
AData visualization
BModel training
CFeature transformation
DPrediction
✗ Incorrect
Data visualization is usually done outside the pipeline.
Why is using ML Pipelines helpful in Spark?
AThey visualize data trends
BThey automatically improve model accuracy
CThey replace the need for data cleaning
DThey make workflows repeatable and easier to manage
✗ Incorrect
Pipelines help organize and automate the ML workflow for better management.
Explain the difference between an Estimator and a Transformer in Spark ML Pipelines.
Think about which one trains and which one applies.
You got /4 concepts.
Describe why ML Pipelines are useful when building machine learning models in Spark.
Consider how pipelines help manage complexity.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using ML Pipelines in Apache Spark?
easy
A. To visualize data in charts and graphs
B. To chain multiple data processing and machine learning steps into one workflow
C. To store data permanently in a database
D. To write SQL queries for data extraction
Solution
Step 1: Understand ML Pipelines concept
ML Pipelines are designed to combine multiple steps like data cleaning, feature extraction, and model training into a single workflow.
Step 2: Identify the main purpose
This chaining helps automate the process and apply the same steps easily to new data.
Final Answer:
To chain multiple data processing and machine learning steps into one workflow -> Option B
Quick Check:
ML Pipelines = chaining steps [OK]
Hint: Remember: Pipelines automate and chain ML steps [OK]
Common Mistakes:
Confusing pipelines with data visualization tools
Thinking pipelines store data permanently
Assuming pipelines are only for SQL queries
2. Which of the following is the correct way to create a Pipeline in Spark ML?
easy
A. pipeline = Pipeline(stages=[tokenizer, hashingTF, lr])
B. pipeline = Pipeline(steps=[tokenizer, hashingTF, lr])
C. pipeline = Pipeline(chain=[tokenizer, hashingTF, lr])
D. pipeline = Pipeline(process=[tokenizer, hashingTF, lr])
Solution
Step 1: Recall Spark ML Pipeline syntax
In Spark ML, the Pipeline constructor expects a parameter named stages which is a list of transformers and estimators.
Step 2: Match the correct parameter name
Only pipeline = Pipeline(stages=[tokenizer, hashingTF, lr]) uses stages correctly; others use incorrect parameter names.
Final Answer:
pipeline = Pipeline(stages=[tokenizer, hashingTF, lr]) -> Option A
Quick Check:
Use 'stages' to list pipeline steps [OK]
Hint: Use 'stages' parameter to add steps in Pipeline [OK]
Common Mistakes:
Using 'steps' instead of 'stages'
Using 'chain' or 'process' which are invalid
Forgetting to pass a list of stages
3. Given the following Spark ML Pipeline code, what will be the output type of model.transform(testData)?
from pyspark.ml import Pipeline
from pyspark.ml.feature import Tokenizer, HashingTF
from pyspark.ml.classification import LogisticRegression
# Define stages
tokenizer = Tokenizer(inputCol='text', outputCol='words')
hashingTF = HashingTF(inputCol='words', outputCol='features')
lr = LogisticRegression(maxIter=10, regParam=0.01)
pipeline = Pipeline(stages=[tokenizer, hashingTF, lr])
# Fit the pipeline
model = pipeline.fit(trainingData)
# Transform test data
result = model.transform(testData)
print(type(result))
medium
A.
B.
C.
D.
Solution
Step 1: Understand what model.transform returns
In Spark ML, calling transform() on a PipelineModel returns a DataFrame with new columns added by the pipeline stages.
Step 2: Identify the output type
The output is a Spark DataFrame, not a list, dict, or PipelineModel.
Final Answer:
<class 'pyspark.sql.dataframe.DataFrame'> -> Option D
Quick Check:
model.transform() returns DataFrame [OK]
Hint: transform() always outputs a DataFrame in Spark ML [OK]
Common Mistakes:
Thinking transform returns the model itself
Expecting a Python list or dict instead of DataFrame
Confusing fit() output with transform() output
4. Identify the error in the following Spark ML Pipeline code snippet:
from pyspark.ml import Pipeline
from pyspark.ml.feature import Tokenizer, HashingTF
tokenizer = Tokenizer(inputCol='text', outputCol='words')
hashingTF = HashingTF(inputCol='words', outputCol='features')
pipeline = Pipeline(stages=[tokenizer, hashingTF])
model = pipeline.fit(trainingData)
result = model.transform(testData)
print(result.select('prediction').show())
medium
A. Missing an estimator stage like LogisticRegression before fitting
B. Tokenizer inputCol should be 'words' instead of 'text'
C. Pipeline stages should be passed as a tuple, not a list
D. transform() cannot be called on the pipeline model
Solution
Step 1: Check pipeline stages
The pipeline only has transformers (Tokenizer and HashingTF) but no estimator (like LogisticRegression) to train a model.
Step 2: Understand fit() behavior
Fitting a pipeline without an estimator returns a PipelineModel but no trained model for prediction; this is valid but usually incomplete for ML tasks.
Step 3: Identify the error
For supervised learning, missing an estimator means no model is trained, so the pipeline is incomplete.
Final Answer:
Missing an estimator stage like LogisticRegression before fitting -> Option A
Quick Check:
Estimator needed for training model [OK]
Hint: Always include an estimator for training in pipeline stages [OK]
Common Mistakes:
Confusing inputCol names
Thinking stages must be tuples
Believing transform() can't be called on PipelineModel
5. You want to create a Spark ML Pipeline that preprocesses text data, trains a logistic regression model, and then applies the same pipeline to new streaming data. Which approach is best to ensure the pipeline works correctly on both batch and streaming data?
hard
A. Use only Tokenizer and HashingTF in the pipeline; train LogisticRegression on streaming data directly
B. Train LogisticRegression separately on batch data; apply Tokenizer and HashingTF manually on streaming data
C. Build a Pipeline with Tokenizer, HashingTF, LogisticRegression; fit on batch data; save and load the PipelineModel for streaming transform
D. Fit the pipeline on streaming data first, then apply it to batch data
Solution
Step 1: Understand pipeline reuse for batch and streaming
To apply the same transformations and model to streaming data, you must save the trained PipelineModel from batch training.
Step 2: Best practice for streaming
Fit the pipeline on batch data, save the PipelineModel, then load it in streaming jobs to transform streaming data consistently.
Step 3: Evaluate options
Build a Pipeline with Tokenizer, HashingTF, LogisticRegression; fit on batch data; save and load the PipelineModel for streaming transform follows this best practice. Other options either separate steps manually or train on streaming data first, which is less reliable.
Final Answer:
Build a Pipeline with Tokenizer, HashingTF, LogisticRegression; fit on batch data; save and load the PipelineModel for streaming transform -> Option C
Quick Check:
Save and reuse PipelineModel for streaming [OK]
Hint: Train pipeline on batch, save model, reuse for streaming [OK]
Common Mistakes:
Training model separately from preprocessing
Trying to train on streaming data first
Not saving/loading PipelineModel for reuse
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.