可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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']