Python 案例:统计长短传比例分布
在足球/篮球数据分析中,"长短传比例"是常用的技术指标,下面我用 足球传球数据 为例,给你一个完整可运行的 Python 案例。

需求定义
| 项目 | 定义 |
|---|---|
| 短传 | 传球距离 < 25 米 |
| 长传 | 传球距离 ≥ 25 米 |
| 短传比例 | 短传次数 / 总传球次数 |
| 长传比例 | 长传次数 / 总传球次数 |
分析目标:看不同类型球员/球队的长短传比例如何分布(如用直方图、分箱统计、分组统计)。
模拟数据生成
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
# 模拟 200 名球员,每人 100 次传球
players = []
positions = ['GK', 'CB', 'FB', 'CM', 'AM', 'FW']
# 不同位置的传球距离分布参数(均值, 标准差)
dist_params = {
'GK': (35, 12), # 门将长传多
'CB': (28, 10),
'FB': (22, 9),
'CM': (18, 7),
'AM': (15, 6),
'FW': (20, 8),
}
for i in range(200):
pos = np.random.choice(positions)
mu, sigma = dist_params[pos]
n_pass = 100
distances = np.random.normal(mu, sigma, n_pass)
distances = np.clip(distances, 2, 80) # 限制合理范围
for d in distances:
players.append({
'player_id': f'P{i:03d}',
'position': pos,
'pass_distance': round(d, 1)
})
df = pd.DataFrame(players)
print(df.head())
print(f"总传球记录数: {len(df)}")
统计每个球员的长短传比例
LONG_THRESHOLD = 25 # 长传阈值(米)
df['is_long'] = df['pass_distance'] >= LONG_THRESHOLD
# 按球员聚合
player_stats = df.groupby(['player_id', 'position']).agg(
total_passes=('is_long', 'size'),
long_passes=('is_long', 'sum')
).reset_index()
player_stats['short_passes'] = player_stats['total_passes'] - player_stats['long_passes']
player_stats['long_ratio'] = player_stats['long_passes'] / player_stats['total_passes']
player_stats['short_ratio'] = 1 - player_stats['long_ratio']
print(player_stats.head())
长短传比例分布统计(核心)
分箱统计分布(推荐)
# 按 10% 一档分箱,统计每个区间的球员人数
bins = np.arange(0, 1.05, 0.1)
player_stats['long_ratio_bin'] = pd.cut(
player_stats['long_ratio'], bins=bins, include_lowest=True
)
dist = player_stats['long_ratio_bin'].value_counts().sort_index()
print("长传比例分布(按10%分档):")
print(dist)
# 顺便输出百分比
dist_pct = (dist / dist.sum() * 100).round(1)
print("\n占比(%):")
print(dist_pct)
输出示例:
长传比例分布(按10%分档):
(0.0, 0.1] 12
(0.1, 0.2] 38
(0.2, 0.3] 51
(0.3, 0.4] 44
(0.4, 0.5] 28
(0.5, 0.6] 15
(0.6, 0.7] 8
(0.7, 0.8] 3
(0.8, 0.9] 1
(0.9, 1.0] 0
按位置分组统计分布
# 每个位置的:人数、平均长传比例、标准差
pos_summary = player_stats.groupby('position').agg(
player_count=('player_id', 'count'),
avg_long_ratio=('long_ratio', 'mean'),
std_long_ratio=('long_ratio', 'std'),
).reset_index().sort_values('avg_long_ratio', ascending=False)
pos_summary['avg_long_ratio'] = pos_summary['avg_long_ratio'].round(3)
print(pos_summary)
预期输出(示意):
position player_count avg_long_ratio std_long_ratio
0 GK 25 0.612 0.081
1 CB 33 0.421 0.112
2 FW 31 0.212 0.098
3 FB 30 0.186 0.087
4 CM 44 0.098 0.052
5 AM 37 0.061 0.041
整体分布描述性统计
desc = player_stats['long_ratio'].describe()
print("长传比例整体分布描述:")
print(desc)
# 分布形态:偏度 / 峰度
from scipy import stats
skew = stats.skew(player_stats['long_ratio'])
kurt = stats.kurtosis(player_stats['long_ratio'])
print(f"\n偏度 (Skewness): {skew:.3f}")
print(f"峰度 (Kurtosis): {kurt:.3f}")
# 偏度 > 0 表示右偏,即大部分球员长传比例偏低,少数球员偏高
可视化分布
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
# 左图:长传比例直方图
axes[0].hist(player_stats['long_ratio'], bins=20,
color='steelblue', edgecolor='black')
axes[0].axvline(player_stats['long_ratio'].mean(), color='red',
linestyle='--', label=f"均值 {player_stats['long_ratio'].mean():.2f}")
axes[0].set_xlabel('长传比例')
axes[0].set_ylabel('球员人数')
axes[0].set_title('长传比例分布直方图')
axes[0].legend()
# 右图:按位置分组的长传比例箱线图
positions_order = pos_summary['position'].tolist()
data_by_pos = [player_stats[player_stats['position'] == p]['long_ratio']
for p in positions_order]
axes[1].boxplot(data_by_pos, labels=positions_order)
axes[1].set_ylabel('长传比例')
axes[1].set_title('不同位置球员长传比例分布')
plt.tight_layout()
plt.savefig('pass_ratio_distribution.png', dpi=120)
plt.show()
结论解读(重点)
运行后你会得到类似结论:
- 整体形态右偏(右倚):大部分球员长传比例集中在 10%–40% 之间,少数门将/中卫拉高整体均值。
- 按位置梯度明显:
- 门将长传比例最高(平均约 60%)
- 中后卫次之(约 40%)
- 中场/前腰最低(10% 以下,以短传为主)
- 前锋因争抢二点球和支点作用居中(20% 左右)
- 分布方差:门将、中卫差异大(不同战术体系影响大),中场球员同质化高。
可以继续拓展的方向
| 拓展 | 方法 |
|---|---|
| 时间维度变化 | 按赛季 / 上下半场分组 |
| 比赛情境 | 领先/落后时的长短传比例 |
| 球队风格 | 按球队聚合做球队风格聚类 |
| 显著性检验 | 用 ANOVA 检验不同位置长传比例差异是否显著 |
| 预警指标 | 定义"长传比例异常"球员并提示 |
如果你需要我把案例改成 篮球(长两分/三分 vs 篮下、传球距离) 或 乒乓球/羽毛球长短球 版本,或者补上 ANOVA 检验、聚类分析,告诉我,我可以继续完善。
版权声明:本站文章为原创整理,转载请注明出处。