实用脚本统计冲刺跑次数谁更多?

wen 实用脚本 2

本文目录导读:

实用脚本统计冲刺跑次数谁更多?

  1. 情况一:你有一份GPS手表/APP导出的CSV/Excel文件(有时间和速度列)
  2. 情况二:你只有“时间间隔”数据(例如从秒表或手动记录的数组)
  3. 情况三:你想比较“冲刺跑”和“慢跑”谁的总距离/次数多(数据是轨迹点)
  4. 关键点:如何定义“一次”冲刺?

这是一个典型的“数据统计”或“运动数据分析”问题,你的问题描述比较简略,我需要先明确“冲刺跑”的数据来源“次数”的定义,才能给出实用的脚本。

为了直接给你可用的东西,我假设以下几种最常见的情况,并分别给出Python脚本(因为处理数据最方便),你可以根据你的实际情况选择对应脚本。


你有一份GPS手表/APP导出的CSV/Excel文件(有时间和速度列)

这是最实用的场景,假设你的文件包含 时间戳速度(m/s或km/h)

逻辑:定义“冲刺”为速度超过阈值(如:>7 m/s 或 >25 km/h),并且持续超过2秒(避免断断续续),脚本会统计每人(或每个文件)的冲刺段数,而不是秒数。

import pandas as pd
def count_sprints(file_path, speed_col='speed_ms', time_col='timestamp', threshold_ms=7.0, min_duration_s=2.0):
    """
    统计一个文件中的冲刺跑段数
    :param file_path: CSV文件路径
    :param speed_col: 速度列名(单位:米/秒)
    :param time_col: 时间列名(用于计算持续时间)
    :param threshold_ms: 冲刺速度阈值(米/秒),默认7m/s(约25km/h)
    :param min_duration_s: 达到阈值的最低持续时间(秒)
    :return: 冲刺次数和总时长
    """
    df = pd.read_csv(file_path)
    # 确保速度是数字
    df[speed_col] = pd.to_numeric(df[speed_col], errors='coerce')
    # 生成一个布尔列:是否在冲刺
    df['is_sprint'] = df[speed_col] > threshold_ms
    sprint_count = 0
    sprint_start = None
    total_sprint_time = 0
    # 遍历每一行(模拟时间序列)
    for index, row in df.iterrows():
        if row['is_sprint']:
            # 如果上一行不是冲刺,说明是新的冲刺开始
            if sprint_start is None:
                sprint_start = index
        else:
            # 如果之前一直在冲刺,现在结束了
            if sprint_start is not None:
                end_index = index - 1
                duration = df.loc[end_index, time_col] - df.loc[sprint_start, time_col]
                # 持续时间大于最小阈值才算一次冲刺
                if duration >= min_duration_s:
                    sprint_count += 1
                    total_sprint_time += duration
                sprint_start = None
    # 处理文件末尾还在冲刺的情况
    if sprint_start is not None:
        duration = df.iloc[-1][time_col] - df.loc[sprint_start, time_col]
        if duration >= min_duration_s:
            sprint_count += 1
            total_sprint_time += duration
    return sprint_count, total_sprint_time
# --- 使用方法 ---
# 假设你有两个文件:player_a.csv 和 player_b.csv
# 文件格式:timestamp,speed_ms
# 调用示例
# a_count, a_time = count_sprints('player_a.csv')
# b_count, b_time = count_sprints('player_b.csv')
# print(f"A的冲刺次数: {a_count}, 总时长: {a_time}")
# print(f"B的冲刺次数: {b_count}, 总时长: {b_time}")
# if a_count > b_count: print("A更多") ...

你只有“时间间隔”数据(例如从秒表或手动记录的数组)

假设你有两个数组,分别记录了两个运动员为了完成某几组冲刺所用的时间(单位:秒),你想比较谁跑的次数多。

# 假设这是数据:每个数字代表一次冲刺的完成时间(秒)
player_a_sprint_times = [10.2, 11.5, 10.8, 12.1]  # A跑了4次
player_b_sprint_times = [9.8, 10.5, 11.0]        # B跑了3次
if len(player_a_sprint_times) > len(player_b_sprint_times):
    print("A比B跑得多")
    print(f"A跑了{len(player_a_sprint_times)}次,B跑了{len(player_b_sprint_times)}次")
elif len(player_a_sprint_times) < len(player_b_sprint_times):
    print("B比A跑得多")
else:
    print("次数一样多")

你想比较“冲刺跑”和“慢跑”谁的总距离/次数多(数据是轨迹点)

如果你只有经纬度和时间戳,需要先计算速度。

import pandas as pd
import numpy as np
from math import radians, cos, sin, asin, sqrt
def haversine(lon1, lat1, lon2, lat2):
    """计算两点间距离(米)"""
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a))
    r = 6371000
    return c * r
def calculate_sprint_count_from_gps(df, time_col='ts', lat_col='lat', lon_col='lon', threshold_speed=7.0):
    """从GPS数据计算冲刺次数"""
    df = df.copy()
    # 计算时间差(秒)和距离(米)
    df['dt'] = df[time_col].diff().dt.total_seconds()
    df['dist'] = df.apply(lambda row: haversine(row[lon_col], row[lat_col], 
                                               df.iloc[row.name-1][lon_col], df.iloc[row.name-1][lat_col]) 
                          if row.name > 0 else 0, axis=1)
    df['speed'] = df['dist'] / df['dt']
    # 标记冲刺状态
    df['is_sprint'] = df['speed'] > threshold_speed
    # 用类似情况一的方法计算连续段数
    # ... (这里省略重复代码,核心逻辑同情况一)
    return sprint_count

关键点:如何定义“一次”冲刺?

脚本的核心在于逻辑判断,请根据你的运动项目(足球、短跑、篮球)调整:

  • 阈值:短跑百米选手阈值应设为10m/s以上;足球运动员阈值设为7-8m/s即可。
  • 最短持续时间:通常要持续1-2秒以上才算一次“冲刺跑”,否则是瞬间提速的抖动。

想要更精准的脚本,请告诉我:

  1. 你的数据是什么格式?(Excel/CSV/纯文本/手表APP截图)
  2. 数据里有哪些列?(时间、速度、心率,还是只有距离和时间?)
  3. 你如何定义“一次冲刺”?(速度超过XX公里/小时,并持续X秒)

如果你手头有具体的文件(可以脱敏后粘贴几行示例),我可以帮你写一个完整可运行的统计脚本。

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