如何将单热编码存储为对象?

How can I store one-hot-encodings as an object?

首先,介绍一下我的模型架构的背景知识。

我的 keras 模型的输入相当简单:

该模型具有单个输出:

训练模型时,我的输入数据是使用 pd.read_sql() 来自 SQL 数据库的数据框。我使用以下函数对分类变量 A 和 B(分别位于数据帧 original_datacol1col2 中)进行单热编码:

from keras import utils as np_utils

def preprocess_categorical_features(self):
        col1 = np_utils.to_categorical(np.copy(self.original_data.CURRENT_RTIF.values))
        col2 = np_utils.to_categorical(np.copy(self.original_data.NEXT_RTIF.values))
        cat_input_data = np.append(col1,col2,axis=1)
        return cat_input_data

稍后,当我需要根据该模型进行预测时,输入数据来自 RabbitMQ 的字典形式的实时提要。此 RabbitMQ 数据必须由它自己的(不同的)reprocess_categorical_features() 函数处理。

这让我想到了我的问题:无论我是在预处理来自数据库的数据还是来自 rabbitMQ 提要,如何确保单热编码完全相同?

应用于数据库数据的A的One-Hot Encoding:

|---------------------|------------------|
|          A          | One-Hot-Encoding |
|---------------------|------------------|
|       "coconut"     |      <0,1,0,0>   |
|---------------------|------------------|
|       "apple"       |      <1,0,0,0>   |
|---------------------|------------------|
|       "quince"      |      <0,0,0,1>   |
|---------------------|------------------|
|       "plum"        |      <0,1,0,0>   |
|---------------------|------------------|

应用于 RabbitMQ 数据的 A 的单热编码(它们必须相同):

|---------------------|------------------|
|          A          | One-Hot-Encoding |
|---------------------|------------------|
|       "coconut"     |      <0,1,0,0>   |
|---------------------|------------------|
|       "apple"       |      <1,0,0,0>   |
|---------------------|------------------|
|       "quince"      |      <0,0,0,1>   |
|---------------------|------------------|
|       "plum"        |      <0,1,0,0>   |
|---------------------|------------------|

有没有办法将编码保存为数据帧、numpy ndarray 或字典,以便我可以将编码从预处理我的训练数据的函数传递到预处理函数我的输入数据?我愿意为 OHE 使用 Keras 以外的其他库,但我很想知道是否有办法使用我目前正在使用的 keras 的 to_categorical 函数来完成此操作。

而不是依赖 keras 的 utils.to_categorical method, I decided to use sklearn.preprocessing.OneHotEncoder。这允许我在处理训练数据时声明一个单热编码器对象,self.encoder

class TrainingData:
    def preprocess_categorical_features(self):
        # declare OneHotEncoder object to save for later
        self.encoder = OneHotEncoder(sparse=False)

        # fit encoder to data
        self.encoder.fit(self.original_data.CURRENT_RTIF.values.reshape(-1,1))

        # perform one-hot-encoding on columns 1 and 2 of the training data
        col1 = self.encoder.transform(self.original_data.CURRENT_RTIF.values.reshape(-1,1))
        col2 = self.encoder.transform(self.original_data.NEXT_RTIF.values.reshape(-1,1))

        # return on-hot-encoded data as a numpy ndarray
        cat_input_data = np.append(col1,col2,axis=1)
        return cat_input_data

稍后,我可以重新使用该编码器(通过将其作为参数传递,training_data_ohe_encoder)到处理最终做出预测所需的输入数据的方法。

class LiveData:
    def preprocess_categorical_features(self, training_data_ohe_encoder):
        # notice the training_data_ohe_encoder parameter; this is the 
        # encoder attribute from the Training Data Class.

        # one-hot-encode the live data using the training_data_ohe_encoder encoder
        col1 = training_data_ohe_encoder.transform(np.copy(self.preprocessed_data.CURRENT_RTIF.values).reshape(-1, 1))
        col2 = training_data_ohe_encoder.transform(np.copy(self.preprocessed_data.NEXT_RTIF.values).reshape(-1, 1))

        # return on-hot-encoded data as a numpy ndarray
        cat_input_data = np.append(col1,col2,axis=1)
        return cat_input_data