-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemp_cells_a.json
More file actions
1 lines (1 loc) · 3.82 KB
/
Copy pathtemp_cells_a.json
File metadata and controls
1 lines (1 loc) · 3.82 KB
1
[{"cell_type": "markdown", "metadata": {}, "source": ["# 方案A: 线性规划模型 - 可行域分析法", "", "## 问题本质", "这是一个**逆问题(Inverse Problem)**:", "- 已知: 评委分、淘汰结果", "- 求解: 粉丝投票的**可行范围**", "", "## 核心方法", "1. 用线性规划求每个选手粉丝投票的**上下界**", "2. 用范围宽度量化**不确定性**", "3. **确定性 = 1 - 范围宽度/100**"]}, {"cell_type": "markdown", "metadata": {}, "source": ["## 1. 环境配置"]}, {"cell_type": "code", "metadata": {}, "source": ["import pandas as pd", "import numpy as np", "from scipy.optimize import linprog", "from scipy import stats", "import matplotlib.pyplot as plt", "import warnings", "warnings.filterwarnings(\"ignore\")", "", "plt.rcParams[\"font.sans-serif\"] = [\"SimHei\", \"DejaVu Sans\"]", "plt.rcParams[\"axes.unicode_minus\"] = False", "plt.style.use(\"seaborn-v0_8-whitegrid\")", "COLORS = [\"#E64B35\", \"#4DBBD5\", \"#00A087\", \"#3C5488\", \"#F39B7F\"]", "print(\"环境配置完成\")"], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["## 2. 加载数据"]}, {"cell_type": "code", "metadata": {}, "source": ["df = pd.read_excel(\"../../data/processed/粉丝投票分析.xlsx\")", "print(f\"数据维度: {df.shape}\")", "print(f\"赛季范围: S{df['赛季'].min()} - S{df['赛季'].max()}\")"], "outputs": [], "execution_count": null}, {"cell_type": "markdown", "metadata": {}, "source": ["## 3. 核心算法: 可行域边界求解"]}, {"cell_type": "code", "metadata": {}, "source": ["def get_scoring_method(season):", " if season <= 2:", " return \"ranking_early\"", " elif season <= 27:", " return \"percentage\"", " else:", " return \"ranking_with_save\"", "", "def compute_feasible_bounds(judge_pct, elim_idx, surv_idx, n):", " epsilon = 0.01", " A_eq = np.ones((1, n))", " b_eq = np.array([100])", " ", " A_ub, b_ub = [], []", " for e in elim_idx:", " for s in surv_idx:", " row = np.zeros(n)", " row[e], row[s] = 1, -1", " A_ub.append(row)", " b_ub.append(judge_pct[s] - judge_pct[e] - epsilon)", " ", " A_ub = np.array(A_ub) if A_ub else None", " b_ub = np.array(b_ub) if b_ub else None", " bounds = [(0, 100) for _ in range(n)]", " ", " results = []", " for i in range(n):", " c_min, c_max = np.zeros(n), np.zeros(n)", " c_min[i], c_max[i] = 1, -1", " res_min = linprog(c_min, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method=\"highs\")", " res_max = linprog(c_max, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method=\"highs\")", " if res_min.success and res_max.success:", " results.append((res_min.fun, -res_max.fun))", " else:", " results.append((np.nan, np.nan))", " return results", "", "def analyze_week(df, season, week):", " week_df = df[(df[\"赛季\"] == season) & (df[\"第几周\"] == week) & (df[\"本周评委总分\"] > 0)]", " if len(week_df) == 0:", " return None", " ", " contestants = week_df[\"选手姓名\"].tolist()", " judge_pct = week_df[\"评委百分比\"].values", " eliminated = week_df[week_df[\"是否被淘汰\"] == 1][\"选手姓名\"].tolist()", " n = len(contestants)", " ", " if len(eliminated) == 0:", " return None", " ", " elim_idx = [contestants.index(e) for e in eliminated if e in contestants]", " surv_idx = [i for i in range(n) if i not in elim_idx]", " bounds = compute_feasible_bounds(judge_pct, elim_idx, surv_idx, n)", " ", " return {\"season\": season, \"week\": week, \"contestants\": contestants,", " \"judge_pct\": judge_pct, \"eliminated\": eliminated, \"bounds\": bounds}", "", "print(\"核心函数定义完成\")"], "outputs": [], "execution_count": null}]