Exploratory data analysis

Look before you model: distributions, relationships, quality problems and surprises.

Lesson 7 of 6610 minBeginner

You will learn to

  • Read distributions and summary statistics
  • Use correlations without over-trusting them
  • Write down questions EDA raises

Before this lesson

Intuition

Before you cook, you look in the fridge. Before you model, you look at the data. Exploratory data analysis (EDA) is that first look: reading each column, checking how values are spread, seeing which columns move together, and finding the surprises before a model quietly learns from them.

EDA is not about proving anything. It is about getting to know the data well enough to ask good questions and to avoid bad surprises: a column that is empty, a number in the wrong unit, a category spelled three ways, a pattern that is too good to be true. Most of the problems that hurt a model later can be seen at this stage in a few minutes.

A good routine has four parts:

  1. Shape and quality. How many rows and columns? How much is missing? Are there duplicate rows?
  2. One column at a time. What is the range, the typical value and the spread? Are there extreme values?
  3. Pairs of columns. Which columns move together? How does each relate to the target?
  4. Questions. Write down what looks odd. Each one is a task: check the source, fix, or ask someone who knows.

Picture it

Summary numbers are useful, but they can hide the shape of the data. The four small datasets below are the famous Anscombe quartet. They have the same mean, the same variance, the same correlation and even the same fitted line, and yet they look nothing alike.

I: a line with noiseII: a curveIII: one outlierIV: one lever pointSame mean, variance, correlation (0.82) and line in all four
Anscombe's quartet: four datasets with identical summary statistics (and the same fitted line) but different structure. Always plot.

In the first, a straight line describes the data well. In the second the relationship is a curve, so a line is the wrong model. In the third one outlier drags the line. In the fourth, one point far to the right decides everything, because all the others share the same x.

The math

Four statistics do most of the work in a first read.

The mean and median describe the centre. The mean is xˉ=1n∑i=1nxi\bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i, and the median is the middle value when the data are sorted. They differ when the distribution is skewed or has outliers.

The standard deviation describes the spread:

s=1n−1∑i=1n(xi−xˉ)2s = \sqrt{\frac{1}{n-1}\sum_{i=1}^{n}(x_i - \bar{x})^2}

The Pearson correlation of two columns xx and yy measures how closely they follow a straight line, from -1 to 1:

r=∑i(xi−xˉ)(yi−yˉ)∑i(xi−xˉ)2 ∑i(yi−yˉ)2r = \frac{\sum_{i}(x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i}(x_i - \bar{x})^2}\,\sqrt{\sum_{i}(y_i - \bar{y})^2}}

A value near 1 means that when one goes up, the other tends to go up; near -1, down; near 0, no straight-line relation. Note what it does not say: it does not say that one causes the other, and it can be near 0 for a strong but curved relation.

Worked example

Mean against median. Ten illustrative salaries (in thousands): 20, 25, 30, 35, 40, 45, 55, 70, 90 and 120. The sum is 530, so the mean is 53. The median is the average of the 5th and 6th values, (40 + 45) / 2 = 42.5. The mean sits 10.5 above the median because the top three values pull it up. Which is the "typical" salary? The median describes a typical row better. When you see a mean well above the median in a profile, expect a long tail on the right.

The quartet. We computed these statistics for the four published datasets (11 points each). All four have mean of x equal to 9, mean of y equal to 7.50, variance of x equal to 11, variance of y about 4.12, a correlation of 0.816 and a fitted line of y = 3.00 + 0.50 x. A report that listed only these numbers would describe four different situations in the same way. Plotting shows the curve in the second set, the single outlier in the third and the single lever point in the fourth.

A checklist of questions you might write down after a first pass on a customer table (illustrative):

  • Why does the column income have 18% missing? Is it random, or are some customers less likely to answer?
  • The mean age is 41 but the maximum is 212. Is that a typo or a different unit?
  • total_spend and n_orders have a correlation of 0.93. Do we need both?
  • The target is yes for 8% of rows. How will we handle the imbalance?
  • A column that is the same for every row: why is it in the file?

How mlflow does it

Open Data, choose a dataset and select Explore to see the data profile. It has six tabs:

  • Overview: tiles for Rows, Columns, Missing, Duplicates, Memory and the counts of numeric, categorical and datetime columns, plus the Top columns by missing.
  • Columns: one row per column with its Type, Missing and Unique counts. Expanding a column shows its Distribution (a histogram) and Statistics: Min, Q1, Median, Q3, Max, Mean, Std, Skew, Kurtosis, Zeros and Outliers (IQR). Categorical columns show Top values.
  • Correlations: a heatmap of the Pearson correlation between numeric columns and a list of the Strongest correlations. It needs at least two numeric columns.
  • Missing: the missing cells and the missing rate by column.
  • Quality: issues found in the dataset, such as correlated pairs, duplicate columns and constant or ID-like columns, with an overall grade.
  • Sample: the first rows of the data.

For work beyond the profile (your own charts and tests), the notebooks and analyses features let you explore the data in code; see the docs page linked below.

Common mistakes

  • Trusting summary numbers without a plot. The quartet above shows why. Look at distributions and scatter plots, not only the table of statistics.
  • Reading correlation as cause. Ice cream sales and sunburn correlate because of the weather. Correlation tells you where to look, not what to conclude.
  • Reading a correlation of 0 as "unrelated". Pearson only sees straight-line patterns. A U-shaped relationship can have a correlation near 0.
  • Ignoring the target in EDA. Look at how the target is distributed and how it differs across groups of the main columns. That is where predictive signal, and leakage, first shows up.
  • Cleaning before understanding. Do not delete outliers or fill gaps before you know why they are there. An extreme value is sometimes a typo and sometimes the most interesting row.
  • Exploring on all the data and then testing on it. Patterns you discover on the full dataset can leak into your choices. Keep a test set aside and untouched, as the lessons on splitting explain.

Try it in mlflow

Read the data profile

  1. Open Data, choose a dataset and select Explore.
  2. On Overview, read the Rows, Columns, Missing and Duplicates tiles and the Top columns by missing.
  3. Open Correlations and read the Strongest correlations; then open Columns and expand the two columns of the top pair to look at their distributions.
  4. Write down three questions the profile raised.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. A column of salaries has a mean of 53 and a median of 42.5. What does the gap suggest?
  2. 2. Two columns have a correlation of 0.8. Which conclusion is justified?
  3. 3. Four datasets share the same mean, variance, correlation and fitted line, yet look completely different when plotted. What does this show?