Recommendation Systems
Recommendation systems are algorithms designed to suggest items or content to users based on their preferences, behaviors, or similarities with other users.
Association Rule Mining (ARM) is a technique used in recommendation systems to uncover patterns in transaction data. By analyzing user-item interactions, ARM identifies associations such as "users who buy X
also tend to buy Y
," enabling personalized recommendations.
For example, if a user has purchased item X
, the system can recommend item Y
based on this association.
This enhances the shopping experience by offering relevant suggestions based on individual preferences and behaviors.
Example
Let's discover a code example that demonstrates a simple recommendation system using Association Rule Mining:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import pandas as pd import numpy as np from mlxtend.frequent_patterns import apriori, association_rules from mlxtend.preprocessing import TransactionEncoder import warnings # Ignore warnings warnings.filterwarnings('ignore') # Generate synthetic transaction data np.random.seed(42) num_transactions = 10000 # Number of transactions to generate min_items_per_transaction = 5 # Minimum number of items per transaction max_items_per_transaction = 20 # Maximum number of items per transaction # List of items available for transactions items = ["bread", "milk", "eggs", "cheese", "butter", "yogurt", "apples", "bananas", "oranges", "grapes", "chicken", "beef", "pork", "fish", "rice", "pasta", "potatoes", "onions", "tomatoes", "lettuce"] # Generate transactions transactions = [] for _ in range(num_transactions): # Randomly select the number of items in each transaction num_items_in_transaction = np.random.randint(min_items_per_transaction, max_items_per_transaction + 1) # Randomly select items for the transaction transaction = list(np.random.choice(items, size=num_items_in_transaction, replace=True)) transactions.append(transaction) # Convert transaction data to DataFrame format df = pd.DataFrame(transactions) # Apply transaction encoding using TransactionEncoder encoder = TransactionEncoder() df_encoded = encoder.fit_transform(transactions) # Convert encoded transactions to DataFrame format df_encoded = pd.DataFrame(df_encoded, columns=encoder.columns_) # Mine frequent itemsets using the Apriori algorithm frequent_itemsets = apriori(df_encoded, min_support=0.05, use_colnames=True) # Generate association rules based on frequent itemsets rules = association_rules(frequent_itemsets, metric='confidence', min_threshold=0.5) # Define items purchased by the user user_items = {'beef', 'apples', 'bread'} # Filter association rules to find relevant recommendations for the user recommended_items = set() for index, row in rules.iterrows(): antecedent = set(row['antecedents']) consequent = set(row['consequents']) # Check if the user's items are a subset of the antecedent of the rule if antecedent.issuperset(user_items): # Add recommended items to the set, excluding items already purchased by the user recommended_items.update(consequent.difference(user_items)) # Print recommended items for the user print('Recommended items for the user:', recommended_items)
As a result, we can conclude that if a person has purchased all these items: {'beef', 'apples', 'bread'}, we can recommend them to purchase all the goods provided in the result of the code sample execution.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla