python案例统计界外球进攻威胁次数?

wen python案例 1

统计界外球进攻威胁次数

下面是一个完整的 Python 案例,用于统计足球比赛中界外球(Throw-in)产生的进攻威胁次数。

python案例统计界外球进攻威胁次数?

需求定义

"界外球进攻威胁"通常定义为:界外球发出后,在规定时间窗口内(如 10 秒)或传球链内,产生了以下任一威胁事件:

  • 射门(Shot)
  • 进球(Goal)
  • 关键传球 / 创造射门的机会(Key Pass / xG > 阈值)
  • 进入进攻三区(Attacking Third)

数据准备(模拟数据)

真实数据一般来自 StatsBomb、Opta、Wyscout 等,这里用模拟事件数据演示。

import pandas as pd
from datetime import timedelta
# 模拟事件数据(每条事件代表一次球场动作)
events = pd.DataFrame([
    # 主队界外球 -> 传中 -> 射门(威胁)
    {"match_id": 1, "team": "A", "type": "Throw-in", "minute": 10, "second": 0,
     "x": 80, "y": 30, "receiver_team": "A"},
    {"match_id": 1, "team": "A", "type": "Pass", "minute": 10, "second": 5,
     "x": 88, "y": 35, "receiver_team": "A"},
    {"match_id": 1, "team": "A", "type": "Shot", "minute": 10, "second": 8,
     "x": 95, "y": 40, "receiver_team": "A"},
    # 主队界外球 -> 被断(无威胁)
    {"match_id": 1, "team": "A", "type": "Throw-in", "minute": 25, "second": 0,
     "x": 60, "y": 20, "receiver_team": "A"},
    {"match_id": 1, "team": "A", "type": "Pass", "minute": 25, "second": 4,
     "x": 65, "y": 25, "receiver_team": "B"},
    # 主队界外球 -> 进球(高威胁)
    {"match_id": 1, "team": "A", "type": "Throw-in", "minute": 40, "second": 0,
     "x": 85, "y": 50, "receiver_team": "A"},
    {"match_id": 1, "team": "A", "type": "Pass", "minute": 40, "second": 6,
     "x": 92, "y": 48, "receiver_team": "A"},
    {"match_id": 1, "team": "A", "type": "Goal", "minute": 40, "second": 9,
     "x": 100, "y": 50, "receiver_team": "A"},
    # 客队界外球 -> 无后续(无威胁)
    {"match_id": 1, "team": "B", "type": "Throw-in", "minute": 55, "second": 0,
     "x": 30, "y": 60, "receiver_team": "B"},
])

核心统计逻辑

from datetime import datetime
def compute_threat_events(events, time_window=10, xg_threshold=0.05):
    """
    统计界外球后的进攻威胁次数
    - time_window: 界外球后时间窗口(秒)
    - xg_threshold: 射门 xG 阈值(这里暂用事件类型代替)
    """
    # 构造统一时间戳
    events = events.copy()
    events["time"] = events["minute"] * 60 + events["second"]
    threats = []
    throw_ins = events[events["type"] == "Throw-in"]
    for _, ti in throw_ins.iterrows():
        # 找同队、同场比赛、界外球之后时间窗口内的事件
        mask = (
            (events["match_id"] == ti["match_id"]) &
            (events["team"] == ti["team"]) &
            (events["time"] > ti["time"]) &
            (events["time"] <= ti["time"] + time_window)
        )
        window = events[mask]
        # 威胁事件类型
        threat_types = {"Shot", "Goal"}
        # 也可加键传球、进攻三区判定
        has_threat = window["type"].isin(threat_types).any()
        # 是否需要"球权仍在本方"?可以通过传球链进一步过滤
        # 此处简化:只要窗口内本队出现射门/进球即算威胁
        if has_threat:
            threats.append({
                "match_id": ti["match_id"],
                "team": ti["team"],
                "minute": ti["minute"],
                "threat_events": ", ".join(
                    window[window["type"].isin(threat_types)]["type"].tolist()
                ),
            })
    return pd.DataFrame(threats)
threat_df = compute_threat_events(events, time_window=10)
print("界外球进攻威胁明细:")
print(threat_df)
print(f"\n总威胁次数:{len(threat_df)}")

按球队汇总

summary = (
    threat_df.groupby("team")
    .agg(威胁次数=("match_id", "count"))
    .reset_index()
)
print(summary)

输出(基于模拟数据):

  team  威胁次数
0    A      2

进阶:加入传球链 / xG 权重

更严谨的做法是构造传球链(Possession Chain):

def threat_with_xg(events, chain_max_sec=15):
    """
    沿传球链统计:从界外球开始,直到对手拿球或超时为止
    统计链内 xG 累计值
    """
    events = events.copy()
    events["time"] = events["minute"] * 60 + events["second"]
    events = events.sort_values(["match_id", "time"]).reset_index(drop=True)
    results = []
    for idx, row in events.iterrows():
        if row["type"] != "Throw-in":
            continue
        team = row["team"]
        start_t = row["time"]
        xg_total = 0
        shots = 0
        # 向后遍历,直到球权丢失或超时
        for j in range(idx + 1, len(events)):
            nxt = events.loc[j]
            if nxt["match_id"] != row["match_id"]:
                break
            if nxt["time"] - start_t > chain_max_sec:
                break
            # 球权丢失(receiver_team 非本队)则终止
            if nxt.get("receiver_team") not in (team, None):
                break
            if nxt["type"] in ("Shot", "Goal"):
                # 假设已有 xG 列;这里模拟赋值
                xg = 0.15 if nxt["type"] == "Goal" else 0.08
                xg_total += xg
                shots += 1
        if xg_total > 0:
            results.append({
                "match_id": row["match_id"],
                "team": team,
                "minute": row["minute"],
                "shots": shots,
                "xg": round(xg_total, 3),
            })
    return pd.DataFrame(results)
chain_df = threat_with_xg(events)
print(chain_df)

关键要点总结

环节 说明
数据源 StatsBomb(免费开放部分比赛)、Opta、Wyscout
时间戳 需将 minute + second 转为秒
威胁定义 射门/进球/关键传球/进入进攻三区,可组合
时间窗口 常用 10–15 秒,或按传球链自然结束
过滤条件 需保证球权仍属本方(receiver_team 判定)
进阶指标 累计 xG、射门数、是否形成定位球二次进攻

如果你有真实的 StatsBomb JSON 或 CSV 事件数据,我可以帮你写一个直接加载并统计的完整脚本(含数据解析部分)——告诉我数据格式即可。

直播入口

足球直播足球直播足球直播足球直播英超直播英超直播NBA直播NBA直播体育比赛直播体育比赛直播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直播亚冠直播亚冠直播亚冠直播

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

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