Problems / Sparse to Dense Feature Matrix / Editorial
users CROSS JOIN features
feature_usage
user_id
feature
uses = NULL
COALESCE(fu.uses, 0)
0
In pandas the same three steps are users.merge(features, how="cross"), then .merge(usage, on=["user_id", "feature"], how="left"), then fillna(0).
users.merge(features, how="cross")
.merge(usage, on=["user_id", "feature"], how="left")
fillna(0)
Sparse storage encodes zeros as *absence*, and absence cannot be aggregated back into existence — any query that starts FROM feature_usage has already lost user 5 (who used nothing) and the sso feature (which nobody used). Densification therefore always runs in one direction: dimensions first, facts second — enumerate the universe with a CROSS JOIN, then LEFT JOIN the facts on the full composite key. Joining on only one of the two keys is the classic slip: the join fans out and quietly duplicates usage counts across the grid.
FROM feature_usage
sso
Solve Sparse to Dense Feature Matrix yourself →