Reverse Label Encoding giving error

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

I label encoded my categorical data into numerical data using label encoder

data['Resi'] = LabelEncoder().fit_transform(data['Resi'])

But I when I try to find how they are mapped internally using

list(LabelEncoder.inverse_transform(data['Resi']))

I am getting below error


TypeError                                 Traceback (most recent call last) <ipython-input-67-419ab6db89e2> in <module>() ----> 1 list(LabelEncoder.inverse_transform(data['Resi']))  TypeError: inverse_transform() missing 1 required positional argument: 'y'

How to fix this

Sample data

Resi IP IP IP IP IP IE IP IP IP IP IP IPD IE IE IP IE IP IP IP

回答1:

You can check label encoding:

>>> from sklearn import preprocessing >>> le = preprocessing.LabelEncoder() >>> le.fit([1, 2, 2, 6]) LabelEncoder() >>> le.classes_ array([1, 2, 6]) >>> le.transform([1, 1, 2, 6]) array([0, 0, 1, 2]) >>> le.inverse_transform([0, 0, 1, 2]) array([1, 1, 2, 6])

And for your solution:

from sklearn.preprocessing import LabelEncoder  le = LabelEncoder().fit(data['Resi']) data['Resi'] = le.transform(data['Resi']) print (data.tail())     Resi 14     1 15     0 16     1 17     1 18     1  L = list(le.inverse_transform(data['Resi'])) print (L) ['IP', 'IP', 'IP', 'IP', 'IP', 'IE', 'IP', 'IP', 'IP',   'IP', 'IP', 'IPD', 'IE', 'IE', 'IP', 'IE', 'IP', 'IP', 'IP']

EDIT:

d = dict(zip(le.classes_, le.transform(le.classes_))) print (d) {'IE': 0, 'IPD': 2, 'IP': 1}


回答2:

You are not storing the LabelEncoder() object anywhere. You need to save it like this:

le = LabelEncoder()

And then call fit(), or transform().

import pandas as pd import numpy as np from sklearn.preprocessing import LabelEncoder  ls = ['IP', 'IP', 'IP', 'IP', 'IP', 'IE', 'IP', 'IP', 'IP', 'IP', 'IP', 'IPD', 'IE', 'IE', 'IP', 'IE', 'IP', 'IP', 'IP']  data = pd.DataFrame(np.array(ls).reshape(-1,1), columns=['Resi'])  le = LabelEncoder() data['Resi'] = le.fit_transform(data['Resi'])  df['resi'] = LabelEncoder().fit_transform(df['resi']) list(le.inverse_transform(data['Resi']))  Out:  ['IP',  'IP',  'IP',  'IP',  'IP',  'IE',  'IP',  'IP',  'IP',  'IP',  'IP',  'IPD',  'IE',  'IE',  'IP',  'IE',  'IP',  'IP',  'IP']


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