class ValueLastMatchFeature(object): """Extract transient feature from previous event""" def __init__(self, field_name, entity_field, feature_name): self.field_name = field_name self.entity_field = entity_field self.feature_name = feature_name self.state_manager = StateManager() def initialise_state(self, entity_id): """Sum and count state initiated as 0. and 0.""" self.state_manager.write(entity_id, None) def get_feature_names(self): """Return feature name""" return [self.feature_name] def pre_extraction_update(self, event): pass def extract_feature_dict(self, event): # if state missing, initialise state entity_id = event[self.entity_field] if self.state_manager.entity_missing(entity_id): self.initialise_state(entity_id) return {self.feature_name: self.state_manager.read(entity_id)} def post_extraction_update(self, event): self.state_manager.write(event[self.entity_field], event[self.field_name])
class DecayFeature(object): def __init__(self, feature_name, entity_field, sum_value_field, incl_count, incl_sum, incl_mean, decay_rate): """Instance initiated with state_id and sample_decay_rate""" self.feature_name = feature_name self.entity_field = entity_field self.sum_value_field = sum_value_field self.incl_count = incl_count self.incl_sum = incl_sum self.incl_mean = incl_mean self.decay_rate = decay_rate self.state_manager = StateManager() def initialise_state(self, entity_id): raise Exception("Implementation missing") def get_feature_names(self): feature_names = [] if self.incl_count: feature_names.append(self.feature_name + "_count") if self.incl_sum: feature_names.append(self.feature_name + "_sum") if self.incl_mean: feature_names.append(self.feature_name + "_mean") return feature_names def pre_extraction_update(self, event): raise Exception("Implementation missing") def extract_feature_dict(self, event): """Return feature dict state""" # read state using state manager state = self.state_manager.read(event[self.entity_field]) feature_dict = {} if self.incl_count: feature_dict[self.feature_name + "_count"] = state['count'] if self.incl_sum: feature_dict[self.feature_name + "_sum"] = state['sum'] if self.incl_mean: feature_dict[self.feature_name + "_mean"] = safe_division( state['sum'], state['count'], 1.) return feature_dict