Sub topic 3
Exploring the data before modeling
This chapter assumes you can use pandas DataFrames, read summary statistics such as mean and standard deviation, and know what a supervised target is. Exploratory data analysis (EDA) is the step where you look at the data before fitting any model. The goal is to find problems and make cleaning and feature decisions from evidence.
What to check
Start with questions that a model cannot answer for you.
| Question | Why it matters | pandas tool |
|---|---|---|
| How many rows and columns are there? | Small data limits which models are safe | `df.shape` |
| What type is each column? | Numbers stored as text break scaling | `df.dtypes` |
| How much is missing, and where? | Decides the imputation strategy | `df.isna().sum()` |
| What is the target distribution? | Imbalance affects the split and metric | `value_counts(normalize=True)` |
| Are there duplicate rows? | Duplicates can land in both train and test | `df.duplicated().sum()` |
| Which features relate to the target? | Gives a first sense of signal | `corrwith` |
A first pass in code
from sklearn.datasets import load_breast_cancer
df = load_breast_cancer(as_frame=True).frame # features plus a "target" column
print("shape:", df.shape)
print("column types:\n", df.dtypes.value_counts())
print("missing values:", df.isna().sum().sum())
print("duplicate rows:", df.duplicated().sum())
print("target share:\n", df["target"].value_counts(normalize=True))
summary = df.describe().T[["mean", "std", "min", "max"]]
print(summary.head())
corr = df.drop(columns="target").corrwith(df["target"]).sort_values()
print("most negative:\n", corr.head(3))
print("most positive:\n", corr.tail(3))Read the output with specific questions in mind:
- The target share shows whether one class dominates. If it does, you will want a stratified split and a metric other than accuracy.
- The
minandmaxcolumns show scale differences. Features that range from 0.01 to 2500 are a reason to scale before using a linear model. - A
minof -999 or amaxfar beyond the rest often means a placeholder value or an entry error, not a real measurement. - A strong correlation with the target is a lead, not proof. Correlation only captures linear relationships and says nothing about causation.
Suspicious features
If one feature predicts the target almost perfectly, check how it was recorded before celebrating. It may be recorded after the outcome happened, such as a "refund issued" column when predicting churn. That is data leakage, covered in a later chapter. EDA is where you usually first spot it.
Common errors
- Skipping plots. Summary numbers hide shape, so plot a histogram of each key feature and a count plot of the target.
- Treating missing values as random. Check whether missingness itself relates to the target.
- Dropping outliers automatically. A large value may be the most informative row in the data.
- Never writing down what you found. Keep a short list of issues, since each one becomes a cleaning or feature step.
Exercise
Your summary shows an "age" column with a minimum of -1 and 4% of rows at exactly that value. What are two possible explanations, and what do you do before cleaning?
Show answer
The value -1 is probably a placeholder for "unknown" or an entry error, since a negative age is impossible. Check the data source or documentation to learn what it means, and check whether those rows differ in the target. Then convert -1 to a missing value so the imputer can handle it, instead of leaving it as a real number.