# How to handle One Hot Encoded columns with changing categories in supervised ML Problem?

Scenario:

I have the following game data about participants, game and the winner in the following format:

id  game        player  winner
-----------------------------------
1   Monopoly    Paul    Paul
1   Monopoly    Frank   Paul
1   Monopoly    Sandra  Paul
2   Monopoly    Peter   Katie
2   Monopoly    Paul    Katie
2   Monopoly    Erwin   Katie
2   Monopoly    Katie   Katie


So the table above shows information about two sessions of monopoly that have been played, the first one with Frank, Paul (Winner) and Sandra; The second one with Peter, Paul, Erwin and Katie (Winner).

My question is: If I want to predict the Winner of a game, how would I transform the data so that each row corresponds to one session?

Idea 1

My initial idea is of course to one hot encode the "player" feature:

id  game        Paul    Frank   Sandra  Peter   Erwin   Katie   winner
-------------------------------------------------------------------------------
1   Monopoly    1       1       1       0       0       0       Paul
2   Monopoly    1       0       0       1       1       1       Katie


That way, it would be easy to predict the target with a simple model.

However, if I want to predict a winner of a new monopoly session with players Sandra, Erwin, Katie and Tim (New Player), the transformed (i.e. one hot encoded) data would have one more column. This column is unknown to the model and thus cannot be processed.

Idea 2

Take Idea 1, add a "new player" binary dummy column (the new player name does not contain any useful information anyway), store the winner, retrain the model with the new (additional) information. However, if I play with multiple new players, this would not work (unless I add multiple dummy columns, which would be quite inflexible).

Idea 3

Monopoly can be played with 2 to 8 players, so I could change the table structure to the following:

id  game        Pl_0    Pl_1    Pl_2    Pl_3    Pl_4    Pl_5    Pl_6    Pl_7    winner
---------------------------------------------------------------------------------------
1   Monopoly    Paul    Frank   Sandra  None    None    None    None    None    Paul
2   Monopoly    Peter   Paul    Erwin   Katie   None    None    None    None    Katie


However, the problem with this approach is of course the structure assumes that the is a semantic meaning behind which player takes which "seat": In game 1, Paul was Player 0 (i.e. the value of Pl_0 is "Paul") but in game 2, Paul was Player 1 (i.e. the value of Pl_1 is "Paul"). In this structure, the model cannot know that it makes no (semantic) difference, whether Paul is Player 1 or 2. Furthermore, a game that can be played with 10 players cannot be mapped like that. So rejected that idea pretty quickly. Or is there any way to overcome this problem?

Final Question

How would I be able to predict the winner of a game, such that

• the model can handle new (unseen) players
• the model can handle multiple games with different numbers of players

The main idea of Machine Learning is that your training data is similar to test data. If that hypothesis does not hold, then big chances of failure are guaranteed.

Given that, you still can handle unseen categories even thought the solution is not good and more with One Hot Encoding

In the category encoders library you have the following hyperparameter with the following options

handle_unknown: str options are ‘error’, ‘return_nan’, ‘value’, and ‘indicator’. The default is ‘value’. Warning: if indicator is used, an extra column will be added in if the transform matrix has unknown categories. This can cause unexpected changes in dimension in some cases.

But, given your problem and your data I am not sure if you will ever be much more successful than doing some descriptive statistics.

• this may be true if you examine fixed data sets and peform the classical ml modelling steps. But in a productive scenario, where the model is applied to unlabeled data to make predictions (as my scenario in the question), it is perfectly normal that new categories may arise. In that case, the system would need to be robust enough to handle the situation (e.g. like you proposed it).
– bk_
Nov 23 '20 at 7:54