反向标签编码给出错误

时间:2017-11-02 05:52:41

标签: python pandas numpy scikit-learn sklearn-pandas

我使用标签编码器

将我的分类数据标记为数字数据
data['Resi'] = LabelEncoder().fit_transform(data['Resi'])

但是当我尝试使用

查找内部映射方式时
list(LabelEncoder.inverse_transform(data['Resi']))

我收到以下错误

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'

如何解决此问题

示例数据

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

3 个答案:

答案 0 :(得分:2)

您可以查看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])

并为您的解决方案:

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

编辑:

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

答案 1 :(得分:1)

您没有将LabelEncoder()对象存储在任何位置。你需要像这样保存它:

le = LabelEncoder()

然后拨打fit()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']

答案 2 :(得分:0)

encoder = LabelEncoder()  
encoder.inverse_transform(data['Resi'])