CertLumen

Databricks Machine Learning Associate · Section 2 · 19% of the exam

Impute missing values with the mode, mean, or median value

One of the objectives of the Databricks Certified Machine Learning Associate exam, in the section “Data Processing”. Here is what it covers, the points to remember and a question to try.

What it covers

Choose between mean, median and mode imputation from the shape and type of each column, impute with Spark ML's Imputer and with the mode for categorical columns, one-hot encode categorical features with StringIndexer and OneHotEncoder, and recognize the models and data sets for which one-hot encoding is the wrong choice.

Points to remember

  • Choose the imputation from the column: mean for symmetric continuous data, median for skewed data or outliers, mode for categorical or discrete data.
  • No imputer chooses the strategy automatically; Spark Imputer and scikit-learn SimpleImputer both default to the mean.
  • Fit imputers on the training split only and reuse the fitted values; add a 0/1 missing-indicator column when missingness may be informative.
  • Spark Imputer: strategies mean, median, mode; numeric columns only; NULL always counts as missing; missingValue defaults to NaN; surrogateDF shows the fill values.
  • For string columns, use F.mode(col) (most frequent non-NULL value) with fillna, or SimpleImputer(strategy='most_frequent').

Try a question

A data scientist one-hot encodes training data with pd.get_dummies(train_pdf, columns=["plan"]) and trains a scikit-learn logistic regression. Scoring a small batch with pd.get_dummies(batch_pdf, columns=["plan"]) then fails with a feature-count mismatch, because the batch contains only two of the five plans. Which fix is most robust?

  1. AFit an sklearn Pipeline with OneHotEncoder(handle_unknown="ignore") on training data and score with it
  2. BCall pd.get_dummies on the batch with drop_first=True, so that it produces the same columns as the training data
  3. CSort the batch's columns alphabetically before predicting, so that they line up with the training columns
  4. DRetrain the model on every scoring batch, so that its features always match the batch's dummy columns
Show the answer

A. pd.get_dummies creates one column per level present in the DataFrame it receives, so training and scoring data can end up with different columns. A fitted encoder, such as scikit-learn's OneHotEncoder in a ColumnTransformer and Pipeline, remembers the training categories, gives every batch the same columns and, with handle_unknown="ignore", encodes unseen levels as all zeros. Logging the whole pipeline with MLflow carries the encoding into serving.

Written for practice in the style of the exam: never a question from the real exam.

In the course

The lesson Imputing Missing Values and One-Hot Encoding teaches this objective (about 45 minutes), and 20 practice questions test it. The course has lessons, exam-style questions, timed practice exams and an AI tutor for every objective, and a pass prediction that tells you when you're ready to book.

More in this section

← Previous objective · All 48 objectives · Next objective →