Skip to content

Commit

Permalink
Implemented PR review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
trechberger committed Aug 18, 2020
1 parent 740a5e5 commit 6297d76
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
9 changes: 4 additions & 5 deletions coupled_biased_random_walks/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ def _score(self, 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))
return sum((self._value_scores(observation)).values())

def value_scores(self, observation_iterable):
"""
Expand All @@ -111,16 +109,17 @@ def value_scores(self, observation_iterable):
raise CBRWScoreError()
if isinstance(observation_iterable, dict):
observation_iterable = [observation_iterable]
return np.array([self._value_scores(obs) for obs in observation_iterable])
return [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
:param observation: dict of the form {feature_name: feature_value, ...}
"""
score_keys = []
value_scores = []
for item in iteritems(observation):
score_keys.append(item[0])
score_keys.append(get_feature_name(item))
value_scores.append(self._get_feature_relevance(item) * \
self._stationary_prob.get(item, self._unknown_feature_score))
return dict(zip(score_keys, value_scores))
Expand Down
13 changes: 6 additions & 7 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from six.moves import zip

#from coupled_biased_random_walks import CBRW
from coupled_biased_random_walks.detect import CBRW
from coupled_biased_random_walks import CBRW
from data.loading import load_from_csv


Expand All @@ -23,14 +23,13 @@

# fit and score data
detector.fit()
print('Feature weights: ', detector.feature_weights)
print()
print('Feature weights:\n{}\n'.format(detector.feature_weights))

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))
print('\nValue scores per attribute:')
for i, value_score in enumerate(detector.value_scores(data)):
print('Observation {}: {}'.format(i, value_score))

0 comments on commit 6297d76

Please sign in to comment.