TExcelTAD
Home › Data Science › DS Exploratory Data Analysis

DS Exploratory Data Analysis

Exploratory Data Analysis (EDA) is the detective work you do before modelling: you look at the data from many angles to learn its structure, spot problems, and form hypotheses. The statistician John Tukey, who named it, described it as "looking at data to see what it seems to say".

EDA has no fixed recipe, but a good routine covers:

  1. Structure — size, types, missing values.
  2. Each variable alone (univariate) — centre, spread, shape, odd values.
  3. Pairs of variables (bivariate) — relationships with the target.
  4. Groups and time — differences between categories, trends over time.
  5. Notes — write down what you found and what to check next.

1. Structure

info() shows types and non-null counts in one call; nunique() reveals which columns are really categories:

python · try it yourself
import numpy as np
import pandas as pd

rng = np.random.default_rng(42)
dates = pd.date_range("2024-04-01", periods=60)
temp = rng.uniform(4, 30, 60).round(1)
rain = np.where(rng.random(60) < 0.3, rng.uniform(1, 15, 60), 0).round(1)
humidity = rng.integers(35, 95, 60)
weekend = (dates.dayofweek >= 5).astype(int)
rides = (150 + 12 * temp - 8 * rain + 60 * weekend + rng.normal(0, 30, 60)).round().astype(int)
bikes = pd.DataFrame({"date": dates, "temp_c": temp, "rain_mm": rain,
                      "humidity": humidity, "weekend": weekend, "rides": rides})

bikes.info()
print()
print(bikes.nunique())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 60 entries, 0 to 59
Data columns (total 6 columns):
 #   Column    Non-Null Count  Dtype
---  ------    --------------  -----
 0   date      60 non-null     datetime64[ns]
 1   temp_c    60 non-null     float64
 2   rain_mm   60 non-null     float64
 3   humidity  60 non-null     int64
 4   weekend   60 non-null     int32
 5   rides     60 non-null     int32
dtypes: datetime64[ns](1), float64(2), int32(2), int64(1)
memory usage: 2.4 KB

date        60
temp_c      54
rain_mm     19
humidity    39
weekend      2
rides       54
dtype: int64

No missing values, sensible types, and weekend has only 2 distinct values — a binary category stored as a number.

2. Each variable alone

Histograms show the shape of a distribution: symmetric or skewed, one peak or several, gaps, extreme values.

python · try it yourself
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
dates = pd.date_range("2024-04-01", periods=60)
temp = rng.uniform(4, 30, 60).round(1)
rain = np.where(rng.random(60) < 0.3, rng.uniform(1, 15, 60), 0).round(1)
humidity = rng.integers(35, 95, 60)
weekend = (dates.dayofweek >= 5).astype(int)
rides = (150 + 12 * temp - 8 * rain + 60 * weekend + rng.normal(0, 30, 60)).round().astype(int)
bikes = pd.DataFrame({"date": dates, "temp_c": temp, "rain_mm": rain,
                      "humidity": humidity, "weekend": weekend, "rides": rides})

cols = ["temp_c", "rain_mm", "humidity", "rides"]
bikes[cols].hist(bins=12, figsize=(9, 6), color="#ff6f00", edgecolor="white")
plt.tight_layout()
plt.show()

print("skewness:")
print(bikes[cols].skew().round(2))
skewness:
temp_c     -0.17
rain_mm     1.68
humidity    0.26
rides      -0.03
dtype: float64

The histograms and the skewness numbers agree: rain is strongly right-skewed (many dry days, a few very wet ones), while temperature, humidity and rides are roughly symmetric. A skewness near 0 means symmetric; positive means a long tail to the right.

3. Relationships with the target

Plot the target against each candidate predictor. Scatter plots for numeric predictors, group summaries for categories:

