本文目录导读:

要分析Python案例中的攻守转换速度,你需要从以下几个方面入手:
找到攻守转换的判定逻辑
在代码中找到判断攻守状态的函数或条件,通常会有类似这样的结构:
def is_attacking(state):
# 判断当前是否处于进攻状态
return state['possession'] == 'us' and state['in_opponent_half'] == True
def is_defending(state):
return state['possession'] == 'opponent' or state['in_own_half'] == True
计算攻守转换速度
攻守转换速度 = 从防守状态变为进攻状态(或反之)的时间间隔
def calculate_transition_speed(states):
transitions = []
current_phase = None
for state in states:
if is_attacking(state):
phase = 'attack'
elif is_defending(state):
phase = 'defend'
else:
continue
if phase != current_phase:
if current_phase is not None:
transitions.append({
'from': current_phase,
'to': phase,
'time': state['timestamp']
})
current_phase = phase
# 计算转换时间差
speeds = []
for i in range(1, len(transitions)):
time_diff = transitions[i]['time'] - transitions[i-1]['time']
speeds.append({
'type': f"{transitions[i-1]['to']}->{transitions[i]['to']}",
'speed': time_diff
})
return speeds
使用时间戳或帧数计算
如果案例中有时间戳,用时间戳差值计算:
transitions_speed = []
for i in range(len(states)-1):
if states[i]['status'] != states[i+1]['status']: # 状态变了
time_diff = states[i+1]['timestamp'] - states[i]['timestamp']
transitions_speed.append({
'from': states[i]['status'],
'to': states[i+1]['status'],
'duration': time_diff
})
可视化转换速度
import matplotlib.pyplot as plt
def visualize_transitions(transitions):
attack_to_defend = [t['duration'] for t in transitions if t['type'] == 'attack->defend']
defend_to_attack = [t['duration'] for t in transitions if t['type'] == 'defend->attack']
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.hist(attack_to_defend, alpha=0.7, label='攻转守')
plt.xlabel('时间(秒)')
plt.ylabel('次数')
plt.title('攻转守速度分布')
plt.legend()
plt.subplot(1, 2, 2)
plt.hist(defend_to_attack, alpha=0.7, label='守转攻', color='green')
plt.xlabel('时间(秒)')
plt.ylabel('次数')
plt.title('守转攻速度分布')
plt.legend()
plt.tight_layout()
plt.show()
计算关键指标
def get_transition_metrics(transitions):
attack_to_defend_times = [t['duration'] for t in transitions if t['from'] == 'attack' and t['to'] == 'defend']
defend_to_attack_times = [t['duration'] for t in transitions if t['from'] == 'defend' and t['to'] == 'attack']
return {
'attack_to_defend_avg': np.mean(attack_to_defend_times) if attack_to_defend_times else None,
'defend_to_attack_avg': np.mean(defend_to_attack_times) if defend_to_attack_times else None,
'max_transition_speed': min(defend_to_attack_times + attack_to_defend_times) if transitions else None,
'total_transitions': len(transitions)
}
实战建议
- 如果代码有明确的攻守状态标记(如
possession_team、is_attacking等),直接提取状态变化点 - 如果没有明确标记,需要通过条件判断(如球权、位置等)自行判定
- 关注关键字段:常见的有
game_time、frame_id、event_time等 - 转换速度通常指的是:防守方获得球权到发起有效进攻的时间,或进攻失败到退防到位的时间
你需要我帮你分析具体的代码结构吗?可以提供相关代码片段,我来帮你指出如何提取攻守转换速度。