DetectAnomalies.py 743 B

12345678910111213141516171819202122232425262728
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: zsc
  4. @time: 2024/11/18
  5. @desc: 行为分析
  6. """
  7. from collections import defaultdict
  8. # 异常检测模块
  9. class AnomalyDetector:
  10. def __init__(self, data):
  11. self.data = data
  12. def detect(self):
  13. # 简单示例:检测行为次数异常高的用户
  14. user_behavior_count = defaultdict(int)
  15. for user_actions in self.data:
  16. user = user_actions['user']
  17. actions = user_actions['actions']
  18. user_behavior_count[user] += len(actions)
  19. anomalies = []
  20. for user, count in user_behavior_count.items():
  21. if count > 20: # 假设行为次数超过20为异常
  22. anomalies.append(user)
  23. return anomalies