python案例统计伤病停赛影响数据对比?

wen python案例 11

足球伤病停赛影响数据对比分析案例

下面是一个完整的Python案例,用于统计和对比球队在有伤病/停赛球员与无伤病/停赛球员情况下的比赛表现差异。

python案例统计伤病停赛影响数据对比?

场景说明

分析目标:对比某支球队在核心球员缺阵(伤病/停赛)与全员出战时的战绩差异,量化影响程度。

完整代码

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['SimHei']  # 中文显示
matplotlib.rcParams['axes.unicode_minus'] = False
# ============ 1. 构造模拟数据 ============
np.random.seed(42)
# 假设一个赛季38场比赛
n_matches = 38
data = {
    'match_id': range(1, n_matches + 1),
    'opponent': [f'球队{i}' for i in np.random.randint(1, 20, n_matches)],
    'is_home': np.random.choice([0, 1], n_matches),
    # 缺阵人数(0-4人)
    'absent_players': np.random.choice([0, 0, 0, 1, 1, 2, 2, 3, 4], n_matches),
    # 缺阵中是否含核心球员
    'key_absent': np.random.choice([0, 1], n_matches, p=[0.7, 0.3]),
    # 进球数
    'goals_for': np.random.poisson(1.5, n_matches),
    # 失球数
    'goals_against': np.random.poisson(1.2, n_matches),
}
df = pd.DataFrame(data)
# 根据缺阵情况调整进球/失球(模拟真实影响)
penalty = df['absent_players'] * 0.15 + df['key_absent'] * 0.3
df['goals_for'] = np.maximum(0, (df['goals_for'] - penalty).round()).astype(int)
df['goals_against'] = (df['goals_against'] + penalty * 0.8).round().astype(int)
# 计算比赛结果
def get_result(row):
    if row['goals_for'] > row['goals_against']:
        return '胜'
    elif row['goals_for'] == row['goals_against']:
        return '平'
    else:
        return '负'
df['result'] = df.apply(get_result, axis=1)
df['points'] = df['result'].map({'胜': 3, '平': 1, '负': 0})
# 划分:有无伤病缺阵
df['has_absence'] = (df['absent_players'] > 0).astype(int)
print("=" * 60)
print("原始数据预览:")
print(df.head(10).to_string(index=False))
# ============ 2. 分组统计对比 ============
print("\n" + "=" * 60)
print("【核心对比:有伤病/停赛 vs 无伤病/停赛】")
def summarize(group_df):
    return pd.Series({
        '场次': len(group_df),
        '胜': (group_df['result'] == '胜').sum(),
        '平': (group_df['result'] == '平').sum(),
        '负': (group_df['result'] == '负').sum(),
        '胜率(%)': round((group_df['result'] == '胜').mean() * 100, 1),
        '场均积分': round(group_df['points'].mean(), 2),
        '场均进球': round(group_df['goals_for'].mean(), 2),
        '场均失球': round(group_df['goals_against'].mean(), 2),
    })
compare = df.groupby('has_absence').apply(summarize).rename(
    index={0: '全员出战', 1: '有伤员/停赛'}
)
print(compare.to_string())
# ============ 3. 按缺阵人数细分 ============
print("\n" + "=" * 60)
print("【按缺阵人数细分】")
by_count = df.groupby('absent_players').apply(summarize)
by_count.index.name = '缺阵人数'
print(by_count.to_string())
# ============ 4. 核心球员缺阵影响 ============
print("\n" + "=" * 60)
print("【核心球员缺阵的影响】")
key_compare = df.groupby('key_absent').apply(summarize).rename(
    index={0: '核心齐整', 1: '核心缺阵'}
)
print(key_compare.to_string())
# 计算影响差值
impact_points = key_compare.loc['核心齐整', '场均积分'] - key_compare.loc['核心缺阵', '场均积分']
impact_goals = key_compare.loc['核心齐整', '场均进球'] - key_compare.loc['核心缺阵', '场均进球']
impact_conceded = key_compare.loc['核心缺阵', '场均失球'] - key_compare.loc['核心齐整', '场均失球']
print(f"\n>>> 核心缺阵导致:场均积分下降 {impact_points:.2f} 分,"
      f"场均进球减少 {impact_goals:.2f} 个,场均失球增加 {impact_conceded:.2f} 个")
