devnotes.

Feature Interactions: Facet Grids and Pair Plots

Feature Interactions: Facet Grids and Pair Plots

When there are many features, the relationship between two of them may change depending on a third. Exploring these interactions uncovers patterns that single-variable views miss.

1. Facet grids

A facet grid splits the data into several subplots by a categorical feature, so each subset is shown side by side with the same axes.

# Facet grid: one histogram of alcohol per quality level
g = sns.FacetGrid(df, col="quality", col_wrap=3, height=3)
g.map(sns.histplot, "alcohol", kde=True)
plt.show()

# Facet grid with a scatter relationship
g = sns.FacetGrid(df, col="quality", col_wrap=3, height=3)
g.map(sns.scatterplot, "alcohol", "density", s=15)
plt.show()
  • Best for: comparing a relationship across categories, e.g. sales versus advertising for each region or season.
  • Not suitable: too many categories - the grid becomes cluttered and small.
  • Example: how product sales differ across seasons. Each facet shows one season, making seasonal trends obvious.

Useful options:

OptionMeaning
col= / row=which variable splits columns / rows
col_wrap=3wrap facets after 3 per row
hue=additional colour split inside each facet
sharex / shareyuse the same axes (fair comparison) or free axes (see each shape)

Modern seaborn also offers sns.displot(..., col="quality") and sns.relplot(..., col="quality") which create facet grids in one line.

2. Pair plots

A pair plot creates a grid of scatter plots for every pair of variables, with distributions on the diagonal.

cols = ["alcohol", "sulphates", "density", "volatile acidity", "quality"]
sns.pairplot(df[cols], hue="quality", palette="viridis", diag_kind="kde", corner=True)
plt.show()
  • Best for: examining relationships among several continuous variables - correlations, trends, clusters.
  • Not suitable: many variables - the number of pairs grows fast (n variables give n x (n-1) / 2 unique pairs), so the figure becomes overwhelming and slow.
  • Example: how price, customer age and purchase frequency relate in an e-commerce dataset.

corner=True draws only the lower triangle, removing mirrored duplicates.

How to read a pair plot

  1. Diagonal: each variable's histogram or KDE - shape, skew, peaks.
  2. Off-diagonal: scatter plot of the pair. Straight upward or downward band = correlation; round cloud = none.
  3. Colours: separated colour groups mean the pair helps distinguish classes; overlapping colours mean it does not.
  4. Isolated dots: potential outliers.

A worked reading

Suppose alcohol vs density shows a downward band: higher-alcohol wines are less dense (alcohol is lighter than water). Colouring by quality shows high-quality points concentrated at high alcohol. Conclusion: alcohol carries useful information about quality, and density partly repeats that information (they are correlated), so a model might not need both.

Other interaction tools

# Interaction via grouped statistics
df.pivot_table(values="quality", index="alcohol_level", columns=pd.cut(df["sulphates"], 3),
               aggfunc="mean")

# Heatmap of that table
sns.heatmap(
    df.pivot_table(values="quality", index="alcohol_level",
                   columns=pd.cut(df["sulphates"], 3), aggfunc="mean"),
    annot=True, cmap="YlGnBu")
plt.show()

If the pattern of the table is not additive (for example, sulphates helps only when alcohol is high), you have found an interaction worth encoding as a feature.

Feature Interactions: Facet Grids and Pair Plots | Exploratory Data Analysis | DevNotes