Sub topic 2
Collecting and Understanding Your Data
This chapter assumes you can use pandas DataFrames, read basic summary statistics, and know what supervised learning means. It covers the step between framing the problem and exploring the data: deciding what data to collect and checking that it fits your target.
Why this step comes first
A model can only learn patterns that exist in its data. If the rows don't represent the cases you will predict later, no tuning will fix that. Most late-stage surprises trace back to a data question nobody asked early on.
Ask four things before you write any modeling code:
- Unit of observation. What does one row represent: a customer, a transaction, a patient visit?
- Target availability. Is the label recorded for every row, and when was it recorded?
- Feature availability. Will each column be known at the moment you need a prediction?
- Representativeness. Do these rows look like the future cases the model will see?
Sources and their risks
| Source | Typical risk |
|---|---|
| Database export | Columns filled in after the outcome occurred |
| Logs and events | Duplicates, missing periods, changing formats |
| Manual labeling | Inconsistent labels between annotators |
| Third-party files | Unclear definitions, undocumented changes |
Record where each file came from, when you pulled it, and what each column means. A short data dictionary saves hours later.
Inspecting a dataset
Start with structure, not statistics. Check the size, the column types, the missing counts, and the target distribution.
from sklearn.datasets import load_breast_cancer
data = load_breast_cancer(as_frame=True)
df = data.frame
print("shape:", df.shape)
print(df.dtypes.value_counts())
print("missing values:", int(df.isna().sum().sum()))
print("duplicate rows:", int(df.duplicated().sum()))
print(df["target"].value_counts(normalize=True))
print(df.describe().T[["min", "mean", "max"]].head())The output shows 569 rows, 31 numeric columns, no missing values, and a target that is about 63% one class. That class balance matters later, when you choose metrics and a splitting strategy.
What to look for
- Wrong types. Numbers stored as text, or dates stored as strings.
- Impossible values. Negative ages, or a date in the future.
- Duplicates. Repeated rows can end up in both the training and test sets.
- Unbalanced targets. A rare positive class changes which metrics are useful.
- Time order. If rows span months, a random split may not match how the model will be used.
Common mistakes
- Starting to model before anyone can say what a row means.
- Trusting column names without reading the documentation.
- Dropping rows with missing values before asking why they are missing.
- Looking at the test data repeatedly. Understanding the data is fine. Tuning decisions based on test rows are not.
Exercise
A dataset for predicting loan default has a column "collections_status" that is set only after a borrower misses payments. Should you use it as a feature? Explain which of the four questions it fails.
Show answer
No. It fails the feature availability question. At the moment a loan is approved, the status does not exist yet. It is set because of the outcome, so a model using it would look accurate in testing and be unusable when making real predictions.