Handling Missing Categorical Features in ML

PMLE By islever Editorial Last updated

Exploring and preprocessing data for ML.

Short answer

D Add a new class for missing values and create a binary indicator feature to preserve predictive signal and capture missingness patterns.

Question

While conducting an exploratory analysis of a dataset, you discover that categorical feature A has substantial predictive power, but it is sometimes missing. What should you do?
  1. Drop feature A if more than 15% of values are missing. Otherwise, use feature A as-is.
  2. Compute the mode of feature A and then use it to replace the missing values in feature A.
  3. Replace the missing values with the values of the feature with the highest Pearson correlation with feature A.
  4. Add an additional class to categorical feature A for missing values. Create a new binary feature that indicates whether feature A is missing. Correct Answer

Key Takeaways

The main trap is choosing simple imputation (like mode) which distorts distributions, or dropping the feature entirely. The key insight is that missingness itself is often informative.

Learn why treating missing values as a distinct category and creating an indicator variable is the optimal strategy for highly predictive categorical features with missing data in Google Cloud ML exams.

Common mistake: Choosing option B (mode imputation) is common but wrong because it assumes missingness is random and can bias the model by over-representing the most frequent value, losing the signal that data was absent.

Expert Analysis

When handling missing data in categorical features, especially when the feature has high predictive power, the goal is to preserve information while avoiding bias. Option D is the correct approach because it treats 'missingness' as a distinct category (e.g., 'Unknown') and creates an indicator variable. This allows the model to learn if the absence of data itself is predictive.

Option A is incorrect because dropping a feature with substantial predictive power based on an arbitrary threshold (15%) discards valuable signal. Option B is risky; imputing with the mode can distort the distribution and introduce bias, particularly if the missingness is not random. Option C is flawed because correlating features does not address the structural issue of missing categorical levels and may introduce noise.

Community comments heavily favor D, noting that preserving the 'missing' signal is crucial. One comment highlights that adding a binary feature explicitly tells the model to pay attention to the absence of data. Another notes that inventing data (imputation) doesn't help prediction objectives. While some argue for B, the consensus and best practice for categorical variables with significant missingness are to treat missing as a class.

Exam Strategy

Always evaluate whether missingness carries semantic meaning. For categorical features with high predictive power, never drop them arbitrarily. Instead, look for solutions that encode the 'missing' state as a valid input class or separate signal, rather than replacing it with a statistical estimate that might obscure the pattern.

Frequently Asked Questions

Why not just drop rows with missing values?

Dropping rows reduces dataset size and may introduce selection bias. If the feature is highly predictive, retaining it via encoding is superior.

Is mode imputation ever acceptable?

Mode imputation is generally discouraged for categorical variables with significant missingness as it distorts distributions. It is only acceptable if missingness is completely random and negligible.

More PMLE FAQ →

Community Votes

D
59%
B
41%

59% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.

Discussion 28 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 28

  1. wish0035 👍 17 Answered D
    ans: D A => no, you don't want to drop a feature with high prediction power. B => i think this could confuse the model... a better solution could be to fill missing values using an algorithm like Expectation Maximization, but using the mode i think is a bad idea in this case, because if you have a significant number of missing values (for example >10%) this would modify the "predictive power". you don't want to lose predictive power of a feature, just guide the model to learn when to use that feature and when to ignore it. C => this doesn't make any sense for me. not sure what i would do that. D => i think this could be a really good approach, and i'm pretty sure it would work pretty well a lot of models. the model would learn that when "is_available_feat_A" == True, then it would use the feature A, but whenever it is missing then it would try to use other features.
  2. hiromi 👍 5 Answered B
    B "For categorical variables, we can usually replace missing values with mean, median, or most frequent values" Dr. Logan Song - Journey to Become a Google Cloud Machine Learning Engineer - Page 48
  3. Vedjha 👍 3
    I will go with 'B' dropping feature is not a solution to handle missing values. We can replace it by mean/mode or by any calculated values. https://www.analyticsvidhya.com/blog/2021/10/handling-missing-value/#:~:text=There%20are%202%20primary%20ways,Imputing%20the%20Missing%20Values
  4. ares81 👍 3
    Mode is the way to go for categorical features. B, for me.
  5. LearnSodas 👍 3 Answered B
    I agree with B
