def transform(self, M, **kwargs):
"""
Takes a Takes a dataframe that has :code:`item_id` index, other
'features' columns for prediction, and applies a Keras sequential
model to it.
:param M:
a dataframe that has an :code:`item_id` index, and
"features" columns.
:type M: pandas.DataFrame
:rtype: a tuple with trained Keras model and its keyword
arguments
"""
rows, columns = M.shape
factors = M.merge(self.validation_matrix, left_index=True,
right_index=True)
factors = factors.values
if self.classification:
kfold = StratifiedKFold(n_splits=self.kfold_n_splits,
random_state=self.kfold_seed,
shuffle=self.kfold_shuffle)
else:
kfold = KFold(n_splits=self.kfold_n_splits,
random_state=self.kfold_seed,
shuffle=self.kfold_shuffle)
X = factors[:, :columns]
Y = factors[:, columns:]
for train_index, test_index in kfold.split(X, Y):
self.keras_model.fit(
X[train_index], Y[train_index],
validation_data=[X[test_index], Y[train_index]],
**self.keras_kwargs)
return self.keras_model, kwargs
评论列表
文章目录