SegmentUsers.py 833 B

123456789101112131415161718192021222324252627282930
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: zsc
  4. @time: 2024/11/18
  5. @desc: 行为分析
  6. """
  7. from collections import defaultdict
  8. # 用户分群模块
  9. class UserSegmentation:
  10. def __init__(self, data):
  11. self.data = data
  12. def segment(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. segments = {'高活跃用户': [], '低活跃用户': []}
  20. for user, count in user_behavior_count.items():
  21. if count > 3:
  22. segments['高活跃用户'].append(user)
  23. else:
  24. segments['低活跃用户'].append(user)
  25. return segments