From 740a5e559b39fd3738b603c209fc4b1a77a5425b Mon Sep 17 00:00:00 2001 From: "tobias.rechberger" Date: Tue, 21 Jul 2020 12:21:39 +0200 Subject: [PATCH] Added individual scores per attributes of an ovservation and adapted example acc. --- coupled_biased_random_walks/detect.py | 29 ++++++++++++++++++++++++++- example.py | 11 ++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/coupled_biased_random_walks/detect.py b/coupled_biased_random_walks/detect.py index e4c5f3c..cca76a8 100644 --- a/coupled_biased_random_walks/detect.py +++ b/coupled_biased_random_walks/detect.py @@ -91,13 +91,40 @@ def score(self, observation_iterable): def _score(self, observation): """ - Computes the weighted anomaly score for an observation + Computes the weighted anomaly score (object_score in the paper) for an observation :param observation: dict of the form {feature_name: feature_value, ...} """ return sum(self._get_feature_relevance(item) * \ self._stationary_prob.get(item, self._unknown_feature_score) for item in iteritems(observation)) + def value_scores(self, observation_iterable): + """ + Compute an anomaly sub-score for each value of the observation in observation_iterable + :param observation_iterable: iterable of dict observations with each dict + of the form {feature_name: feature_value, ...} + Return dict with sub score of each value of the observation/object of the form: + {feature_name: weighted score of value of feature, ...} + (sub-scores sum up to score(self, observation_iterable)) + """ + if not (self._feature_relevance and self._stationary_prob): + raise CBRWScoreError() + if isinstance(observation_iterable, dict): + observation_iterable = [observation_iterable] + return np.array([self._value_scores(obs) for obs in observation_iterable]) + + def _value_scores(self, observation): + """ + Computes the weighted value scores for each feature value of an observation + """ + score_keys = [] + value_scores = [] + for item in iteritems(observation): + score_keys.append(item[0]) + value_scores.append(self._get_feature_relevance(item) * \ + self._stationary_prob.get(item, self._unknown_feature_score)) + return dict(zip(score_keys, value_scores)) + def _get_feature_relevance(self, feature_tuple): """ Getter for the relevance (weight) of a feature (category) diff --git a/example.py b/example.py index f151359..a88672b 100644 --- a/example.py +++ b/example.py @@ -2,7 +2,8 @@ from six.moves import zip -from coupled_biased_random_walks import CBRW +#from coupled_biased_random_walks import CBRW +from coupled_biased_random_walks.detect import CBRW from data.loading import load_from_csv @@ -22,8 +23,14 @@ # fit and score data detector.fit() + print('Feature weights: ', detector.feature_weights) + print() scores = detector.score(data) - # print scores and observations for score, datum in zip(scores, data): print('Score: {} | Data: {}'.format(round(score, 4), datum)) + + print() + print('Scores: ', scores) + print() + print('Value scores per attribute: ', detector.value_scores(data)) \ No newline at end of file