I have a regression problem. Two input features describe a category and subcategory. For illustrative explanation, let's consider we speak about city and district.

Some more details about the regression problem: 39000 observations, 104 cities, each 1-17 districts. 5 additional demographical variables (age, salary, gender, marital status, education level). Trying to predict the number of children of a given person (from given city, district, age, salary, ....). In some districts, we have only 1 record

The question: Is there any specific method how to represent the nested categories for machine learning?

Important comments:

  • Plain application of one-hot encoder to city-district pairs will not work as lots of combinations are very rare in the data.
  • Still, not willing to ignore the information about districts completely.
  • If doing just logistic regression, a hierarchical Bayesian model could help. However, what about xgboost or neural networks?

Non-specific attempts so far:

  • To do one-hot encoding for both higher (city) and lower hierarchies (city-district) and then apply standard feature selection methods.
  • To combine the above with explicit filtration of very rare combinations (say at least 5 examples).
  • 1
    $\begingroup$ Can you tell us some more details? Many cities with many districts within each one? You could look at multilevel models, maybe mixed models. $\endgroup$ Sep 29, 2020 at 17:50
  • $\begingroup$ Yes, many cities, each with multiple districts. The district is specified by city name and district name. $\endgroup$ Sep 29, 2020 at 19:18
  • $\begingroup$ Added. How to combine mixed effects with a neural network? $\endgroup$ Sep 30, 2020 at 14:39
  • $\begingroup$ Have a look at this paper? aclanthology.org/2020.ecnlp-1.10.pdf $\endgroup$ Aug 18, 2023 at 2:46

1 Answer 1


As a start I would go for a random effects model with districts nested within cities, and then for each district a random intercept. Then the few districts with only one or very few observations should not be a problem. With 39000 observations and only a few covariates I would just use them all, avoid feature selection, see for instance Why is variable selection necessary?. Spline variables such as salary and age, and use relevant interactions!

You seem to want some flexible model, like a neural network? I'm not sure what much sense that make with 39000 observations, start first with flexible linear mixed models and evaluate the results. But there is an R package Buddle for neural networks with random effecs, and google gives a lot of hits.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.