You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于RFM分析绘制7类客户分群的3D聚类图?

Hey there! Since you've already segmented your customers into 7 RFM groups like "不可流失客户", "忠诚客户", "冠军客户", etc., we can tweak your Plotly code to visualize these 7 clusters clearly. Let's cover two common scenarios based on your needs:

Scenario 1: Visualize your pre-defined RFM segments

If you already have a column in your dataset (let's call it rfm_segment) that stores the 7 group names, you can directly use this column to color your 3D plot. This ensures the visualization matches your existing segmentation work:

import plotly.graph_objs as go
import plotly as py

# Assuming your dataset has a 'rfm_segment' column with the 7 group names
trace1 = go.Scatter3d(
    x= data['recency'],
    y= data['frequency'],
    z= data['monetary'],
    mode='markers',
    text=data['rfm_segment'],  # Show segment name on hover
    marker=dict(
        color=data['rfm_segment'],  # Color by pre-defined segment
        colorscale='Viridis',  # Distinct color scale for 7 groups
        size=5,
        line=dict(
            width=0.5
        ),
        opacity=0.7
    )
)

data_temp = [trace1]
layout = go.Layout(
    title= 'RFM Customer Segmentation - 7 Pre-defined Groups',
    scene = dict(
        xaxis = dict(title = 'Recency (R)'),
        yaxis = dict(title = 'Frequency (F)'),
        zaxis = dict(title = 'Monetary (M)')
    ),
    hovermode='closest'
)

fig = go.Figure(data=data_temp, layout=layout)
py.offline.iplot(fig)

Key tweaks here:

  • Uses your existing rfm_segment column for coloring instead of KMeans labels
  • Adds hover text to display the segment name when you mouse over a point
  • Uses a distinct color scale (Viridis) to make 7 groups easy to tell apart
  • Adjusted marker size and opacity for better readability

Scenario 2: Generate 7 clusters via KMeans (if you need to cluster from scratch)

If you want to create 7 clusters directly using KMeans on your R quartile data, here's how to modify your original code:

import plotly.graph_objs as go
import plotly as py
from sklearn.cluster import KMeans  # Don't forget to import KMeans!

# Use R/F/M quartile data for clustering
x3 = data[['R_Quartile','F_Quartile','M_Quartile']].values

# Initialize KMeans with 7 clusters
algorithm = KMeans(
    n_clusters=7,
    init='k-means++',
    n_init=10,
    max_iter=300,
    tol=0.0001,
    random_state=111,
    algorithm='elkan'
)
algorithm.fit(x3)
labels7 = algorithm.labels_
data['label7'] = labels7

trace1 = go.Scatter3d(
    x= data['recency'],
    y= data['frequency'],
    z= data['monetary'],
    mode='markers',
    text=data['label7'],  # Show cluster number on hover
    marker=dict(
        color=data['label7'],
        colorscale='Plasma',  # High-contrast scale for 7 clusters
        size=5,
        line=dict(
            width=0.5
        ),
        opacity=0.7
    )
)

data_temp = [trace1]
layout = go.Layout(
    title= 'RFM Customer Segmentation - 7 KMeans Clusters',
    scene = dict(
        xaxis = dict(title = 'Recency (R)'),
        yaxis = dict(title = 'Frequency (F)'),
        zaxis = dict(title = 'Monetary (M)')
    ),
    hovermode='closest'
)

fig = go.Figure(data=data_temp, layout=layout)
py.offline.iplot(fig)

Key changes from your original code:

  • Updated n_clusters=3 to n_clusters=7 in KMeans
  • Renamed variables (e.g., labels3 → labels7) for clarity
  • Switched to a high-contrast color scale (Plasma) to distinguish 7 clusters easily
  • Added hover text to show cluster numbers
  • Increased marker size and reduced line width for a cleaner look

Extra Tips:

  • If you want custom colors for your pre-defined segments, create a color mapping dictionary:
    color_map = {
        "冠军客户": "#FF5733",
        "忠诚客户": "#33FF57",
        "不可流失客户": "#3357FF",
        # Add colors for the other 4 groups here
    }
    # Then use it in the marker: color=data['rfm_segment'].map(color_map)
    
  • If your dataset is large, try reducing marker size or using opacity=0.5 to avoid overcrowding the plot
  • You can add a legend by setting showlegend=True in the marker dict, but for 7 groups, it's usually better to rely on hover text for clarity

内容的提问来源于stack exchange,提问作者Mete Ceviz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 07:02:29