1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import plotly.express as px import plotly.graph_objects as go from plotly.subplots import make_subplots
fig = make_subplots( rows=2, cols=2, subplot_titles=('用户活跃时段热力图', '商品类别消费分布', '用户分群3D视图', 'RFM分群雷达图'), specs=[[{'type': 'heatmap'}, {'type': 'sunburst'}], [{'type': 'scatter3d'}, {'type': 'scatterpolar'}]] )
hour_week_heatmap = behaviors_clean.groupby(['day_of_week', 'hour']).size().unstack(fill_value=0) fig.add_trace( go.Heatmap( z=hour_week_heatmap.values, x=hour_week_heatmap.columns, y=['周一', '周二', '周三', '周四', '周五', '周六', '周日'], colorscale='Viridis', showscale=True ), row=1, col=1 )
category_spent = behaviors_clean.groupby(['product_category', 'behavior_type'])['price'].sum().reset_index() fig.add_trace( go.Sunburst( labels=list(category_spent['product_category']) + list(category_spent['behavior_type']), parents=[''] * len(category_spent['product_category']) + list(category_spent['product_category']), values=list(category_spent['price']) * 2, branchvalues="total" ), row=1, col=2 )
fig.add_trace( go.Scatter3d( x=user_features['behavior_count'], y=user_features['total_spent'], z=user_features['active_days'], mode='markers', marker=dict( size=5, color=user_features['cluster'], colorscale='Viridis', opacity=0.8 ), text=user_features['cluster_name'] ), row=2, col=1 )
fig.add_trace( go.Scatterpolar( r=[3, 2, 4, 1], theta=['近期消费', '消费频率', '消费金额', '活跃度'], fill='toself' ), row=2, col=2 )
fig.update_layout(height=800, showlegend=False, title_text="电商用户行为分析仪表板") fig.show()
|