Any Way To Get Mappings Of A Label Encoder In Python Pandas?
Answer :
You can create additional dictionary with mapping:
from sklearn import preprocessing le = preprocessing.LabelEncoder() le.fit(data['name']) le_name_mapping = dict(zip(le.classes_, le.transform(le.classes_))) print(le_name_mapping) {'Tom': 0, 'Nick': 1, 'Kate': 2}
The best way of doing this can be to use label encoder of sklearn library.
Something like this:
from sklearn import preprocessing le = preprocessing.LabelEncoder() le.fit(["paris", "paris", "tokyo", "amsterdam"]) list(le.classes_) le.transform(["tokyo", "tokyo", "paris"]) list(le.inverse_transform([2, 2, 1]))
A simple & elegant way to do the same.
cat_list = ['Sun', 'Sun', 'Wed', 'Mon', 'Mon'] encoded_data, mapping_index = pd.Series(cat_list).factorize()
and you are done, check below
print(encoded_data) print(mapping_index) print(mapping_index.get_loc("Mon"))
Comments
Post a Comment