# One hot encoding vs dummy variables best practices for explainable AI (XAI)

When creating artificial columns for your categorical variables there are two mainstream methods you could use:

Disclaimer: For this example, I use the following definitions of dummy variables and one-hot-encoding. I'm aware both methods can be used to either return n or n-1 columns.
Dummy variables: each category is converted to it's own column and the value 0 or 1 indicates if that category is present for each record
one-hot-encoding: similar to dummy variables, but one column is dropped, as its value can be derived from the other columns. This is to prevent multicollinearity and the dummy variable trap.

As an arbitrary example, let's take people's favorite color: pink, blue and green. For a person who's favorite color is pink, the dummy and one-hot-encoded data would look as follows:

dummy variables

person_id favorite_color_pink favorite_color_blue favorite_color_green
xyz 1 0 0

one-hot-encoded variables

person_id favorite_color_blue favorite_color_green
xyz 0 0

From a statistics point of view, I would use the one-hot encoded columns to build my model. In addition, I can infer the favorite color is pink, because I encoded the variables.
However, when I'm applying XAI to explain the prediction to someone else and they see the favorite color wasn't blue or green. I'm not so sure they will infer the favorite color was pink unless it's explicitly stated. So using dummy variables might serve explainability better, but brings other risks..

Are there any best practices on this?

Personally I would chose one hot encoding as it is statistically correct/model friendly. Moreover, you can always provide additional help/tools to aid explainability. Lastly even if you add the nth column, you still need some idea about the working of model(and the boundaries it created while training) to interpret the result.

Pasting an answer here from a colleague, credits go to Amelie Groud

"we encountered the same issue during our PoC, not only with encoding but also with normalization (seeing "age=0.18" wasn't super meaningful).

If you are using model-agnostic techniques (such as Lime, PDP or ANCHOR), you should be able to apply XAI on your original data, before applying the encoding. With these techniques, usually we need 2 main elements: 1. an input dataset and 2. a "predict()" function (calling your trained model). From there you have 2 possibilities:

• use your already transformed data and the predict function of your trained model (which seems to be what you are describing)
• or, use the original, unchanged data and have a custom predict function which first apply the transformation needed (encoding, normalization, etc.) and then call the predict() function of your trained model.

If you choose the 2nd option, then you are free to choose any encoding strategy you see fit. Plus, most XAI techniques have some embedded capacity to deal with categorical data so instead of seeing "pink=1, blue=0, green=0", you will see "color=pink".

Here is an example with LIME but it works similarly for other XAI techniques: https://marcotcr.github.io/lime/tutorials/Tutorial%20-%20continuous%20and%20categorical%20features.html with the predict function defined as predict_fn = lambda x: rf.predict_proba(encoder.transform(x))"

Please feel free to add more answers/views if you have a different way of dealing with the question at hand

Regards, Koen