A linear model on one-hot encoded categorical inputs captures each variable's main effect but no interaction. A feature cross adds one indicator per combination of levels of two variables, so the model can learn, say, that trucks from the USA are less fuel efficient than trucks in general and USA cars in general would suggest.
Implement cross_features(X, levels, pairs):
X: a list of rows; row n holds one categorical value per feature.levels: levels[f] is the ordered list of known values of feature f.pairs: a list of feature-index pairs (i, j) to cross.Return (Phi, names), where Phi is a float array of shape (N, K) and names is the list of the K column names, with columns in this order:
"1".f in order, one indicator per level in levels[f] order, named "f=v" (for example "0=SUV").(i, j) in order, one indicator per level combination, with the level of feature i varying fastest: for levels of and of . Name each column "i=u&j=v".A value that does not appear in levels[f] (an unseen category) gets all-zero indicators for that feature and for every cross that involves it.