Show 23 more comments
  1. tavva_prudhvi 👍 2
    I think, its D. Option B of imputing the missing values of feature A with the mode of feature A could be a reasonable approach if the mode provides a good representation of the distribution of feature A. However, this method may lead to biased results if the mode is not representative of the missing values. This could be the case if the missing values have a different distribution than the observed values. Similarly, When a categorical feature has substantial predictive power, it is important not to discard it. Instead, missing values can be handled by adding an additional class for missing values and creating a new binary feature that indicates whether feature A is missing or not. This approach ensures that the predictive power of feature A is retained while accounting for missing values. Computing the mode of feature A and replacing missing values may distort the distribution of the feature and create bias in the analysis. Similarly, replacing missing values with values from another feature may introduce noise and lead to incorrect results.
  2. PST21 👍 2
    By creating a new class for the missing values, you explicitly capture the absence of data, which can provide valuable information for predictive modeling. Additionally, creating a binary feature allows the model to distinguish between cases where feature A is present and cases where it is missing, which can be useful for identifying potential patterns or relationships in the data.
  3. frangm23 👍 2
    I guess I would go with D, but it confuses me the fact that in option D, it doesn't say that NaN values are replaced (only that there's a new column added) and this could lead to problems like exploding gradients. Plus, Google encourages to replace missing values. https://developers.google.com/machine-learning/testing-debugging/common/data-errors Any thoughts on this?
  4. MultiCloudIronMan 👍 2 Answered B
    Google encourages filling missing value and using mode is one of the examples given. D only tell the obvious - data is missing!
  5. Pancy 👍 2
    B. Because the important feature is already known. By using mode, contribution of other features will not be missed
  6. Scipione_ 👍 2 Answered D
    Both B and D are possible, but the correct answer is D because of the feature high predictive power.
  7. ichbinnoah 👍 2 Answered B
    Definitely not D, it does not even solve the problem of NA values.
  8. guid1984 👍 1 Answered D
    Preserving the Signal: By adding a new category (e.g., "Unknown" or "Missing"), you allow the model to learn a specific weight for when that feature is unavailable. Indicator Variable: Creating a secondary binary feature (0 for present, 1 for missing) explicitly tells the model to pay attention to the absence of data. This "indicator" helps the model distinguish between a genuine value and a placeholder. Handling Categorical Data: Since Feature A is categorical, you cannot use mathematical averages. Simply assigning it a new class name is the cleanest way to keep all your data rows without losing the predictive power of the samples that do have the feature.
  9. taylz876 👍 1 Answered D
    Feature A is strongly predictive but sometimes missing, so you want to keep it and not lose the information. D does two things: 1. It adds a "missing" category to feature A, so rows with no value still go into the model in a valid form. 2. It adds a binary "is missing" feature. Often the fact that a value is missing is itself a signal (for example, a customer who didn't fill in a field). That keeps all the rows and all the information, and doesn't invent values that aren't there.
  10. BenMS 👍 1 Answered D
    If our objective was to produce a complete dataset then we might use some average value to fill in the gaps (option B) but in this case we want to predict an outcome, so inventing our own data is not going to help in my view. Option D is the most sensible approach to let the model choose the best features.
  11. Fer660 👍 1 Answered B
    Not A: destroys value. B is correct Not C: obvious nonsense Not D: the 'tell' is that this approach does the same thing twice. If we already added a new category for the missing values, why add a binary feature to show the same thing ? Makes no sense.
  12. gq_dp1 👍 1 Answered B
    It should be B. Missing categorical values can be imputed with the most frequent (mode) value in the column. https://docs.cloud.google.com/vertex-ai/generative-ai/docs/prompt-gallery/samples/code_learn_about_dataset_transformations
  13. b7ad1d9 👍 1 Answered D
    Voted D because that seems to cause less harm than the other good option B (replace with mode). IRL, you would be guided by the domain and the use case. Too little info here!
  14. tavva_prudhvi 👍 1
    While this approach may seem reasonable, it can introduce bias in the dataset by over-representing the mode, especially if the missing values are not missing at random.
  15. andresvelasco 👍 1
    Options B or D But isnt there an inconsistency in option D? if you replace missing values with a new category ("missing") why would you haveto create an extra feature?
  16. amtg 👍 1 Answered B
    By imputing the missing values with the mode (the most frequent value), you retain the original feature's predictive power while handling the missing values
  17. AmitK121981 👍 1 Answered D
    having extra feature of missing flag is good, and hence D. B is close but as this new feature is not added there, it loses out here
  18. OzoneReloaded 👍 1 Answered D
    I'll stick to D because for categorical features it's a nice approach
  19. fragkris 👍 1 Answered D
    B and D are correct, but I decided to go with D.
  20. Liting 👍 1 Answered D
    Agree with wish0035, answer should be D
  21. Mickey321 👍 1 Answered D
    highly predictive
  22. PhilipKoku 👍 1 Answered D
    D) Good approach
  23. M25 👍 1 Answered D
    Went with D

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Practice All PMLE Questions

Access 65 questions with complete answers and detailed explanations.

View Full PMLE Practice Test →

← Back to PMLE Study Guide