Extend LabelEncoder classes

I have a LabelEncoder with 500 classes.

To store and load it, I used pickle:

with open('../data/label_encoder_v500.pkl', 'rb') as file:
    label_encoder = pickle.load(file)

I want to add 24 new classes to this encoder, keeping existing labels unchanged.

additional_classes = ['class501', 'class502', ..., 'class524']

but it seems like this operation does not come out-of-the-box with LabelEncoder. How to do this?

Since the LabelEncoder is quite simple one option will to modify it to a expanded LabelEncoder with this functionality.

from sklearn.preprocessing import LabelEncoder
from sklearn.utils import column_or_1d
import numpy as np

class ExpandedLabelEncoder(LabelEncoder):
    def add_extra_labels(self, y):
        y = column_or_1d(y, warn=True)
        y_expand = np.concatenate((self.classes_, y))
        super().fit(y_expand)

encoder = ExpandedLabelEncoder()
encoder.fit([class001', ..., 'class500'])
# save as you normally do

loaded and reuse the extra added functionality which allow you to add new labels

with open('../data/label_encoder_v500.pkl', 'rb') as file:
    label_encoder = pickle.load(file)

additional_classes = ['class501', 'class502', ..., 'class524']
encoder.add_extra_labels(additional_classes)

Note that as with the standar LabelEncoder when using it to transform data, the encoding order is not the one use to fit labels but that of the order in which labels appear in the array pass to transform.

I am not sure what you want to do is actually possible. The natural idea, as stated previously, is to manually add labels to the classes_ parameter. However, it does not always behave as intended:

from sklearn.preprocessing import LabelEncoder
import numpy as np
le = LabelEncoder()
le.fit([1, 2, 2, 6])
le.transform([1, 1, 2, 6])
>>> array([0, 0, 1, 2])

Now let’s manually add a label :

import numpy as np
le.classes_ = np.concatenate([le.labels_, [4]])
le.transform([1,2,6,4])
>>> array([0, 1, 2, 2])

whereas you would have expected array([0,1,2,3]). If you sort the labels while adding them,

le.classes_ = np.sort(np.concatenate([le.labels_, [4]]))
le.transform([1,2,6,4])
>>> array([0, 1, 3, 2])

Every figure gets a different index, however your “6” is not encoded the same way it was before, which may be a problem down the road.
Note, however, that the problem would have not occured if you new labels are all above the max label of your initial labels (for instance, if I had added a 7 instead of a 4 in the example above).
This is because transform expects the labels to be sorted.

So, I guess a workaround is to transform your new labels so that you are certain they are all superior to the max existing label.

Yeah, this operation does not come out-of-the-box. You can extend classes_ attr manually.
Here is example

import pickle
import numpy as np

with open('../data/label_encoder_v500.pkl','rb') as file:
    label_encoder = pickle.load(file)

additional_classes = ['class501', ...,]

updated_classes=np.concatenate([label_encoder.classes_,additional_classes])

label_encoder.classes_ =np.sort(updated_classes)

# create new file if you want
with open('../daa/label_encoder_v524.pkl','wb' ) as file:
    pickle.dump(label_encoder,file)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật