Reverse Label Encoding giving error

2024/11/13 12:07:35

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
Answer

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 LabelEncoderle = LabelEncoder().fit(data['Resi'])
data['Resi'] = le.transform(data['Resi'])
print (data.tail())Resi
14     1
15     0
16     1
17     1
18     1L = 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}
https://en.xdnf.cn/q/72301.html

Related Q&A

how to check if a value exists in a dataframe

hi I am trying to get the column name of a dataframe which contains a specific word,eg: i have a dataframe,NA good employee Not available best employer not required well mana…

Do something every time a module is imported

Is there a way to do something (like print "funkymodule imported" for example) every time a module is imported from any other module? Not only the first time its imported to the runtime or r…

Unit Testing Interfaces in Python

I am currently learning python in preperation for a class over the summer and have gotten started by implementing different types of heaps and priority based data structures.I began to write a unit tes…

Python Pandas average based on condition into new column

I have a pandas dataframe containing the following data:matchID server court speed 1 1 A 100 1 2 D 200 1 3 D 300 1 …

Merging same-indexed rows by taking non-NaNs from all of them in pandas dataframe

I have a sparse dataframe with duplicate indices. How can I merge the same-indexed rows in a way that I keep all the non-NaN data from the conflicting rows?I know that you can achieve something very c…

Approximating cos using the Taylor series

Im using the Taylors series to calculate the cos of a number, with small numbers the function returns accurate results for example cos(5) gives 0.28366218546322663. But with larger numbers it returns i…

How to apply max min boundaries to a value without using conditional statements

Problem:Write a Python function, clip(lo, x, hi) that returns lo if x is less than lo; hi if x is greater than hi; and x otherwise. For this problem, you can assume that lo < hi.Dont use any conditi…

pandas to_json() redundant backslashes

I have a .csv file containing data about movies and Im trying to reformat it as a JSON file to use it in MongoDB. So I loaded that csv file to a pandas DataFrame and then used to_json method to write i…

How can I get the old zip() in Python3?

I migrated from Python 2.7 to Python 3.3 and zip() does not work as expected anymore. Indeed, I read in the doc that it now returns an iterator instead of a list.So, how I am supposed to deal with this…

How can I use tensorflow metric function within keras models?

using python 3.5.2 tensorflow rc 1.1Im trying to use a tensorflow metric function in keras. the required function interface seems to be the same, but calling:import pandas import numpy import tensorflo…