BehaviorAnalyzer.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: utf-8 -*-
  2. """
  3. @author: zsc
  4. @time: 2024/11/18
  5. @desc: 行为分析
  6. """
  7. import time
  8. import random
  9. from collections import defaultdict
  10. import matplotlib.pyplot as plt
  11. # 行为分析模块
  12. class BehaviorAnalyzer:
  13. def __init__(self, data):
  14. self.data = data
  15. def analyze(self):
  16. # 分析行为数据,例如计算每个流程的行为次数
  17. process_stats = defaultdict(lambda: defaultdict(int))
  18. action_stats = defaultdict(int)
  19. product_stats = defaultdict(int)
  20. channel_stats = defaultdict(int)
  21. for user_actions in self.data:
  22. process = user_actions['process']
  23. actions = user_actions['actions']
  24. product = user_actions['product']
  25. channel = user_actions['channel']
  26. for action in actions:
  27. process_stats[process][action] += 1
  28. action_stats[action] += 1
  29. product_stats[product] += 1
  30. channel_stats[channel] += 1
  31. # 将内部字典转换为普通字典
  32. process_stats = {process: dict(actions) for process, actions in process_stats.items()}
  33. return self.data, process_stats, action_stats, product_stats, channel_stats