CertLumen

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

Compute summary statistics on a Spark DataFrame using .summary() or dbutils data summaries

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

A listings table covers five neighborhoods whose typical prices differ by a factor of ten. One set of IQR fences computed over all listings removes most genuine listings in the most expensive neighborhood, while data-entry errors in the cheapest neighborhood still fall inside the overall fences. Which approach handles outliers correctly for this data?

  1. ASwitch to mean +/- 3 standard deviations computed over all listings, which adapts better to differences between groups
  2. BCompute Q1 and Q3 per neighborhood, for example with groupBy and F.percentile_approx, join them back and apply each neighborhood's own fences
  3. CWiden the overall fences to Q1 - 3 x IQR and Q3 + 3 x IQR, so that the expensive listings are kept
  4. DOne-hot encode neighborhood before computing the fences, so that the quartiles take the neighborhood into account
Show the answer

B. When a continuous feature depends strongly on a group, a value is unusual relative to its own group, not to the whole table. Computing the quartiles per neighborhood (df.groupBy('neighborhood').agg(F.percentile_approx('price', [0.25, 0.75]))), joining them back and filtering with each group's fences removes the errors in cheap areas without discarding genuine expensive listings. A box plot of price per neighborhood shows the problem before you filter.

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 →