Просмотр исходного кода

feat(渠道效果分析): 添加层级目录,修复进入推荐率矩阵

- 添加带层级的目录(一、二、三、四级标题)
- 修复进入推荐率矩阵数据为空的问题(pivot索引顺序错误)
- 重新组织章节结构:渠道整体、一级品类、二级品类、趋势分布

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
yangxiaohui 2 месяцев назад
Родитель
Сommit
e643722868
1 измененных файлов с 62 добавлено и 23 удалено
  1. 62 23
      tasks/渠道效果分析/visualize.py

+ 62 - 23
tasks/渠道效果分析/visualize.py

@@ -136,8 +136,8 @@ for cat in valid_categories:
             cells.append("<td>-</td>")
     uv_rows.append("<tr>" + "".join(cells) + "</tr>")
 
-# 4. 进入推荐率热力图
-pivot_recommend = df.groupby(['channel', 'merge一级品类']).apply(
+# 4. 进入推荐率热力图(按品类×渠道)
+pivot_recommend = df.groupby(['merge一级品类', 'channel']).apply(
     lambda x: (x['进入推荐率'] * x['点击uv']).sum() / x['点击uv'].sum() if x['点击uv'].sum() > 0 else 0,
     include_groups=False
 ).unstack()
@@ -165,9 +165,11 @@ def get_recommend_color(val):
 
 recommend_rows = []
 for cat in valid_categories:
+    if cat not in pivot_recommend.index:
+        continue
     cells = [f"<td>{str(cat)[:12]}</td>"]
     for ch in heatmap_cols:
-        if ch in pivot_recommend.columns and cat in pivot_recommend.index and pd.notna(pivot_recommend.loc[cat, ch]):
+        if ch in pivot_recommend.columns and pd.notna(pivot_recommend.loc[cat, ch]):
             val = pivot_recommend.loc[cat, ch]
             style = get_recommend_color(val)
             cells.append(f'<td style="{style}">{val:.4f}</td>')
@@ -286,7 +288,8 @@ html_content = f"""<!DOCTYPE html>
                margin: 20px; background: #f5f5f5; }}
         .container {{ max-width: 1400px; margin: 0 auto; }}
         h1 {{ color: #333; border-bottom: 2px solid #007bff; padding-bottom: 10px; }}
-        h2 {{ color: #555; margin-top: 30px; }}
+        h2 {{ color: #444; margin-top: 30px; padding-top: 20px; border-top: 1px solid #ddd; }}
+        h3 {{ color: #555; margin-top: 20px; }}
         .chart-container {{ background: white; padding: 20px; margin: 20px 0;
                            border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }}
         .summary {{ background: white; padding: 20px; margin: 20px 0;
@@ -294,7 +297,7 @@ html_content = f"""<!DOCTYPE html>
         .stat-card {{ flex: 1; min-width: 150px; padding: 15px;
                      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                      border-radius: 8px; color: white; text-align: center; }}
-        .stat-card h3 {{ margin: 0; font-size: 24px; }}
+        .stat-card h4 {{ margin: 0; font-size: 24px; }}
         .stat-card p {{ margin: 5px 0 0; opacity: 0.9; }}
         table {{ width: 100%; border-collapse: collapse; margin: 10px 0; }}
         th, td {{ padding: 8px 12px; text-align: left; border-bottom: 1px solid #ddd; }}
@@ -303,13 +306,16 @@ html_content = f"""<!DOCTYPE html>
         .heatmap {{ overflow-x: auto; }}
         .heatmap table {{ font-size: 12px; }}
         .heatmap td {{ text-align: center; min-width: 80px; }}
-        .high {{ background: #28a745; color: white; }}
-        .medium {{ background: #ffc107; }}
-        .low {{ background: #dc3545; color: white; }}
         canvas {{ max-height: 400px; }}
         .matrix-section {{ margin-bottom: 40px; }}
-        .legend {{ font-size: 12px; margin: 10px 0; }}
-        .legend span {{ padding: 2px 8px; margin-right: 10px; border-radius: 3px; }}
+        .legend {{ font-size: 12px; margin: 10px 0; color: #666; }}
+        .toc {{ background: white; padding: 20px; margin: 20px 0; border-radius: 8px; }}
+        .toc h2 {{ margin-top: 0; border: none; padding: 0; }}
+        .toc ul {{ margin: 0; padding-left: 20px; }}
+        .toc li {{ margin: 5px 0; }}
+        .toc a {{ color: #007bff; text-decoration: none; }}
+        .toc a:hover {{ text-decoration: underline; }}
+        .toc .sub {{ padding-left: 20px; }}
     </style>
 </head>
 <body>
@@ -319,24 +325,51 @@ html_content = f"""<!DOCTYPE html>
 
         <div class="summary">
             <div class="stat-card">
-                <h3>{total_uv:,}</h3>
+                <h4>{total_uv:,}</h4>
                 <p>总点击UV</p>
             </div>
             <div class="stat-card">
-                <h3>{avg_ror:.4f}</h3>
+                <h4>{avg_ror:.4f}</h4>
                 <p>平均回流率</p>
             </div>
             <div class="stat-card">
-                <h3>{channel_count}</h3>
+                <h4>{channel_count}</h4>
                 <p>渠道数</p>
             </div>
             <div class="stat-card">
-                <h3>{category_count}</h3>
+                <h4>{category_count}</h4>
                 <p>品类数</p>
             </div>
         </div>
 
-        <h2>1. 渠道整体表现</h2>
+        <div class="toc">
+            <h2>目录</h2>
+            <ul>
+                <li><a href="#sec1">一、渠道整体表现</a></li>
+                <li><a href="#sec2">二、一级品类分析</a>
+                    <ul class="sub">
+                        <li><a href="#sec2-1">2.1 回流率矩阵</a></li>
+                        <li><a href="#sec2-2">2.2 点击UV矩阵</a></li>
+                        <li><a href="#sec2-3">2.3 进入推荐率矩阵</a></li>
+                    </ul>
+                </li>
+                <li><a href="#sec3">三、二级品类分析</a>
+                    <ul class="sub">
+                        <li><a href="#sec3-1">3.1 回流率矩阵</a></li>
+                        <li><a href="#sec3-2">3.2 点击UV矩阵</a></li>
+                    </ul>
+                </li>
+                <li><a href="#sec4">四、趋势与分布</a>
+                    <ul class="sub">
+                        <li><a href="#sec4-1">4.1 渠道占比 vs 回流率</a></li>
+                        <li><a href="#sec4-2">4.2 每日回流率趋势</a></li>
+                        <li><a href="#sec4-3">4.3 UV vs 回流率散点</a></li>
+                    </ul>
+                </li>
+            </ul>
+        </div>
+
+        <h2 id="sec1">一、渠道整体表现</h2>
         <div class="chart-container">
             <table>
                 <tr><th>渠道</th><th>点击UV</th><th>回流率</th><th>可视化</th></tr>
@@ -344,7 +377,9 @@ html_content = f"""<!DOCTYPE html>
             </table>
         </div>
 
-        <h2>2. 渠道×品类 回流率矩阵</h2>
+        <h2 id="sec2">二、一级品类分析</h2>
+
+        <h3 id="sec2-1">2.1 回流率矩阵</h3>
         <div class="chart-container heatmap matrix-section">
             <div class="legend">颜色越深=回流率越高(白→绿渐变,max=0.50)</div>
             <table>
@@ -353,7 +388,7 @@ html_content = f"""<!DOCTYPE html>
             </table>
         </div>
 
-        <h2>3. 渠道×品类 点击UV矩阵</h2>
+        <h3 id="sec2-2">2.2 点击UV矩阵</h3>
         <div class="chart-container heatmap matrix-section">
             <div class="legend">颜色越深=UV越高(白→蓝渐变,max=10万)</div>
             <table>
@@ -362,7 +397,7 @@ html_content = f"""<!DOCTYPE html>
             </table>
         </div>
 
-        <h2>4. 渠道×品类 进入推荐率矩阵</h2>
+        <h3 id="sec2-3">2.3 进入推荐率矩阵</h3>
         <div class="chart-container heatmap matrix-section">
             <div class="legend">颜色:红(&lt;0.70) → 黄(0.70-0.80) → 绿(&gt;0.80)</div>
             <table>
@@ -371,7 +406,9 @@ html_content = f"""<!DOCTYPE html>
             </table>
         </div>
 
-        <h2>5. 渠道×二级品类 回流率矩阵</h2>
+        <h2 id="sec3">三、二级品类分析</h2>
+
+        <h3 id="sec3-1">3.1 回流率矩阵</h3>
         <div class="chart-container heatmap matrix-section">
             <div class="legend">颜色越深=回流率越高(白→绿渐变,max=0.50)</div>
             <table>
@@ -380,7 +417,7 @@ html_content = f"""<!DOCTYPE html>
             </table>
         </div>
 
-        <h2>6. 渠道×二级品类 点击UV矩阵</h2>
+        <h3 id="sec3-2">3.2 点击UV矩阵</h3>
         <div class="chart-container heatmap matrix-section">
             <div class="legend">颜色越深=UV越高(白→蓝渐变,max=10万)</div>
             <table>
@@ -389,18 +426,20 @@ html_content = f"""<!DOCTYPE html>
             </table>
         </div>
 
-        <h2>7. 渠道占比 vs 回流率(气泡图)</h2>
+        <h2 id="sec4">四、趋势与分布</h2>
+
+        <h3 id="sec4-1">4.1 渠道占比 vs 回流率(气泡图)</h3>
         <div class="chart-container">
             <canvas id="bubbleChart"></canvas>
             <p style="font-size:12px;color:#666;margin-top:10px;">X轴=UV占比,Y轴=回流率,气泡大小=UV量级</p>
         </div>
 
-        <h2>8. 每日回流率趋势</h2>
+        <h3 id="sec4-2">4.2 每日回流率趋势</h3>
         <div class="chart-container">
             <canvas id="trendChart"></canvas>
         </div>
 
-        <h2>9. UV vs 回流率 散点分布</h2>
+        <h3 id="sec4-3">4.3 UV vs 回流率 散点分布</h3>
         <div class="chart-container">
             <canvas id="scatterChart"></canvas>
         </div>