Calculate Shannon entropy for a list of commands. Shannon entropy measures the "surprise" or information content in the distribution. - Higher entropy = more uniform distribution across command types (more diverse) - Lower entropy = some commands are much more frequent (more focuse
(command_list)
| 49 | |
| 50 | |
| 51 | def shannon_entropy(command_list): |
| 52 | """ |
| 53 | Calculate Shannon entropy for a list of commands. |
| 54 | |
| 55 | Shannon entropy measures the "surprise" or information content in the distribution. |
| 56 | - Higher entropy = more uniform distribution across command types (more diverse) |
| 57 | - Lower entropy = some commands are much more frequent (more focused) |
| 58 | - Max entropy = log2(unique_commands) when all commands equally frequent |
| 59 | |
| 60 | Args: |
| 61 | command_list: List of command strings (e.g., ['ls', 'cat', 'ls', 'grep']) |
| 62 | |
| 63 | Returns: |
| 64 | Float entropy value (0 = single command type, higher = more diverse) |
| 65 | """ |
| 66 | if not command_list: |
| 67 | return 0.0 |
| 68 | |
| 69 | # Count frequency of each command type |
| 70 | counter = Counter(command_list) |
| 71 | total = len(command_list) |
| 72 | entropy = 0.0 |
| 73 | |
| 74 | # Calculate Shannon entropy: -Σ(p * log2(p)) where p is probability of each command |
| 75 | for count in counter.values(): |
| 76 | prob = count / total |
| 77 | entropy -= prob * math.log2(prob) |
| 78 | |
| 79 | return entropy |
| 80 | |
| 81 | |
| 82 | def extract_commands_from_trajectory(traj): |