Skip to content

Instantly share code, notes, and snippets.

@cindyangelira
Created January 23, 2023 15:15
Show Gist options
  • Save cindyangelira/6e01eb8e1f46b4b952a09e452f8dcaab to your computer and use it in GitHub Desktop.
Save cindyangelira/6e01eb8e1f46b4b952a09e452f8dcaab to your computer and use it in GitHub Desktop.
Market Basket Analysis Streamlit
def encode_units(x):
if x <= 0:
return 0
else:
return 1
def mba(df, support):
df1 = df.copy()
df2 = df1[df1.sum(axis=1) > 1]
frequent_itemsets = apriori(df2.astype('bool'), min_support=support, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1).sort_values(by='lift', ascending =False)
return rules
def frequent_bought_recommender(basket_df, item, support):
basket_sets = basket_df.copy()
df_item = basket_sets.loc[basket_sets[item] == 1]
item_frequent = apriori(df_item, min_support=support, use_colnames=True)
a_rules = association_rules(item_frequent, metric="lift", min_threshold=1)
# Sorting on lift and support
a_rules.sort_values(['lift','support'],ascending=False).reset_index(drop=True)
recommender = a_rules['consequents'].unique()[:5]
return recommender
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment