Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to get mappings of a label encoder in Python pandas?

I am converting strings to categorical values in my dataset using the following piece of code.

data['weekday'] = pd.Categorical.from_array(data.weekday).labels  

For eg,

index    weekday 0        Sunday 1        Sunday 2        Wednesday 3        Monday 4        Monday 5        Thursday 6        Tuesday 

After encoding the weekday, my dataset appears like this:

index    weekday     0       3     1       3     2       6     3       1     4       1     5       4     6       5 

Is there any way I can know that Sunday has been mapped to 3, Wednesday to 6 and so on?

like image 531
Gingerbread Avatar asked Feb 13 '17 04:02

Gingerbread


People also ask

What is LabelEncoder () in Python?

Label Encoder: Label Encoding in Python can be achieved using Sklearn Library. Sklearn provides a very efficient tool for encoding the levels of categorical features into numeric values. LabelEncoder encode labels with a value between 0 and n_classes-1 where n is the number of distinct labels.

Is label encoder ordinal?

An ordinal encoding involves mapping each unique label to an integer value. This type of encoding is really only appropriate if there is a known relationship between the categories. This relationship does exist for some of the variables in our dataset, and ideally, this should be harnessed when preparing the data.

How is label encoding done?

Label Encoding is a popular encoding technique for handling categorical variables. In this technique, each label is assigned a unique integer based on alphabetical ordering.


1 Answers

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} 
like image 194
chinskiy Avatar answered Sep 23 '22 00:09

chinskiy