I am using sklearn's LabelEncoder() to encode the labels of data with value 0 and 1. The labels assigned to the data by the function need to be changed from 0 to 1 and vice versa. How can I flip the labels?

+2 votes

0 votes

You can subtract the outcome of transform() from 1 to flip the labels of your data. Check the following example.

>>> from sklearn import preprocessing

>>> le = preprocessing.LabelEncoder()

>>> Y=['p','p','e','e','e','p','e','p']

>>> le.fit(Y)

LabelEncoder()

>>> le.transform(Y) #get numeric label

array([1, 1, 0, 0, 0, 1, 0, 1], dtype=int64)>>> 1-le.transform(Y) # flip the label

array([0, 0, 1, 1, 1, 0, 1, 0], dtype=int64)

>>>