Search code examples
pythonencodingscikit-learnlabel-encoding

How do I find the categorical name corresponding to the encoded values ​after fit_transform() in Python?


I'm doing label encoding with LabelEncoder(). I want to know what is the categorical name corresponding to the encoded values. For example:

import pandas as pd 
from sklearn.preprocessing import LabelEncoder
from sklearn import preprocessing

le = LabelEncoder()
data = [['tom', 10], ['nick', 15], ['juli', 14]]
df = pd.DataFrame(data, columns = ['Name', 'Age'])
df['Name']= le.fit_transform(df['Name'])
df

When i use this, code works successfully. But how do i know encoded tom=2 or nick=1? This is a really big problem when working with big data. How do i save these encoded categorical informations?

Thank you for your answer in advance.


Solution

  • Use LabelEncoder.inverse_transform

    le.inverse_transform([1])
    array(['nick'], dtype=object)