首先,介绍一下我的模型的架构背景。
对我的keras模型的输入相当简单:
该模型具有一个单一的输出:
在培训模型时,我的输入数据是来自使用pd.read_sql()
的SQL数据库的数据。我用以下函数对分类变量A和B(分别在dataframe col1
和col2
的original_data
中)进行了一次热编码:
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的一次热编码:
|---------------------|------------------|
| 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‘范畴化函数来完成这一任务。
发布于 2019-09-24 17:14:59
我没有依赖keras的utils.to_categorical
方法,而是决定使用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
https://stackoverflow.com/questions/58066673
复制相似问题