-
Notifications
You must be signed in to change notification settings - Fork 2
#163 Purchase Path Analysis #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mayurkmmt
wants to merge
2
commits into
main
Choose a base branch
from
feature/purchase-path-analysis
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Purchase Path | ||
|
|
||
| ::: pyretailscience.analysis.purchase_path | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| """Module for analyzing customer purchase paths from transaction data. | ||
|
|
||
| This module defines the `purchase_path_analysis` function that tracks | ||
| customer journeys through product categories over time. | ||
| """ | ||
|
|
||
| import ibis | ||
| import pandas as pd | ||
|
|
||
| from pyretailscience.options import ColumnHelper | ||
|
|
||
|
|
||
| def _build_category_group_df( | ||
| first_df: pd.DataFrame, | ||
| category_column: str, | ||
| sort_by_metric: bool, | ||
| multi_category_handling: str, | ||
| ) -> pd.DataFrame: | ||
| """Creates a DataFrame mapping customers to concatenated or individual categories.""" | ||
| if multi_category_handling == "concatenate": | ||
| sort_cols = ["customer_id", "first_basket_number"] | ||
| if sort_by_metric: | ||
| sort_cols.append("metric_value") | ||
| ascending = [True, True, False] | ||
| else: | ||
| sort_cols.append(category_column) | ||
| ascending = [True, True, True] | ||
|
|
||
| return ( | ||
| first_df.sort_values(sort_cols, ascending=ascending) | ||
| .groupby(["customer_id", "first_basket_number"])[category_column] | ||
| .apply(lambda x: ",".join(str(v) for v in x)) | ||
| .reset_index() | ||
| .rename(columns={category_column: "categories"}) | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return first_df[["customer_id", "first_basket_number", category_column]].rename( | ||
| columns={category_column: "categories"}, | ||
| ) | ||
|
|
||
|
|
||
| def _build_paths_df(category_groups_df: pd.DataFrame) -> pd.DataFrame: | ||
| """Constructs a pivoted DataFrame representing customer purchase paths.""" | ||
| actual_baskets = sorted(category_groups_df["first_basket_number"].unique()) if not category_groups_df.empty else [] | ||
| paths_df = category_groups_df.pivot_table( | ||
| index="customer_id", | ||
| columns="first_basket_number", | ||
| values="categories", | ||
| aggfunc="first", | ||
| ).reset_index() | ||
|
|
||
| column_mapping = {"customer_id": "customer_id"} | ||
| for i, basket_num in enumerate(sorted(actual_baskets), 1): | ||
| if basket_num in paths_df.columns: | ||
| column_mapping[basket_num] = f"basket_{i}" | ||
| return paths_df.rename(columns=column_mapping).fillna("") | ||
|
|
||
|
|
||
| def purchase_path_analysis( | ||
| transactions_df: pd.DataFrame, | ||
| category_column: str = "product_category", | ||
| min_transactions: int = 3, | ||
| min_basket_size: int = 2, | ||
| min_basket_value: float = 10.0, | ||
| max_depth: int = 10, | ||
| min_customers: int = 5, | ||
| exclude_negative_revenue: bool = True, | ||
| multi_category_handling: str = "concatenate", | ||
| sort_by: str = "alphabetical", | ||
| aggregation_column: str | None = None, | ||
| aggregation_function: str = "sum", | ||
| ) -> pd.DataFrame: | ||
|
murray-ds marked this conversation as resolved.
|
||
| """Analyzes customer purchase paths through product categories over time.""" | ||
| cols = ColumnHelper() | ||
| required_cols = [cols.customer_id, cols.transaction_id, cols.transaction_date, "product_id", category_column] | ||
|
murray-ds marked this conversation as resolved.
|
||
| if exclude_negative_revenue: | ||
| required_cols.append("revenue") | ||
| missing_cols = set(required_cols) - set(transactions_df.columns) | ||
| if missing_cols: | ||
| msg = f"The following columns are required but missing: {missing_cols}" | ||
| raise ValueError(msg) | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| transactions_table = ( | ||
| ibis.memtable(transactions_df) if isinstance(transactions_df, pd.DataFrame) else transactions_df | ||
| ) | ||
| if exclude_negative_revenue: | ||
| transactions_table = transactions_table.filter(transactions_table.revenue > 0) | ||
|
|
||
| customer_baskets = ( | ||
| transactions_table.group_by(["customer_id", "transaction_id", "transaction_date"]) | ||
| .aggregate( | ||
| item_count=ibis._.product_id.nunique(), | ||
| basket_value=ibis._.revenue.sum(), | ||
| ) | ||
| .filter( | ||
| (ibis._.item_count >= min_basket_size) & (ibis._.basket_value >= min_basket_value), | ||
| ) | ||
| .mutate( | ||
| basket_number=ibis.row_number().over( | ||
| ibis.window(group_by="customer_id", order_by="transaction_date"), | ||
| ), | ||
| ) | ||
| .filter(ibis._.basket_number <= max_depth) | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| eligible_customers = ( | ||
| customer_baskets.group_by("customer_id") | ||
| .aggregate(transaction_count=ibis._.basket_number.count()) | ||
| .filter(ibis._.transaction_count >= min_transactions) | ||
| .select("customer_id") | ||
| ) | ||
|
|
||
| transactions_with_baskets = transactions_table.inner_join( | ||
| customer_baskets.inner_join(eligible_customers, "customer_id").select( | ||
| ["customer_id", "transaction_id", "basket_number"], | ||
| ), | ||
| ["customer_id", "transaction_id"], | ||
| ) | ||
|
|
||
| use_agg_sort = ( | ||
| multi_category_handling == "concatenate" | ||
| and sort_by == "aggregation" | ||
| and aggregation_column | ||
| and aggregation_function | ||
| ) | ||
|
|
||
| agg_func_map = { | ||
| "sum": "sum", | ||
| "max": "max", | ||
| "min": "min", | ||
| "avg": "mean", | ||
| } | ||
|
|
||
| if use_agg_sort: | ||
| agg_method = agg_func_map.get(aggregation_function) | ||
| if not agg_method: | ||
| msg = f"Unsupported aggregation function: {aggregation_function}" | ||
| raise ValueError(msg) | ||
| agg_func = getattr(transactions_with_baskets[aggregation_column], agg_method) | ||
| first_df = transactions_with_baskets.group_by(["customer_id", category_column]).aggregate( | ||
| first_basket_number=ibis._.basket_number.min(), | ||
| metric_value=agg_func(), | ||
| ) | ||
| else: | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| first_df = transactions_with_baskets.group_by(["customer_id", category_column]).aggregate( | ||
| first_basket_number=ibis._.basket_number.min(), | ||
| ) | ||
| first_df = first_df.execute() | ||
|
|
||
| if first_df.empty: | ||
| return pd.DataFrame(columns=["customer_count", "transition_probability"]) | ||
|
|
||
|
Comment on lines
+148
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix inconsistent empty DataFrame returns. The function returns DataFrames with different column structures when empty (lines 139 vs 161). Standardize empty DataFrame structure: - if first_df.empty:
- return pd.DataFrame(columns=["customer_count", "transition_probability"])
+ # Define expected columns at function start
+ base_columns = ["customer_count", "transition_probability"]
+
+ if first_df.empty:
+ return pd.DataFrame(columns=base_columns)
# ... later in the function ...
- return pd.DataFrame(columns=[*basket_cols, "customer_count", "transition_probability"])
+ if not pattern_counts.empty:
+ # existing logic
+ else:
+ # Return consistent empty structure
+ return pd.DataFrame(columns=base_columns)Also applies to: 161-161 🤖 Prompt for AI Agents |
||
| category_groups_df = _build_category_group_df(first_df, category_column, use_agg_sort, multi_category_handling) | ||
| paths_df = _build_paths_df(category_groups_df) | ||
|
|
||
| basket_cols = sorted( | ||
| [col for col in paths_df.columns if col.startswith("basket_")], | ||
| key=lambda x: int(x.split("_")[1]), | ||
| ) | ||
| paths_df = paths_df[paths_df[basket_cols].ne("").any(axis=1)] | ||
|
|
||
| if paths_df.empty: | ||
| return pd.DataFrame(columns=["customer_count", "transition_probability"]) | ||
|
|
||
| pattern_counts = paths_df.groupby(basket_cols).size().reset_index(name="customer_count") | ||
| pattern_counts = pattern_counts[pattern_counts.customer_count >= min_customers] | ||
|
|
||
| if not pattern_counts.empty: | ||
| total_customers = pattern_counts.customer_count.sum() | ||
| pattern_counts["transition_probability"] = (pattern_counts.customer_count / total_customers).round(3) | ||
| return pattern_counts.sort_values("customer_count", ascending=False).reset_index(drop=True) | ||
|
|
||
| return pd.DataFrame(columns=[*basket_cols, "customer_count", "transition_probability"]) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.