|
| 1 | +""" |
| 2 | +Example demonstrating itemset support in GSP-Py. |
| 3 | +
|
| 4 | +This example shows how to use GSP-Py with itemsets, where multiple items |
| 5 | +can occur together at the same time step in a sequence. |
| 6 | +
|
| 7 | +Key concepts: |
| 8 | +1. Flat sequences: ['A', 'B', 'C'] - each item at separate time steps |
| 9 | +2. Itemset sequences: [['A', 'B'], ['C']] - A and B occur together, then C |
| 10 | +
|
| 11 | +Author: Jackson Antonio do Prado Lima |
| 12 | + |
| 13 | +""" |
| 14 | + |
| 15 | +from gsppy import GSP |
| 16 | + |
| 17 | +def example_flat_vs_itemset(): |
| 18 | + """ |
| 19 | + Demonstrate the difference between flat and itemset representations. |
| 20 | + """ |
| 21 | + print("=" * 80) |
| 22 | + print("EXAMPLE 1: Flat vs Itemset Sequences") |
| 23 | + print("=" * 80) |
| 24 | + |
| 25 | + # Flat sequences - each item happens at a separate time step |
| 26 | + print("\n1a. Flat sequences (traditional format):") |
| 27 | + flat_transactions = [ |
| 28 | + ['A', 'B', 'C'], # A, then B, then C |
| 29 | + ['A', 'C'], # A, then C |
| 30 | + ['A', 'B', 'C'], # A, then B, then C |
| 31 | + ] |
| 32 | + print(f" Transactions: {flat_transactions}") |
| 33 | + |
| 34 | + gsp_flat = GSP(flat_transactions) |
| 35 | + patterns_flat = gsp_flat.search(min_support=0.66) |
| 36 | + |
| 37 | + print(" Frequent patterns (min_support=0.66):") |
| 38 | + for i, level_patterns in enumerate(patterns_flat, start=1): |
| 39 | + print(f" {i}-sequences: {level_patterns}") |
| 40 | + |
| 41 | + # Itemset sequences - items in same list occur together |
| 42 | + print("\n1b. Itemset sequences:") |
| 43 | + itemset_transactions = [ |
| 44 | + [['A', 'B'], ['C']], # A and B together, then C |
| 45 | + [['A'], ['C']], # A, then C |
| 46 | + [['A', 'B'], ['C']], # A and B together, then C |
| 47 | + ] |
| 48 | + print(f" Transactions: {itemset_transactions}") |
| 49 | + |
| 50 | + gsp_itemset = GSP(itemset_transactions) |
| 51 | + patterns_itemset = gsp_itemset.search(min_support=0.66) |
| 52 | + |
| 53 | + print(" Frequent patterns (min_support=0.66):") |
| 54 | + for i, level_patterns in enumerate(patterns_itemset, start=1): |
| 55 | + print(f" {i}-sequences: {level_patterns}") |
| 56 | + |
| 57 | + |
| 58 | +def example_market_basket(): |
| 59 | + """ |
| 60 | + Real-world example: Market basket analysis with itemsets. |
| 61 | + |
| 62 | + Customers can buy multiple items in a single transaction, then return |
| 63 | + to buy more items in subsequent transactions. |
| 64 | + """ |
| 65 | + print("\n" + "=" * 80) |
| 66 | + print("EXAMPLE 2: Market Basket Analysis with Itemsets") |
| 67 | + print("=" * 80) |
| 68 | + |
| 69 | + # Each customer's purchase history |
| 70 | + # Nested lists represent items bought together (same shopping trip) |
| 71 | + transactions = [ |
| 72 | + # Customer 1: Bought bread & milk together, then came back for eggs |
| 73 | + [['Bread', 'Milk'], ['Eggs']], |
| 74 | + |
| 75 | + # Customer 2: Bought bread, milk & butter together |
| 76 | + [['Bread', 'Milk', 'Butter']], |
| 77 | + |
| 78 | + # Customer 3: Bought bread & milk together, then eggs later |
| 79 | + [['Bread', 'Milk'], ['Eggs']], |
| 80 | + |
| 81 | + # Customer 4: Bought bread & milk together, then eggs & butter together |
| 82 | + [['Bread', 'Milk'], ['Eggs', 'Butter']], |
| 83 | + ] |
| 84 | + |
| 85 | + print("\nCustomer transaction history:") |
| 86 | + for i, tx in enumerate(transactions, start=1): |
| 87 | + print(f" Customer {i}: {tx}") |
| 88 | + |
| 89 | + gsp = GSP(transactions) |
| 90 | + patterns = gsp.search(min_support=0.5) |
| 91 | + |
| 92 | + print("\nFrequent patterns (min_support=0.5, i.e., 2+ customers):") |
| 93 | + for i, level_patterns in enumerate(patterns, start=1): |
| 94 | + print(f"\n {i}-sequences:") |
| 95 | + for pattern, support in level_patterns.items(): |
| 96 | + print(f" {pattern} - appears in {support} customer histories") |
| 97 | + |
| 98 | + # Insights |
| 99 | + print("\n📊 Insights:") |
| 100 | + print(" - Customers who buy Bread and Milk often return to buy Eggs later") |
| 101 | + print(" - This is different from 'Bread, then Milk, then Eggs' pattern") |
| 102 | + print(" - Itemsets capture co-occurrence (items bought together)") |
| 103 | + |
| 104 | + |
| 105 | +def example_clickstream(): |
| 106 | + """ |
| 107 | + Example: Web analytics with itemsets. |
| 108 | + |
| 109 | + Users can view multiple pages in parallel (multiple tabs) before |
| 110 | + moving to the next set of pages. |
| 111 | + """ |
| 112 | + print("\n" + "=" * 80) |
| 113 | + print("EXAMPLE 3: Web Clickstream with Parallel Page Views") |
| 114 | + print("=" * 80) |
| 115 | + |
| 116 | + # User sessions with parallel page views |
| 117 | + sessions = [ |
| 118 | + # User 1: Opened Home & Products in tabs, then viewed Checkout |
| 119 | + [['Home', 'Products'], ['Checkout']], |
| 120 | + |
| 121 | + # User 2: Home and Products together, then Cart, then Checkout |
| 122 | + [['Home', 'Products'], ['Cart'], ['Checkout']], |
| 123 | + |
| 124 | + # User 3: Home page, then Products & Cart together, then Checkout |
| 125 | + [['Home'], ['Products', 'Cart'], ['Checkout']], |
| 126 | + |
| 127 | + # User 4: Home & Products together, then Checkout |
| 128 | + [['Home', 'Products'], ['Checkout']], |
| 129 | + ] |
| 130 | + |
| 131 | + print("\nUser sessions (parallel page views):") |
| 132 | + for i, session in enumerate(sessions, start=1): |
| 133 | + print(f" User {i}: {session}") |
| 134 | + |
| 135 | + gsp = GSP(sessions) |
| 136 | + patterns = gsp.search(min_support=0.5) |
| 137 | + |
| 138 | + print("\nFrequent navigation patterns (min_support=0.5):") |
| 139 | + for i, level_patterns in enumerate(patterns, start=1): |
| 140 | + if level_patterns: |
| 141 | + print(f"\n {i}-sequences:") |
| 142 | + for pattern, support in level_patterns.items(): |
| 143 | + print(f" {pattern} - in {support} sessions") |
| 144 | + |
| 145 | + |
| 146 | +def example_spm_format(): |
| 147 | + """ |
| 148 | + Example: Reading itemsets from SPM format files. |
| 149 | + |
| 150 | + SPM format uses delimiters: |
| 151 | + - `-1` marks end of itemset |
| 152 | + - `-2` marks end of sequence |
| 153 | + """ |
| 154 | + print("\n" + "=" * 80) |
| 155 | + print("EXAMPLE 4: Reading Itemsets from SPM Format") |
| 156 | + print("=" * 80) |
| 157 | + |
| 158 | + import tempfile |
| 159 | + import os |
| 160 | + from gsppy.utils import read_transactions_from_spm |
| 161 | + |
| 162 | + # Create a temporary SPM file with itemsets |
| 163 | + spm_content = """1 2 -1 3 -1 -2 |
| 164 | +1 -1 3 4 -1 -2 |
| 165 | +1 2 -1 3 -1 -2""" |
| 166 | + |
| 167 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f: |
| 168 | + f.write(spm_content) |
| 169 | + temp_path = f.name |
| 170 | + |
| 171 | + try: |
| 172 | + print(f"\nSPM file content:\n{spm_content}") |
| 173 | + |
| 174 | + # Read with itemsets flattened (backward compatible) |
| 175 | + print("\nReading with itemsets flattened (preserve_itemsets=False):") |
| 176 | + flat_txs = read_transactions_from_spm(temp_path, preserve_itemsets=False) |
| 177 | + for i, tx in enumerate(flat_txs, start=1): |
| 178 | + print(f" Transaction {i}: {tx}") |
| 179 | + |
| 180 | + # Read with itemsets preserved |
| 181 | + print("\nReading with itemsets preserved (preserve_itemsets=True):") |
| 182 | + itemset_txs = read_transactions_from_spm(temp_path, preserve_itemsets=True) |
| 183 | + for i, tx in enumerate(itemset_txs, start=1): |
| 184 | + print(f" Transaction {i}: {tx}") |
| 185 | + |
| 186 | + # Use in GSP |
| 187 | + print("\nRunning GSP on itemset data:") |
| 188 | + gsp = GSP(itemset_txs) |
| 189 | + patterns = gsp.search(min_support=0.66) |
| 190 | + print(f" Frequent patterns: {patterns}") |
| 191 | + |
| 192 | + finally: |
| 193 | + os.unlink(temp_path) |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == '__main__': |
| 197 | + # Run all examples |
| 198 | + example_flat_vs_itemset() |
| 199 | + example_market_basket() |
| 200 | + example_clickstream() |
| 201 | + example_spm_format() |
| 202 | + |
| 203 | + print("\n" + "=" * 80) |
| 204 | + print("Summary:") |
| 205 | + print("=" * 80) |
| 206 | + print("✓ Itemsets capture co-occurrence of items at the same time step") |
| 207 | + print("✓ Flat sequences are automatically normalized to itemsets internally") |
| 208 | + print("✓ Both formats work seamlessly with GSP-Py") |
| 209 | + print("✓ Use itemsets when temporal co-occurrence matters in your domain") |
| 210 | + print("=" * 80) |
0 commit comments