Say I have a list as given below:
l = [
['PER', 'O', 'O', 'GEO'],
['ORG', 'O', 'O', 'O'],
['O', 'O', 'O', 'GEO'],
['O', 'O', 'PER', 'O']
]
I want to encode the 2D list with LabelEncoder().
It should look something like:
l = [
[1, 0, 0, 2],
[3, 0, 0, 0],
[0, 0, 0, 2],
[0, 0, 1, 0]
]
Is it possible? If not, is there any workaround?
Thanks in advance!
You can flatten the list, fit the encoder with all the potential values and then use the encoder to transform each sublist, as shown below: