环境配置

获取 API 密钥并快速体验 TimechoAI 的时序预测能力,只需几分钟即可完成。

步骤 1:REST 方式

在终端中运行以下命令,将 API-Key 替换为您的实际 API 密钥:

curl -s -X POST https://ai.timecho.com/ai/api/v1/forecast \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <API-Key>" \
  -d '{
    "targets": [{
      "columns": ["value"],
      "data": [[120],[135],[142],[168],[195],[220],[285],[310],[345],[380],[420],[468],[125],[140],[155],[180],[210],[245],[295],[325],[360],[395],[440],[485]]
    }],
    "output_length": [5]
  }'

步骤 2:Python 方式

使用 Python SDK 进行预测,代码更加清晰易读。首先安装 SDK:

pip install timecho-ai

示例数据CSV:sample.csv,然后运行以下代码:

import pandas as pd
from timecho_ai import TimechoAIClient

# 读取示例数据
raw_df = pd.read_csv("https://ai.timecho.com/data/sample.csv")
print(raw_df.head())

# 任务定义:输入 16 个点,预测 8 个点
INPUT_LENGTH = 16
OUTPUT_LENGTH = 8

# 创建客户端(需替换为您的 API Key)
client = TimechoAIClient(api_key="your_timecho-ai_api_key")

# 构建目标变量 DataFrame
target_df = raw_df[["time", "target"]][:INPUT_LENGTH]

# 预测 "target" 未来 8 个点
forecast_dfs = client.forecast(
    targets=target_df,
    output_length=OUTPUT_LENGTH
)

print(forecast_dfs[0])

步骤 3:查看预测结果

Python 方式成功调用后,打印结果如下(预测 “target” 未来 8 个时间点):

{
  "code": 200,
  "message": "Forecast tasks completed successfully",
  "data": {
    "results": [
      {
        "data": [
          [450.21026611328125],
          [383.2858581542969],
          [309.28387451171875],
          [298.32379150390625],
          [264.8620910644531],
          [256.2868347167969],
          [257.38861083984375],
          [266.3112487792969]
        ],
        "columns": ["value"]
      }
    ]
  }
}

下一步