CertLumen

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

Remove outliers from a Spark DataFrame based on standard deviation or IQR

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

Before engineering features you profile the data. This lesson covers describe(), summary(), dbutils.data.summarize and the notebook Data Profile, then removes outliers from a Spark DataFrame with the standard deviation rule and the IQR rule, and explains when each rule is the better choice.

Points to remember

  • describe() gives count, mean, stddev, min and max; summary() adds approximate 25%, 50% and 75% by default.
  • summary() takes statistic names such as 'count' or '90%'; describe() takes column names. Select columns before summary().
  • Both return a DataFrame whose statistic values are strings; cast them or use agg() for arithmetic.
  • dbutils.data.summarize(df) renders per-column statistics and histograms (Public Preview); precise=True gives exact counts (Databricks Runtime 10.4 LTS+).
  • The Data Profile tab (+ > Data Profile on a display() result) shows the same profile without code.

Try a question

Before choosing an imputation strategy, a data scientist needs the number of NULL values in every column of the Spark DataFrame df, returned as one row with one column per input column. Assume from pyspark.sql import functions as F. Which code does this?

  1. Adf.select([F.count(F.when(F.col(c).isNull(), 1)).alias(c) for c in df.columns])
  2. Bdf.select([F.count(c).alias(c) for c in df.columns])
  3. Cdf.summary("nulls")
  4. Ddf.dropna().count()
Show the answer

A. For each column, the when(... .isNull(), 1) expression is 1 for a NULL and NULL otherwise, and F.count counts only non-NULL values, so the expression counts the NULLs of each column in a single pass over the data. summary() and describe() don't report missing values directly: their count is the number of non-NULL values. The NULL count per column drives the choice between dropping rows, dropping the column and imputing.

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

In the course

The lesson Summary Statistics and Outlier Removal teaches this objective (about 40 minutes), and 15 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 →