Create Audience Segments Using K-Means Clustering in Python

Foursquare1 Mar 2023

Problem

Marketers want to describe an audience as a few groups with shared behavior and reach each group with its own message. Raw user tables mix visit counts, distances, dwell times and demographics on different scales, and with many features, distances between points stop being informative.

Example from the article: Foursquare took about 100K users of its U.S. mobile location panel and 16 holiday-season features (store types visited, visits per user per active day, distance traveled, dwell time, time patterns, predicted age and gender, income, population density) to find holiday shopping segments for advertisers.

Solution in brief

Bucket each feature into quantiles, standardize, compress with PCA to the components that keep over 80% of variance, then run K-means with a chosen K. Join the cluster labels back to the original features to describe and name each segment.

Questions this case answers
  • How to group users into a small number of actionable segments from a dozen mixed behavioral and demographic features when features have different scales and no labels exist
  • How to split a consumer base into behavioral segments that marketers can target?
  • How to prepare mixed behavioral and demographic features for K-means?
  • How to choose the number of PCA components before clustering?

Tools and methods

  • K-means (scikit-learn) — assigns each user to one of K clusters by distance to the nearest centroid.
  • Quantile bucketing (pandas qcut) and StandardScaler — put all features on one scale.
  • PCA — reduces 16 features to 10 components to limit the curse of dimensionality.
  • Matplotlib, seaborn — explained-variance and 3D cluster plots.
Continue reading — it's free

Google or a one-time email code. No password.