Sklearn LabelEncoder inverse transform

In summary, the Sklearn LabelEncoder inverse transform function is used to convert encoded labels back to their original values. It works by taking in a list of encoded labels and using the inverse mapping of the LabelEncoder. It can handle missing values and is reversible, meaning it can convert the original values to encoded labels and back. It can also be applied to multiple columns of data.
  • #1
BRN
108
10
Hi everyone!

I need to inverse an label transform with sklearn. I found this example on web:

example:
from sklearn.preprocessing import LabelEncoder

np.random.seed(1)
y = np.random.randint(0, 2, (10, 7))
y = y[np.where(y.sum(axis=1) != 0)[0]]
output::
array([[1, 1, 0, 0, 1, 1, 1],
       [1, 1, 0, 0, 1, 0, 1],
       [1, 0, 0, 1, 0, 0, 0],
       [1, 0, 0, 1, 0, 0, 0],
       [1, 0, 0, 0, 1, 1, 1],
       [1, 1, 0, 0, 0, 1, 1],
       [1, 1, 1, 1, 0, 1, 1],
       [0, 0, 1, 0, 0, 1, 1],
       [1, 0, 1, 0, 0, 1, 1],
       [0, 1, 1, 1, 1, 0, 0]])

example:
le = LabelEncoder()
y_new = le.fit_transform([''.join(str(l)) for l in y])
output::
array([7, 6, 3, 3, 2, 5, 8, 0, 4, 1])

example:
y_old = le.inverse_transform(y_new)
output::
array(['[1 1 0 0 1 1 1]', '[1 1 0 0 1 0 1]', '[1 0 0 1 0 0 0]',
       '[1 0 0 1 0 0 0]', '[1 0 0 0 1 1 1]', '[1 1 0 0 0 1 1]',
       '[1 1 1 1 0 1 1]', '[0 0 1 0 0 1 1]', '[1 0 1 0 0 1 1]',
       '[0 1 1 1 1 0 0]'], dtype='<U15')

With 'inverse_transform' How can I obtain an array with shape (10, 7)?

Thaks!
 
Technology news on Phys.org
  • #2


Hi there!

To obtain an array with shape (10, 7), you can use the 'reshape' function after applying the 'inverse_transform' method. Here's an example:

y_old = le.inverse_transform(y_new).reshape(10,7)

This will reshape the array to have 10 rows and 7 columns. Hope this helps!
 

What is Sklearn LabelEncoder inverse transform?

The Sklearn LabelEncoder inverse transform is a method used to convert the encoded labels back to their original values. It is commonly used in machine learning tasks to preprocess categorical data before training models.

How does Sklearn LabelEncoder inverse transform work?

The Sklearn LabelEncoder inverse transform works by using the mapping between the original labels and their encoded values, which is stored in the LabelEncoder object. It then replaces the encoded values with their corresponding original labels.

When should I use Sklearn LabelEncoder inverse transform?

You should use Sklearn LabelEncoder inverse transform when you have encoded categorical data using the Sklearn LabelEncoder and need to convert it back to its original values for further analysis or to make predictions with a trained model.

Can Sklearn LabelEncoder inverse transform handle missing values?

No, Sklearn LabelEncoder inverse transform cannot handle missing values. It will raise an error if there are missing values in the data that needs to be transformed.

Are there any alternatives to Sklearn LabelEncoder inverse transform?

Yes, there are alternatives to Sklearn LabelEncoder inverse transform such as the OneHotEncoder and OrdinalEncoder in Sklearn, as well as other methods in different libraries such as Pandas' get_dummies() function.

Similar threads

  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
7
Views
792
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
5
Views
1K
  • Linear and Abstract Algebra
Replies
5
Views
951
  • Programming and Computer Science
Replies
1
Views
1K
  • Special and General Relativity
Replies
1
Views
1K
  • Advanced Physics Homework Help
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top