Learning sparsity patterns and constraints from data improves prediction accuracy in out-of-distribution scenarios.