Which Spark ML component fills missing numeric values with the median?
- Exam domain
- Data Processing20% of the exam
- Difficulty
- Medium
- In this mock
- 10 questions on this domainPractise this domain
Why the exam asks this
Data Processing (20% of the exam)
Objective in the exam guide: Impute missing values with the mode, mean, or median value
Imputing missing values is named.
Why C is the answer
Imputer(strategy="median", inputCols=[...], outputCols=[...])
pyspark.ml.feature.Imputer supports mean, median and mode strategies for numeric columns.
Remember: Spark ML Imputer.
How it works
Imputer is an estimator that learns fill values.
In detail
- Imputer with strategy.
- Fit on training data only.
Every option, checked
Knowing why each wrong option fails is how you rule it out on exam day.
A. VectorAssembler(inputCols=[...], outputCol="features", handleInvalid="median")Wrong
VectorAssembler builds feature vectors.
B. df.dropna(how="median")WrongMost tempting
dropna drops rows; it has no median option.
Why it tempts: Dropping is easier but loses data.
C. Imputer(strategy="median", inputCols=[...], outputCols=[...])Right
Spark ML Imputer.
D. StringIndexer(strategy="median")Wrong
StringIndexer encodes strings.
Use case
An illustrative scenario, not a real incident.
Illustrative: an Imputer fitted on training data fills test gaps with the training median.
See it in different ways
flowchart LR S0["Imputer"] --> S1["Fit on train"] --> S2["Transform train and test"]
How to read this chart
- Read it in order: Imputer, then Fit on train, then Transform train and test.
sequenceDiagram participant P0 as Pipeline participant P1 as Imputer participant P2 as ImputerModel P0->>P1: fit on train P1->>P2: learned medians P2-->>P0: transform
How to read this chart
- 1. Pipeline sends "fit on train" to Imputer.
- 2. Imputer sends "learned medians" to ImputerModel.
- 3. Pipeline gets a reply from ImputerModel: "transform".