Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Machine Learning Associate

Outliers and missing values

Databricks Machine Learning Associate · Lesson 9 of 19 · 1:10:56

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Machine Learning Associate exam. In this lesson: finding outliers with the standard deviation and the interquartile range, choosing the mean, median, or mode to impute missing values, and the Spark ML Imputer. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Forgotten scooters

The trip data of Juniper Rides has two problems: a few trips last more than a day, when a rider forgot to lock a scooter, and the battery level is missing for one trip in twenty, after a firmware bug. Both would mislead a demand model if they stayed as they are. This lesson covers how to remove outliers with the standard deviation or the interquartile range, and how to impute missing values with the mean, the median, or the mode.

The standard deviation

An outlier is a value far from the others, often an error, like a trip that lasts thirty hours, but sometimes a rare and real event. The standard deviation method keeps the values within a few standard deviations of the mean, often three, and removes the rest. In Spark, compute the mean and the standard deviation with an aggregation, then filter the DataFrame between the lower and the upper bounds. This method assumes a roughly symmetric, bell shaped distribution, since the outliers themselves pull the mean and inflate the standard deviation. So it suits a feature like a battery temperature, and less a skewed one, like trip duration.

The interquartile range

The interquartile range, or IQR, is the distance between the first quartile, the twenty fifth percentile, and the third quartile, the seventy fifth. The usual rule removes the values below the first quartile minus one and a half times the IQR, and above the third quartile plus one and a half times the IQR. In Spark, the approxQuantile method returns the quartiles of a column, with a relative error that you choose, and a filter keeps the values between the two bounds. Because extreme values barely move the quartiles, the IQR method is robust, and it suits skewed features, like trip duration. Before removing anything, check whether the outliers are errors or rare real events, since a model may need to learn from the real ones.

Choosing a value to impute

Missing values can be dropped, or imputed, which replaces them with a value computed from the column. The mean keeps the average of the column, but extreme values pull it, so it suits symmetric numeric features. The median, the middle value, resists outliers, and suits skewed numeric features, like trip duration or a repair cost. The mode, the most frequent value, suits categorical or discrete features, like the vehicle type or the number of passengers. And an indicator column that flags the imputed rows keeps the information that a value was missing, which matters when the absence itself means something.

The Imputer

In Spark ML, the Imputer fills the missing values of numeric columns, with a strategy of mean, the default, median, or mode. It is an estimator: fit computes the value of each column on the training data, and returns an ImputerModel, whose transform method then fills any DataFrame. Fit it on the training data only, then apply the same model to the validation, test, and new data, so that no information leaks from them into training. The Imputer does not support categorical columns, so fill those with the mode yourself, for example with fillna. By default, it treats nulls and NaN values as missing, and setMissingValue can mark another value, like minus one, as missing. In scikit-learn, the SimpleImputer does the same, with the mean, the median, the most frequent value, or a constant.

Recap

Let's recap. The standard deviation method keeps values within a few standard deviations of the mean, and suits symmetric features. The IQR method removes values beyond one and a half IQR outside the quartiles, from approxQuantile, and suits skewed features. Impute symmetric numbers with the mean, skewed numbers with the median, and categories with the mode, and flag the imputed rows when the absence matters. The Imputer of Spark ML is an estimator for numeric columns, with mean, median, or mode, fitted on the training data only. And outliers that are real events may be worth keeping.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. Battery temperatures are roughly symmetric, with a mean of 18 degrees and a standard deviation of 4. With a three standard deviation rule, which values are removed?

    • Below 14 and above 22
    • Below 6 and above 30
    • Below 10 and above 26
    • Only the values above 30
    Show the answer

    Below 6 and above 30

    Three standard deviations of four degrees are twelve degrees, so the bounds are six and thirty. “Below 14 and above 22” uses one standard deviation, and would remove many normal values. “Below 10 and above 26” uses two standard deviations, not three. And “Only the values above 30” keeps the low outliers, which are just as unusual.

  2. Repair costs are strongly skewed to the right: most repairs are small, and a few cost a thousand times more. About 3% of the costs are missing. Which value should replace them?

    • The mean of the column
    • The mode of the column
    • Zero for every missing row
    • The median of the column
    Show the answer

    The median of the column

    The median resists the few very large costs, so it stays close to a typical repair. “The mean of the column” would be pulled up by the most expensive repairs. “The mode of the column” suits categories, while nearly every cost is a different number. And “Zero for every missing row” would add a value that no real repair has.

  3. A data scientist fits an Imputer with the median strategy on the full dataset, and only then splits it into training and test sets. What is the problem?

    • Test data leaks into the imputed values
    • The Imputer has no median strategy
    • The Imputer cannot run on a full dataset
    • The test set keeps its missing values
    Show the answer

    Test data leaks into the imputed values

    The medians were computed with the test rows, so information from the test set leaks into training, and the test score looks better than it should. “The Imputer has no median strategy” is wrong, since median is one of the three strategies of the Imputer. “The Imputer cannot run on a full dataset” is wrong too, since the Imputer can run on any DataFrame. And “The test set keeps its missing values” is wrong, since the model fills every DataFrame that it transforms.