import pandas as pd
import plotly.express as px
import plotly.io as pio
import plotly.graph_objects as go
pio.templates.default = "plotly_white"
data = pd.read_csv("rfm_data.csv")
print(data.head())
# Filter the data to include only the customers in the Champions segment
champions_segment = data[data['RFM Customer Segments'] == 'Champions']
fig = go.Figure()
fig.add_trace(go.Box(y=champions_segment['RecencyScore'], name='Recency'))
fig.add_trace(go.Box(y=champions_segment['FrequencyScore'], name='Frequency'))
fig.add_trace(go.Box(y=champions_segment['MonetaryScore'], name='Monetary'))
fig.update_layout(title='Distribution of RFM Values within Champions Segment',
yaxis_title='RFM Value',
showlegend=True)
fig.show()
[图表:Champions 细分市场 RFM 值分布]
现在,让我们分析 Champions 细分市场中的 RFM 之间的相关性:
correlation_matrix = champions_segment[['RecencyScore', 'FrequencyScore', 'MonetaryScore']].corr()
# Visualize the correlation matrix using a heatmap
fig_heatmap = go.Figure(data=go.Heatmap(
z=correlation_matrix.values,
x=correlation_matrix.columns,
y=correlation_matrix.columns,
colorscale='RdBu',
colorbar=dict(title='Correlation')))
fig_heatmap.update_layout(title='Correlation Matrix of RFM Values within Champions Segment')
fig_heatmap.show()
[图表:Champions 细分市场 RFM 相关性热力图]
现在让我们来看看所有细分市场的客户数量:
import plotly.colors
pastel_colors = plotly.colors.qualitative.Pastel
segment_counts = data['RFM Customer Segments'].value_counts()
# Create a bar chart to compare segment counts
fig = go.Figure(data=[go.Bar(x=segment_counts.index, y=segment_counts.values,
marker=dict(color=pastel_colors))])
# Set the color of the Champions segment as a different color
champions_color = 'rgb(158, 202, 225)'
fig.update_traces(marker_color=[champions_color if segment == 'Champions'else pastel_colors[i]
for i, segment inenumerate(segment_counts.index)],
marker_line_color='rgb(8, 48, 107)',
marker_line_width=1.5, opacity=0.6)
# Update the layout
fig.update_layout(title='Comparison of RFM Segments',
xaxis_title='RFM Segments',
yaxis_title='Number of Customers',
showlegend=False)
fig.show()
[图表:RFM 细分市场客户数量对比]
现在让我们来看看所有细分市场的 Recency,Frequency 和 Monetary 分数:
# Calculate the average Recency, Frequency, and Monetary scores for each segment
segment_scores = data.groupby('RFM Customer Segments')['RecencyScore', 'FrequencyScore', 'MonetaryScore'].mean().reset_index()
# Create a grouped bar chart to compare segment scores
fig = go.Figure()
# Add bars for Recency score
fig.add_trace(go.Bar(
x=segment_scores['RFM Customer Segments'],
y=segment_scores['RecencyScore'],
name='Recency Score',
marker_color='rgb(158,202,225)'
))
# Add bars for Frequency score
fig.add_trace(go.Bar(
x=segment_scores['RFM Customer Segments'],
y=segment_scores['FrequencyScore'],
name='Frequency Score',
marker_color='rgb(94,158,217)'
))
# Add bars for Monetary score
fig.add_trace(go.Bar(
x=segment_scores['RFM Customer Segments'],
y=segment_scores['MonetaryScore'],
name='Monetary Score',
marker_color='rgb(32,102,148)'
))
# Update the layout
fig.update_layout(
title='Comparison of RFM Segments based on Recency, Frequency, and Monetary Scores',
xaxis_title='RFM Segments',
yaxis_title='Score',
barmode='group',
showlegend=True
)
fig.show()