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

假装没事ソ 提交于 2019-12-20 16:56:51

问题


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?


回答1:


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]))



回答2:


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}



回答3:


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"))



回答4:


First, make a categorical series:

weekdays = pd.Series(['Sun', 'Sun', 'Wed', 'Mon', 'Mon']).astype('category')

Then, inspect its "categories":

weekdays.cat.categories.get_loc('Sun')



回答5:


There are many ways of doing this. You can consider pd.factorize, sklearn.preprocessing.LabelEncoder etc. However, in this specific case, you have two options which will suit you best:

Going by your own method, you can add the categories:

pd.Categorical( df.weekday, [ 
    'Sunday', 'Monday', 'Tuesday', 
    'Wednesday', 'Thursday', 'Friday', 
    'Saturday']  ).labels

The other option is to map values directly using a dict

df.weekday.map({
    'Sunday': 0,
    'Monday': 1,
     # ... and so on. You get the idea ...
})



回答6:


If you have numerical and categorical both type of data in dataframe You can use : here X is my dataframe having categorical and numerical both variables

from sklearn import preprocessing
le = preprocessing.LabelEncoder()

for i in range(0,X.shape[1]):
    if X.dtypes[i]=='object':
        X[X.columns[i]] = le.fit_transform(X[X.columns[i]])

Or you can try this:

from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
data = data.apply(le.fit_transform)

Note: This technique is good if you are not interested in converting them back.



来源:https://stackoverflow.com/questions/42196589/any-way-to-get-mappings-of-a-label-encoder-in-python-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!