# ============ 5. 可视化对比 ============
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
# 图1:胜平负分布对比
result_compare = df.groupby(['has_absence', 'result']).size().unstack(fill_value=0)
result_compare.index = ['全员出战', '有伤员/停赛']
result_compare = result_compare[['胜', '平', '负']]
result_compare.plot(kind='bar', ax=axes[0, 0], color=['#2ecc71', '#f1c40f', '#e74c3c'])
axes[0, 0].set_title('胜平负场次对比')
axes[0, 0].set_ylabel('场次')
axes[0, 0].tick_params(axis='x', rotation=0)
axes[0, 0].legend(title='结果')
# 图2:场均积分对比
compare[['场均积分']].plot(kind='bar', ax=axes[0, 1], color='#3498db', legend=False)
axes[0, 1].set_title('场均积分对比')
axes[0, 1].set_ylabel('场均积分')
axes[0, 1].tick_params(axis='x', rotation=0)
axes[0, 1].set_xticklabels(['全员出战', '有伤员/停赛'])
# 图3:场均进失球对比
goal_compare = compare[['场均进球', '场均失球']]
goal_compare.plot(kind='bar', ax=axes[1, 0], color=['#27ae60', '#e67e22'])
axes[1, 0].set_title('场均进/失球对比')
axes[1, 0].set_ylabel('球数')
axes[1, 0].tick_params(axis='x', rotation=0)
axes[1, 0].set_xticklabels(['全员出战', '有伤员/停赛'])
# 图4:缺阵人数与场均积分关系
axes[1, 1].plot(by_count.index, by_count['场均积分'], marker='o', color='#9b59b6', linewidth=2)
axes[1, 1].set_title('缺阵人数与场均积分关系')
axes[1, 1].set_xlabel('缺阵人数')
axes[1, 1].set_ylabel('场均积分')
axes[1, 1].grid(alpha=0.3)
plt.tight_layout()
plt.savefig('injury_impact.png', dpi=150)
plt.show()
# ============ 6. 结论输出 ============
print("\n" + "=" * 60)
print("【分析结论】")
full = compare.loc['全员出战']
injured = compare.loc['有伤员/停赛']
print(f"1. 全员出战时胜率 {full['胜率(%)']}%,缺阵时胜率 {injured['胜率(%)']}%,"
      f"差 {full['胜率(%)'] - injured['胜率(%)']:.1f} 个百分点")
print(f"2. 全员出战场均积分 {full['场均积分']},缺阵时 {injured['场均积分']},"
      f"差 {full['场均积分'] - injured['场均积分']:.2f} 分")
print(f"3. 缺阵人数越多,场均积分呈下降趋势(详见细分表)")
print(f"4. 核心球员缺阵影响显著大于普通球员缺阵")

关键分析维度

维度 说明
二元对比 有/无伤病停赛 vs 战绩、积分、进失球
人数细分 缺阵 0/1/2/3/4 人时的表现衰减曲线
核心权重 区分核心球员与普通球员缺阵的差异
主客场 可增加 is_home 分组,看主场抗风险能力

可扩展方向

  1. 接入真实数据:用 football-data.org、FBref、SofaScore API 或爬取数据。
  2. 球员权重模型:按球员身价/评分/出场时间给缺阵球员赋权,计算"缺阵权重总和"。
  3. 回归分析:用 statsmodels 做 OLS 回归,量化缺阵人数对积分的边际影响。
  4. 时间序列:观察伤病潮期间与复出后球队状态的恢复曲线。
  5. 对比多支球队:横向对比哪些球队"抗伤病能力强"(阵容深度指标)。

真实数据接入示例(伪代码)

# 从FBref等数据源读取
# matches = pd.read_csv('team_matches.csv')
# injuries = pd.read_csv('injury_reports.csv')
# 按比赛日期合并缺阵名单
# merged = matches.merge(injuries, on=['team', 'match_date'], how='left')
# merged['absent_count'] = merged['absent_players'].str.split(',').str.len()

如果你能提供真实比赛数据CSV字段结构(如比赛日期、对手、比分、缺阵名单),我可以帮你改造成直接可用的分析脚本,需要我补充 回归建模 或 多队横向对比 的版本吗?

直播入口

NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播NBA直播NBA直播NBA直播U17直播U17直播U17直播亚冠直播亚冠直播亚冠直播欧冠直播欧冠直播欧冠直播

抱歉,评论功能暂时关闭!

友情链接攻略窝 · 礼包窝 · 配置窝 · 换算窝 · 算法窝 · 查文窝 · 手册窝 · 问答窝 · 全文搜 · 手册库
相关站点礼包窝 · 查文窝