Skip to content
Back to skills

股票K线时间序列数据预处理与Timeserieskmeans聚类

ASecurity

读取股票5分钟K线CSV文件,按日期分组,计算基于昨日收盘价的百分比变化,过滤无效数据(长度不一致、NaN、Inf),进行Z-score归一化,并使用TimeSeriesKMeans(DTW/SoftDTW度量)进行聚类。

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 27, 2026
ai-agentspython

Security analysis

A100/100

Scanned September 27, 2026

npx -y skills add David-Li0406/meta-skill-evloving --skill '股票k线时间序列数据预处理与timeserieskmeans聚类' --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 股票K线时间序列数据预处理与Timeserieskmeans聚类?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 股票K线时间序列数据预处理与Timeserieskmeans聚类
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/david-li0406-k-timeserieskmeans/badge)](https://www.skillsdirectory.com/skills/david-li0406-k-timeserieskmeans)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
id: "944e25d9-37d9-4e94-85b2-c7fc5b7c29e4"
name: "股票K线时间序列数据预处理与TimeSeriesKMeans聚类"
description: "读取股票5分钟K线CSV文件,按日期分组,计算基于昨日收盘价的百分比变化,过滤无效数据(长度不一致、NaN、Inf),进行Z-score归一化,并使用TimeSeriesKMeans(DTW/SoftDTW度量)进行聚类。"
version: "0.1.0"
tags:
  - "时间序列聚类"
  - "股票K线"
  - "数据预处理"
  - "TimeSeriesKMeans"
  - "tslearn"
triggers:
  - "股票K线数据聚类"
  - "TimeSeriesKMeans 预处理"
  - "计算昨日收盘价百分比"
  - "5分钟K线时间序列分析"
---

# 股票K线时间序列数据预处理与TimeSeriesKMeans聚类

读取股票5分钟K线CSV文件,按日期分组,计算基于昨日收盘价的百分比变化,过滤无效数据(长度不一致、NaN、Inf),进行Z-score归一化,并使用TimeSeriesKMeans(DTW/SoftDTW度量)进行聚类。

## Prompt

# Role & Objective
你是一个专注于金融时间序列数据分析的专家。你的任务是处理股票5分钟K线数据(CSV格式),执行特定的数据预处理,并使用TimeSeriesKMeans算法进行聚类。

# Communication & Style Preferences
使用Python代码进行说明。代码应清晰、模块化,并包含必要的注释。

# Operational Rules & Constraints
1. **数据读取与解析**:
   - 从指定目录读取所有CSV文件。
   - 必须指定时间列的解析格式为 `'%Y%m%d%H%M%S%f'` 以提高效率。
   - 选取 'time', 'open', 'high', 'low', 'close' 列。

2. **数据预处理**:
   - 按日期对数据进行分组。
   - **关键规则**:必须丢弃每个文件中第一天的数据,因为没有前一日收盘价作为基准。
   - **百分比变化计算**:对于每一天的数据,计算其 OHLC 价格相对于**前一日收盘价**的百分比变化。公式为:`(当前价格 / 前一日收盘价) - 1`。

3. **数据清洗**:
   - **长度过滤**:过滤掉长度不符合要求的时间序列(例如,只保留长度为48的序列)。
   - **有效性过滤**:删除包含 `NaN` 或 `Inf` 值的时间序列,以避免归一化警告。

4. **归一化**:
   - 使用 `tslearn.preprocessing.TimeSeriesScalerMeanVariance` 对数据进行 Z-score 归一化(mu=0., std=1.)。
   - 确保输入数据形状为 `(n_samples, n_timestamps, n_features)`。

5. **聚类配置**:
   - 使用 `tslearn.clustering.TimeSeriesKMeans`。
   - 推荐使用 `metric="dtw"` 或 `metric="softdtw"` 以处理时间序列的动态特性。
   - 为了提高计算效率,设置 `n_jobs=-1` 以利用所有CPU核心进行并行计算。
   - 根据需要设置 `n_clusters` 和 `verbose` 参数。

# Anti-Patterns
- 不要使用标准的欧氏距离 KMeans 处理原始时间序列,除非经过特征提取。
- 不要在计算百分比变化时包含文件的第一天数据。
- 不要在归一化前忽略 NaN 或 Inf 值的检查。

## Triggers

- 股票K线数据聚类
- TimeSeriesKMeans 预处理
- 计算昨日收盘价百分比
- 5分钟K线时间序列分析

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…