Handling of missing values in categorical encoders
Some transformers such as the CountFrequency categorical encoder have options for handling new unseen categories during calls to transform. In some cases, unseen categories and explicit missing values might be getting mixed together however.
It would be good to have some distinction between the two and potentially different options for courses of action that would be consistent across transformers. For example, if CountFrequency encoder receives an explicit missing value during transform, the user might want to do one of the following:
- Propagate those missing values to the output of
transform. - Treat them the same as unseen categories (encoding them with zeros).
- Encode them with the frequency of missing values that were passed to the training data (treating missing values as a separate category).
- Raise an error.
In most of the cases, only the last alternative (raising an error) is supported, but would be ideal to have options and to make explicit in the documentation what happens with missing values.
Hi @david-cortes
Could you give a code example of what you mean?
If you are following up on the modifications you did to the countencoder... unseen categories will be encoded as 0, and nan will remain nan when errors=encode. Alternatively, both unseen and nan will be encoded as nan when errors=ignore. That is for the user to choose.
I don't think we should add treatment of missing values to the encoders. If there are missing data, we have an entire module on imputation to take care of them.
Does this somehow answer the question?
Many transformers actually fail to fit when the data has missing values, even though their presence shouldn't affect the calculations that the transformer does. One might want to preserve missing values as missing in the output before passing them to some further transformer for example (such as a multivariate imputer).
All encoders will fail if the input has missing data. They will fail during fit and they will also fail during transform. This is intentional.
The user can impute missing data beforehand, for example with the CategoricalImputer() and replace nan by a string, for example "Missing", if they want to preserve that as a separate category.
Unseen categories are a different story. They will only be taken care of during transform in 1 of 2 ways for most transformers: either fail or return nan in place of the unseen category. The countfrequency now has the option to return 0.
I am not sure I understand what the issue is.
Maybe you could give a code example?
Example use cases for passing explicit missing values:
- I have a data frame with mixed columns and missing values in many of them. I want to fit a regression model using a software that handles missing values without imputation, such as lightgbm. Some categorical columns have high cardinality and lightgbm doesn't handle them well, so I want to encode them with some transformers.
- I have a data frame with mixed columns and missing values in some columns. I want to fit a linear model, for which I want to impute the missing values. I again have high-cardinality categorical features and I know that I can get better results by using a multivariate imputer with the transformed columns when I impute numerical columns for example.
- I might want to combine the results of the transformations across different columns and fill missing values using those results. If they get imputed beforehand, the end result will be different.
Ok, so if I understand this issue correctly, the idea is that the encoders should not fail when the data has missing values (or at least have the option not to fail). And in addition to have some functionality to handle the missing data in some ways, as below:
- Propagate those missing values to the output of transform.
- Treat them the same as unseen categories (encoding them with zeros).
- Encode them with the frequency of missing values that were passed to the training data (treating missing values as a separate category).
- Raise an error.
My thoughts:
- Can be obtained if using the RareLabelEncoder before the categorical encoders. So I am not sure I want to add this functionality to every single encoder, when we have it centralized already. It is hard to maintain and also over-complicates the classes
- Can be done if using the CategoricalImputer before the categorical encoders and replacing missing values by a specific string, for example "missing". Again, I do not want to add this functionality to every encoder, when we have it centralized in the CategoricalImputer already
- This happens already.
So the only thing missing would be that the encoders accept missing values (point 1 in the list), and they just leave them unmodified after transform.
The idea of Feature-engine transformers is to leave the data ready to train machine learning models. So in this sense, allowing NAN, is not what you want to do. I do see that some models accept NAN, like light GBMs, thus the user may not want to impute them. I guess, I would like to know more about the mechanisms of lighgbm. What is it doing under the hood with the NA? most likely treating them as a separate category, so would 1 be very different from 3?
In the case of lightgbm, when it encounters NaNs in a categorical variable during fitting, at each node that makes a split on that variable, it decides to which branch to assign NaNs by checking on which branch they would decrease the loss function the most, which is something that could not be achieved by using the same imputed value at every tree and node - hence why one would want to pass NaNs as-is instead of imputing them.