Databricks Machine Learning Associate · Section 2 · 19% of the exam
Use one-hot encoding for categorical features
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
Imputerand scikit-learnSimpleImputerboth 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;missingValuedefaults to NaN;surrogateDFshows the fill values. - For string columns, use
F.mode(col)(most frequent non-NULL value) withfillna, orSimpleImputer(strategy='most_frequent').
Try a question
The feature body_temp_c is roughly normally distributed with no outliers, and its mean and median are almost equal. About 3% of values are missing. Which statement about imputing it is correct?
- AMode imputation is the best choice for every continuous column, because it fills in a value that was actually observed
- BMedian imputation is required, because a long tail always biases the mean of a continuous column
- CMean imputation is appropriate, because the distribution is symmetric with no outliers to distort the mean
- DThe values can't be imputed with a statistic; they must first be predicted with a separate model
Show the answer
C. Choose the fill value from the column's type and shape: the mean for roughly symmetric continuous data with few outliers, the median for skewed data or data with outliers, and the mode for categorical or discrete data. When the distribution is symmetric, the mean and median are close and the mean is a sound choice, for example Imputer(strategy="mean"), the default.
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
- Compute summary statistics on a Spark DataFrame using .summary() or dbutils data summaries
- Remove outliers from a Spark DataFrame based on standard deviation or IQR
- Create visualizations for categorical or continuous features
- Compare two categorical or two continuous features using the appropriate method
- Compare and contrast imputing missing values with the mean or median or mode value
- Impute missing values with the mode, mean, or median value
- Identify and explain the model types or data sets for which one-hot encoding is or is not appropriate.
- Identify scenarios where log scale transformation is appropriate