๐ŸŽ Get the FREE AI Skills Starter Guide โ€” Subscribe โ†’
BytesAgainBytesAgain
๐Ÿฆ€ ClawHub

customer-segment-eng

by @yukirang

Analyze uploaded bank customer data to segment and profile customers by assets, transactions, and behavior, outputting clusters, statistics, and visual charts.

Versionv1.0.0
Downloads345
TERMINAL
clawhub install customer-segment-eng

๐Ÿ“– About This Skill


name: customer-segmentation description: Financial customer segmentation analysis Skill. Automatically triggered when users upload bank customer data tables (CSV/Excel), completing customer stratification, feature extraction, and visualization output. Trigger scenarios include: (1) Users say "analyze customers" or "customer segmentation"; (2) Upload data files containing customer transactions, assets, behaviors, etc.; (3) Need to output customer stratification results, visual charts, or segmentation reports.

Customer Segmentation Skill

Financial customer segmentation analysis: Stratify customers based on assets, transaction behaviors, activity levels, and other dimensions, outputting actionable segmentation results and visualizations.

Workflow

Step 1 โ€” Data Loading and Cleaning

Read user-uploaded CSV or Excel files, automatically identifying column names.

Priority fields to retain:

  • customer_id / ๅฎขๆˆทID โ€” Unique customer identifier
  • age / ๅนด้พ„
  • gender / ๆ€งๅˆซ
  • balance / ่ต„ไบงไฝ™้ข
  • txn_amount / ไบคๆ˜“้‡‘้ข
  • txn_count / ไบคๆ˜“ๆฌกๆ•ฐ
  • last_date / ๆœ€่ฟ‘ไบคๆ˜“ๆ—ฅๆœŸ
  • product_count / ๆŒๆœ‰ไบงๅ“ๆ•ฐ
  • branch / ็ฝ‘็‚น
  • Missing value handling:

  • Numeric: Fill with median
  • Categorical: Fill with mode
  • Columns with >30% missing: Delete and notify user
  • import pandas as pd

    df = pd.read_csv(file_path) df.columns = df.columns.str.strip().str.lower()

    Step 2 โ€” Feature Engineering

    Build RFM + extended features:

    | Feature | Description | |---------|-------------| | Recency | Days since last transaction (smaller = more active) | | Frequency | Transaction frequency (number of transactions in specified period) | | Monetary | Transaction amount (total amount in specified period) | | Tenure | Customer duration (months) | | Product_Depth | Number of products held | | Age | Customer age |

    Data standardization: Use StandardScaler (Z-score) to normalize all numeric features.

    Step 3 โ€” Clustering Analysis

    Use K-Means algorithm, automatically determine K value (Elbow Method, SSE inflection point).

    from sklearn.cluster import KMeans
    from sklearn.preprocessing import StandardScaler

    scaler = StandardScaler() X_scaled = scaler.fit_transform(features)

    Elbow method to find optimal K

    sse = {} for k in range(2, 10): km = KMeans(n_clusters=k, random_state=42, n_init=10) km.fit(X_scaled) sse[k] = km.inertia_ optimal_k = min(sse, key=sse.get) # Simply take k with minimum SSE

    K=5 can also be fixed based on business needs (high/medium-high/medium/medium-low/low value customers).

    Step 4 โ€” Segment Profiling

    Output core statistics for each cluster:

    Cluster 0 (High-Value Customers): Avg. assets 850k, Avg. transaction frequency 28/month, Gender distribution 62% male
    Cluster 1 (Potential Customers): Avg. assets 320k,ๆ˜Žๆ˜พ younger trend
    ...
    

    Recommended label system (five categories):

  • ๐ŸŒŸ High-Value Customers (VIP)
  • โฌ†๏ธ Potential Customers
  • ๐ŸŸข Stable Customers
  • ๐Ÿ”„ Active Transaction Customers
  • โš ๏ธ Dormant/Churn Warning Customers
  • Step 5 โ€” Visualization

    Generate the following charts (saved as PNG):

    1. Customer Asset Distribution Histogram โ€” Asset distribution comparison across levels 2. Radar Chart โ€” Feature comparison across segments 3. Heatmap โ€” Cluster feature mean matrix 4. Scatter Plot โ€” Customer distribution with assets ร— transaction frequency as coordinates

    import matplotlib.pyplot as plt
    import matplotlib
    matplotlib.use('Agg')
    plt.rcParams['font.sans-serif'] = ['WenQuanYi Micro Hei', 'SimHei']

    fig, axes = plt.subplots(1, 2, figsize=(14, 5))

    Asset distribution

    axes[0].hist([g['balance'] for _, g in df.groupby('cluster')], bins=30, label=[f'C{i}' for i in range(k)]) axes[0].set_title('Customer Balance Distribution by Cluster')

    Heatmap

    import seaborn as sns sns.heatmap(cluster_means.T, annot=True, fmt='.1f', ax=axes[1]) axes[1].set_title('Cluster Feature Heatmap') plt.tight_layout() plt.savefig(output_path, dpi=150)

    Step 6 โ€” Output Results

    Output content: 1. Segmentation result table (including customer ID, cluster, segmentation label) โ†’ segmentation_results.csv 2. Cluster feature statistics โ†’ cluster_summary.csv 3. Visualization charts โ†’ segmentation_charts.png 4. Analysis summary (Markdown format) โ†’ segmentation_report.md

    For detailed clustering and parameter documentation:

  • RFM model explanation: Refer to references/rfm-guide.md
  • Clustering parameter explanation: Refer to references/clustering-guide.md