Sub topic 2
What makes tabular data different
This chapter assumes you know basic pandas, the supervised learning vocabulary (features, labels, train and test splits), and scikit-learn's fit and predict interface. It describes the properties of rows-and-columns data that later choices depend on, such as preprocessing, validation, and model family.
Columns are not interchangeable
In an image, every pixel has the same type and meaning, and neighboring pixels are related. In text, tokens come from one vocabulary and their order matters. A table has none of this. Each column is its own variable with its own type, unit, and distribution.
| Property | Image or text | Tabular |
|---|---|---|
| Feature types | Uniform | Mixed: numeric, categorical, boolean, dates |
| Neighbor structure | Strong (adjacent pixels, word order) | Usually none, and column order is arbitrary |
| Scale | Comparable | Wildly different (age in tens, income in tens of thousands) |
| Typical size | Large | Often hundreds to a few hundred thousand rows |
Because column order carries no meaning, models that assume local structure, such as convolutions, have nothing to exploit here.
Properties that affect model choice
- Mixed types. Categorical columns must be encoded before most models can use them. A column with 3 values and one with 50,000 values (such as a user ID) need different treatment.
- Missing values. Missingness is common and is sometimes informative. For example, a blank income field may correlate with the label. Some models handle
NaNnatively, and others need imputation. - Different scales. Linear models, neural networks, and distance-based methods are sensitive to scale. Tree-based models are not, because they only compare a feature to a threshold.
- Irregular target functions. The label often changes sharply at a threshold (for example, "over 65" or "balance below zero") and depends on interactions between columns. Trees represent such steps and interactions easily. Smooth models need extra features or more capacity.
- Uninformative features. Tables often contain many columns that do not matter, such as IDs and near-duplicates. Models vary in how well they ignore them.
- Modest row counts. With few rows, high-capacity models overfit, and validation estimates become noisy.
- Hidden time and group structure. Rows are not always independent. Several rows may belong to one customer, or the data may be ordered in time. Chapter 4 covers why this matters for validation.
Profile before you model
A quick profile shows which of these properties your data has. The script below builds the same small table used in later chapters and summarizes it.
import numpy as np
import pandas as pd
rng = np.random.default_rng(0)
n = 500
X = pd.DataFrame({
"age": rng.integers(18, 80, n).astype(float),
"income": rng.normal(50000, 15000, n),
"city": rng.choice(["a", "b", "c"], n),
})
X.loc[rng.choice(n, 25, replace=False), "income"] = np.nan
profile = pd.DataFrame({
"dtype": X.dtypes.astype(str),
"missing": X.isna().mean().round(3),
"unique": X.nunique(),
"min": X.min(numeric_only=True),
"max": X.max(numeric_only=True),
})
print(profile)The output shows 5% of income is missing, city has three categories, and income spans a range thousands of times wider than age. Each finding maps to a step in the pipeline you will build next.
Common errors
- Treating integer-coded categories (such as a
zip_codestored as a number) as numeric, which imposes a false order. - Leaving high-cardinality identifiers in the features, which lets flexible models memorize rows.
- Assuming that a method that wins on images or text will also win on tables.
Exercise
A table has a numeric column balance ranging from -5,000 to 2,000,000 and a categorical column region with 4 values. You must choose between logistic regression and a random forest, and you do no other preprocessing. Which model is more sensitive to the scale of balance, and why?
Show answer
Logistic regression. Its coefficients and optimization depend on feature magnitudes, so a very large range can slow convergence and distort regularization. A random forest only compares balance to thresholds, so rescaling does not change its splits. (Both models still need region encoded, at least in scikit-learn's standard estimators.)