Skip to content
tezvyn:

How would feature engineering for categoricals differ for logistic regression versus LightGBM?

Source: machinelearningmastery.comMediumHow cards are made

How would feature engineering for categoricals differ for logistic regression versus LightGBM?

It tests model-specific encoding decisions. Logistic regression needs one-hot to avoid false ordinality; tree models like LightGBM use ordinal encoding since splits rely on thresholds, not distance.

What's really being asked

This question probes whether you understand that preprocessing is not model-agnostic. Specifically it checks if you know why linear models like logistic regression are sensitive to the numeric representation of categories while tree-based models like LightGBM are not. The interviewer wants to see that you connect algorithm mechanics to feature engineering decisions rather than applying a single recipe everywhere.

The full answer

First state that logistic regression is a linear model that assumes a continuous input space and learns weights implying direction and magnitude. Therefore it requires one-hot encoding for nominal variables so the model does not impose a false order or distance between categories. For ordinal variables integer encoding is acceptable if the order is real and roughly linear. Second explain that tree-based models split on thresholded feature values and do not compute dot products. This means they can handle ordinally encoded nominal features without assuming linear distance because the tree simply finds optimal cut points. Third mention that one-hot encoding for tree-based models can hurt performance by creating sparse features and deeper trees. Fourth note that high-cardinality features may push you toward target encoding or embeddings for either model type but the default split remains one-hot for logistic regression and ordinal for LightGBM.

The mistakes people make

A major red flag is saying both models should use one-hot encoding. This reveals a lack of understanding about how trees handle ordinality and ignores the sparsity and memory costs one-hot imposes on tree learners. Another red flag is claiming that LightGBM always requires label encoding because it handles categories automatically. You should still discuss whether the category order is meaningful and whether native categorical support or ordinal encoding is being used. Saying that encoding never matters because the model will learn anyway is also incorrect especially for logistic regression where ordinality directly corrupts the weight interpretation.

What usually comes next

The interviewer may ask how you would handle a categorical feature with ten thousand unique values. They might also ask what you would do if the categorical variable has a true hierarchy but nonlinear effects or how you would validate that your encoding choice actually improved performance. Be ready to discuss target encoding embeddings or hash encoding as alternatives when dimensionality explodes.

A concrete example

Imagine a customer churn dataset with a nominal feature called neighborhood containing fifty distinct values. For logistic regression you would one-hot encode this into fifty binary columns so the model can learn independent weights for each area without assuming neighborhood A is closer to neighborhood B. For LightGBM you could ordinally encode neighborhood into integers one through fifty. The tree would evaluate splits like neighborhood less than or equal to twelve versus greater than twelve grouping similar churn rates without ever assuming that twelve is numerically farther from thirteen than from eleven. If you one-hot encoded for LightGBM you would create fifty sparse columns forcing the tree to evaluate many shallow splits and increasing training time without accuracy gains.

Interview question

When encoding a nominal categorical feature for logistic regression versus LightGBM, what is the key mechanical difference driving the choice?

  • a.Logistic regression learns weights that imply direction and magnitude across a continuous space, so one-hot encoding is needed to avoid false ordinality.Correct
  • b.Tree models assume linear relationships between ordinally encoded values, making them unsuitable for nominal data.
  • c.LightGBM requires integer inputs to build histograms, so ordinal encoding is mandatory for all categorical features.
  • d.One-hot encoding is avoided in LightGBM solely because it increases memory usage, not because of model mechanics.
Why?

Logistic regression computes dot products in a continuous input space where numeric distance between codes would falsely imply order and magnitude, making one-hot encoding necessary for nominal variables. Option C is a common red flag because LightGBM does not mandate ordinal encoding for every categorical feature and also offers native categorical support.

Just read this? Test yourself on what you have been reading.

Read the original → machinelearningmastery.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles