import pandas as pd import streamlit as st def explain_market_basket_rules(df: pd.DataFrame) -> None: """ Function to output output basket analysis metrics in user friendly manner :param df: Pandas dataframe :return: None """ for _, row in df.iterrows(): first = row["antecedents"] second = row["consequents"] support = row["support"] confidence = row["confidence"] lift = row["lift"] support_pct = support * 100 confidence_pct = confidence * 100 explanation = ( f"When customers buy **{first}**, there's a **{confidence_pct:.1f}% chance** they’ll also buy **{second}**.\n" f"This combination appears in **{support_pct:.3f}%** of all transactions, so it's relatively {'rare' if support < 0.01 else 'common'}.\n" f"The lift value is **{lift:.1f}**, meaning customers are **{lift:.1f} times more likely** to buy {second} when they buy {first}, " f"compared to random chance.\n" f"=> This is a {'very strong' if lift > 5 else 'moderate' if lift > 2 else 'weak'} association that could be used for cross-sell strategies.\n" ) st.write("-" * 80) st.write(explanation) def results(df: pd.DataFrame, selected_artist: str) -> None: """ Function to output Pandas dataframe to Streamlit app page :param selected_artist: :param df: Pandas dataframe :return: None """ # before, after, support, confidence, lift cols = ["antecedents", "consequents", "support", "confidence", "lift"] st.write("Most useful transactions:") limited_data = df[ (df["artist"] == selected_artist) & (df["lift"] > 2) & (df["confidence"] > 0.5) ][cols].sort_values(by="lift", ascending=False) st.dataframe( limited_data, hide_index=True, column_config={"antecedents": "Before", "consequents": "After"}, use_container_width=True, ) explain_market_basket_rules(limited_data) def intro_text() -> None: """ Function to write text to Streamlit app page :return: """ st.write( '

The three main ways of measuring association are: Support, Confidence and Lift.

', unsafe_allow_html=True, ) st.markdown( "SUPPORT- This is the relative frequency of an item in the given dataset (all the transactions). It represents the popularity of the items, also defined by its proportion of the total items sold. So support 0.5 would mean that out of all transactions 50% contained that item.", unsafe_allow_html=True, ) st.markdown( "CONFIDENCE- This corresponds to the probability of seeing the consequent item within data, given that the data also contains the antecedent item, In other words, it tells how likely it is for one item to be purchased given that another one is purchased. Support(X->Y) / Support(X).", unsafe_allow_html=True, ) st.markdown( "LIFT- This measures how much more often the antecedent and consequent occur together rather than them occurring independently.", unsafe_allow_html=True, ) # st.markdown( # "
  • If Lift score < 1, it means that if X is purchased, it is unlikely that Y will be purchased.", # unsafe_allow_html=True, # ) # st.markdown( # "
  • If Lift score > 1, it means that X is highly associated with Y. In other words, if X is purchased, it is likely that Y will be purchased.", # unsafe_allow_html=True, # ) # st.markdown( # "
  • If Lift score = 1, it means that there is no association between X and Y.

    ", # unsafe_allow_html=True, # )