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
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}
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With