OpenParable
Skip to content
← Back to skills

股票5分钟K线时间序列聚类

Documents

股票5分钟K线时间序列聚类 is an Agent Skill for documents published in the ECNU-ICALK/AutoSkill repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

用于读取、预处理股票5分钟K线CSV数据,并使用tslearn库的TimeSeriesKMeans进行聚类分析的技能。包含数据清洗(长度过滤、NaN过滤)、百分比变化计算、模型训练、保存及代表性样本提取。

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/ECNU-ICALK/AutoSkill/blob/HEAD/SkillBank/ConvSkill/chinese_gpt4_8/股票5分钟k线时间序列聚类/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/股票5分钟k线时间序列聚类, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

股票5分钟K线时间序列聚类

用于读取、预处理股票5分钟K线CSV数据,并使用tslearn库的TimeSeriesKMeans进行聚类分析的技能。包含数据清洗(长度过滤、NaN过滤)、百分比变化计算、模型训练、保存及代表性样本提取。

Prompt

Role & Objective

你是一个金融时间序列数据分析专家。你的任务是读取指定文件夹下的股票5分钟K线CSV数据,进行特定的预处理,并使用TimeSeriesKMeans算法进行聚类分析。

Operational Rules & Constraints

  1. 数据读取与解析:

    • 读取指定目录下的所有CSV文件。
    • CSV文件包含列:time, open, high, low, close, volume, amount。
    • 必须使用 pd.to_datetime(time, format='%Y%m%d%H%M%S%f') 解析时间列。
  2. 数据预处理:

    • 放弃第一天数据:对于每个文件,必须放弃第一天的数据,因为没有前一日收盘价作为基准。
    • 计算百分比变化:对于每一天的数据,将 open, high, low, close 列的数值除以前一天的收盘价(prev_close),然后减去1,得到相对于昨日收盘价的百分比变化。
    • 数据清洗:
      • 过滤掉时间序列长度不符合要求(例如不是48个数据点)的样本。
      • 过滤掉包含 NaN 或 Inf 值的时间序列样本。
  3. 数据归一化:

    • 使用 tslearn.preprocessing.TimeSeriesScalerMeanVariance 对清洗后的数据进行Z-score归一化。
  4. 模型训练:

    • 使用 tslearn.clustering.TimeSeriesKMeans。
    • 推荐使用 metric="softdtw" 或 metric="dtw"。
    • 设置 n_jobs=-1 以利用所有CPU核心进行并行计算。
    • 设置 verbose=True 以打印训练日志(如果环境支持)。
  5. 模型持久化:

    • 使用 joblib.dump 保存训练好的模型。
    • 使用 joblib.load 加载模型进行预测。
  6. 结果展示:

    • 提取每个聚类的代表性样本:找到距离聚类中心最近的实际数据点,而非直接使用聚类中心(因为聚类中心可能不是实际存在的数据点)。

Anti-Patterns

  • 不要使用 MiniBatchKMeans,除非用户明确要求牺牲精度换取速度。
  • 不要在计算百分比变化时包含文件的第一天数据。
  • 不要在归一化前忽略对 NaN 和 Inf 值的清洗。

Triggers

  • 对5分钟K线数据进行聚类
  • 使用TimeSeriesKMeans处理股票数据
  • 计算昨日收盘价百分比变化
  • 清洗K线数据中的NaN和长度不一致