mgbam commited on
Commit
18347b1
·
verified ·
1 Parent(s): e0d9e98

Delete odules/profiling.py

Browse files
Files changed (1) hide show
  1. odules/profiling.py +0 -86
odules/profiling.py DELETED
@@ -1,86 +0,0 @@
1
- # modules/profiling.py
2
-
3
- # -*- coding: utf-8 -*-
4
- #
5
- # PROJECT: CognitiveEDA v5.7 - The QuantumLeap Intelligence Platform
6
- #
7
- # DESCRIPTION: A dedicated module for profiling and characterizing customer
8
- # segments identified through clustering.
9
-
10
- import pandas as pd
11
- import plotly.express as px
12
- import plotly.graph_objects as go
13
- import logging
14
-
15
- def profile_clusters(df: pd.DataFrame, cluster_labels: pd.Series, numeric_cols: list, cat_cols: list) -> tuple:
16
- """
17
- Analyzes and profiles clusters to create meaningful business personas.
18
-
19
- This function groups the data by cluster and calculates key statistics
20
- for numeric and categorical features to describe each segment. It then
21
- visualizes these differences.
22
-
23
- Args:
24
- df (pd.DataFrame): The feature-engineered DataFrame.
25
- cluster_labels (pd.Series): The series of cluster labels from the K-Means model.
26
- numeric_cols (list): List of numeric columns to profile (e.g., ['Total_Revenue']).
27
- cat_cols (list): List of categorical columns to profile (e.g., ['City', 'Product']).
28
-
29
- Returns:
30
- A tuple containing:
31
- - A markdown string with the detailed profile of each cluster.
32
- - A Plotly Figure visualizing the differences between clusters.
33
- """
34
- if df.empty or cluster_labels.empty:
35
- return "No data to profile.", go.Figure()
36
-
37
- logging.info(f"Profiling {cluster_labels.nunique()} clusters...")
38
-
39
- profile_df = df.copy()
40
- profile_df['Cluster'] = cluster_labels
41
-
42
- # --- Generate Markdown Report ---
43
- report_md = "### Cluster Persona Analysis\n\n"
44
-
45
- # Analyze numeric features by cluster
46
- numeric_profile = profile_df.groupby('Cluster')[numeric_cols].mean().round(2)
47
-
48
- # Analyze categorical features by cluster (get the most frequent value - mode)
49
- cat_profile_list = []
50
- for col in cat_cols:
51
- mode_series = profile_df.groupby('Cluster')[col].apply(lambda x: x.mode().iloc[0])
52
- mode_df = mode_series.to_frame()
53
- cat_profile_list.append(mode_df)
54
-
55
- full_profile = pd.concat([numeric_profile] + cat_profile_list, axis=1)
56
-
57
- for cluster_id in sorted(profile_df['Cluster'].unique()):
58
- report_md += f"#### Cluster {cluster_id}: The '{full_profile.loc[cluster_id, 'City']}' Persona\n"
59
-
60
- # Numeric Summary
61
- for col in numeric_cols:
62
- val = full_profile.loc[cluster_id, col]
63
- report_md += f"- **Avg. {col.replace('_', ' ')}:** `{val:,.2f}`\n"
64
-
65
- # Categorical Summary
66
- for col in cat_cols:
67
- val = full_profile.loc[cluster_id, col]
68
- report_md += f"- **Dominant {col}:** `{val}`\n"
69
- report_md += "\n"
70
-
71
- # --- Generate Visualization ---
72
- # We'll visualize the average 'Total_Revenue' by 'City' for each cluster
73
- # This directly tests our hypothesis that 'City' is the dominant feature.
74
- vis_df = profile_df.groupby(['Cluster', 'City'])['Total_Revenue'].mean().reset_index()
75
-
76
- fig = px.bar(
77
- vis_df,
78
- x='Cluster',
79
- y='Total_Revenue',
80
- color='City',
81
- barmode='group',
82
- title='<b>Cluster Profile: Avg. Total Revenue by City</b>',
83
- labels={'Total_Revenue': 'Average Total Revenue ($)', 'Cluster': 'Customer Segment'}
84
- )
85
-
86
- return report_md, fig