Help with type of ML problem: when training data is spread across different subgroups/categories
I'll try to rephrase your question: How does one use the information contained in some categorical feature x to predict y in the presence of unseen category values in the test set?
Assuming the train set is representative of the test set distribution, you'd expect the large categories to also be present in the test set.
We're thus mostly concerned with the small categories which might be present in the train set but absent from the test set and/or present in the test set but absent from the train set.
One way of dealing with such situation is merging small categories (e.g. under 2% of all observations) into a single category. That way you treat any new category level as part of the merged category.
Below I share how I implement the above in python, in a way that can be combined in scikit learn pipelines:
from collections import Counter
import pandas as pd

class mergeSmallCategoryLevels():
    def __init__(self, min_frac):
        self.min_frac = min_frac
        
    def fit(self, X, y=None, **fit_params):
        category_counts = pd.DataFrame.from_dict(Counter(X), orient = &quot;index&quot;, columns = [&quot;count&quot;])
        min_category_count = len(X)*self.min_frac
        large_categories = category_counts[category_counts[&quot;count&quot;] &gt;= min_category_count]
        self.large_categories = list(large_categories.index.values)
        return self

    def transform(self, X, **transform_params):
        ans = [val if val in self.large_categories else &quot;.merged&quot; for val in X]
        ans = pd.DataFrame({&quot;category_feature&quot;:ans})
        return ans.to_numpy().reshape(-1, 1)