4

I have a model with several categorical features that need to be converted to numeric format. I am using a combination of LabelEncoder and OneHotEncoder to achieve this. Once in production, I need to apply the same encoding to new incoming data before the model can be used. I've saved on disk the model and the encoders using pickle. The problem here is that the LabelEncoder keeps only the last set of classes (for the last feature it has encoded), thus it can't be used to encode all the categorical features for the new data. To face this issue I am saving on disk a different LabelEncoder for each one of the categorical features, but this does not seem to scale very well to me, especially when you have a large number of categorical features.

What is the common practice for this situation? Is it possible to serialize and save just one encoder for all the categorical features to be used in production?

revy
  • 3,945
  • 7
  • 40
  • 85

1 Answers1

0

If i understand your question well. I think you need to confirm a few things here.

  1. the Schema of the DataFrame or the payload needs to be confirmed in Production.
  2. once the schema is confirmed, you can always serialized these encoders. for example, you can store the encoder as Dict(). cate_encoders = {"feature_1": LabelEncoder(), "feature_2": LabelEncoder(), "feature_3": OneHotEncoder()} and serialzied it.
  3. for model in production, you could create a preprocessor class, for example
  4. and then create model (load model from disk or s3) and do prediction...

class MyPreprocessor:
    def __init__(self):
        self.cate_transform = None
        self.num_transform = None
    def load_transform(self, cat_trans, num_trans):
        pass
    def transform(self):
        pass
seninus
  • 256
  • 2
  • 5