Для этого вы можете переопределить оригинальный LabelEncoder с помощью пользовательского кодировщика.
Примерно так:
import numpy as np
class TolerantLabelEncoder(LabelEncoder):
def __init__(self, ignore_unknown=False,
unknown_original_value='unknown',
unknown_encoded_value=-1):
self.ignore_unknown = ignore_unknown
self.unknown_original_value = unknown_original_value
self.unknown_encoded_value = unknown_encoded_value
def transform(self, y):
check_is_fitted(self, 'classes_')
y = column_or_1d(y, warn=True)
indices = np.isin(y, self.classes_)
if not self.ignore_unknown and not np.all(indices):
raise ValueError("y contains new labels: %s"
% str(np.setdiff1d(y, self.classes_)))
y_transformed = np.searchsorted(self.classes_, y)
y_transformed[~indices]=self.unknown_encoded_value
return y_transformed
def inverse_transform(self, y):
check_is_fitted(self, 'classes_')
labels = np.arange(len(self.classes_))
indices = np.isin(y, labels)
if not self.ignore_unknown and not np.all(indices):
raise ValueError("y contains new labels: %s"
% str(np.setdiff1d(y, self.classes_)))
y_transformed = np.asarray(self.classes_[y], dtype=object)
y_transformed[~indices]=self.unknown_original_value
return y_transformed
Пример использования:
en = TolerantLabelEncoder(ignore_unknown=True)
en.fit(['a','b'])
print(en.transform(['a', 'c', 'b']))
# Output: [ 0 -1 1]
print(en.inverse_transform([-1, 0, 1]))
# Output: ['unknown' 'a' 'b']