python · try it yourself
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
dates = pd.date_range("2024-04-01", periods=60)
temp = rng.uniform(4, 30, 60).round(1)
rain = np.where(rng.random(60) < 0.3, rng.uniform(1, 15, 60), 0).round(1)
humidity = rng.integers(35, 95, 60)
weekend = (dates.dayofweek >= 5).astype(int)
rides = (150 + 12 * temp - 8 * rain + 60 * weekend + rng.normal(0, 30, 60)).round().astype(int)
bikes = pd.DataFrame({"date": dates, "temp_c": temp, "rain_mm": rain,
                      "humidity": humidity, "weekend": weekend, "rides": rides})

fig, axes = plt.subplots(1, 3, figsize=(12, 3.5), sharey=True)
for ax, col in zip(axes, ["temp_c", "rain_mm", "humidity"]):
    ax.scatter(bikes[col], bikes["rides"], s=12, c=bikes["weekend"], cmap="coolwarm")
    ax.set_xlabel(col)
axes[0].set_ylabel("rides")
plt.tight_layout()
plt.show()

print(bikes.groupby("weekend")["rides"].agg(["count", "mean", "median"]).round(1))
         count   mean  median
weekend
0           44  327.4   319.0
1           16  432.5   445.5

Colouring the points by weekend (red) versus weekday (blue) reveals a second pattern inside the temperature plot: at the same temperature, weekend days sit higher. That's a hypothesis for the model — and indeed the regression case found about +64 rides at weekends.

4. Time

Data collected over time should always be plotted over time. Trends, weekly cycles or sudden breaks (a new station opening, a broken sensor) show up immediately:

python · try it yourself
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
dates = pd.date_range("2024-04-01", periods=60)
temp = rng.uniform(4, 30, 60).round(1)
rain = np.where(rng.random(60) < 0.3, rng.uniform(1, 15, 60), 0).round(1)
humidity = rng.integers(35, 95, 60)
weekend = (dates.dayofweek >= 5).astype(int)
rides = (150 + 12 * temp - 8 * rain + 60 * weekend + rng.normal(0, 30, 60)).round().astype(int)
bikes = pd.DataFrame({"date": dates, "temp_c": temp, "rain_mm": rain,
                      "humidity": humidity, "weekend": weekend, "rides": rides})

ax = bikes.plot(x="date", y="rides", figsize=(9, 3.5), legend=False)
bikes.plot(x="date", y="rides", kind="scatter", ax=ax, s=10)
ax.set_ylabel("rides")
plt.show()

weekly = bikes.set_index("date")["rides"].resample("W").mean().round(1)
print(weekly.head())
date
2024-04-07    404.3
2024-04-14    390.7
2024-04-21    351.3
2024-04-28    356.6
2024-05-05    332.0
Freq: W-SUN, Name: rides, dtype: float64

The daily line is jagged — mostly weather. Resampling to weekly means (resample("W")) smooths out the day-to-day noise; whether the weekly values reveal a real trend is a question for the Time Series Basics lesson.

5. Write it down

An EDA ends with notes, for example:

  • 60 days, no missing values, no impossible values.
  • Rain is right-skewed; 18 of 60 days were wet.
  • Rides rise strongly with temperature (r ≈ 0.85), fall with rain, are higher at weekends.
  • Humidity shows no visible relationship.
  • Daily rides are noisy over time; trend to be checked with weekly averages.

These notes become the plan for modelling.

Test yourself

What is the main purpose of EDA?

  1. To produce the final model
  2. To understand the data, find problems and form hypotheses before modelling
  3. To make the data bigger
  4. To remove all outliers

Rain has a skewness of about 1.7. What does that tell you?

  1. Rain is symmetric
  2. Rain has a long tail to the right: many small values and a few large ones
  3. Rain values are negative
  4. Rain is constant

Why colour a scatter plot of rides vs temperature by weekend?

  1. To make it prettier
  2. To see whether a third variable shifts the relationship
  3. Because seaborn requires it
  4. To hide outliers