Time-Anchor ModernBERT 32M实战教程:用Python预测东京气温的64小时变化
Time-Anchor ModernBERT 32M实战教程:用Python预测东京气温的64小时变化
【免费下载链接】time-anchor-modernbert-32m项目地址: https://ai.gitcode.com/hf_mirrors/K-Iwa/time-anchor-modernbert-32m
Time-Anchor ModernBERT 32M是一款基于32M参数ModernBERT骨干构建的紧凑型时间序列模型,总参数达33.4M(包含时间序列输入/输出头)。通过一次调用,该模型可返回概率预测、变量/时间影响和锚定预测三大核心结果,为东京气温等时间序列数据预测提供强大支持。
快速安装Time-Anchor ModernBERT 32M
要开始使用Time-Anchor ModernBERT 32M进行东京气温预测,首先需要安装相关依赖包。在命令行中执行以下简单命令即可完成安装:
pip install time-anchorpredict_time_anchor函数支持接受Hugging Face Hub模型ID或本地 checkpoint 目录,方便用户灵活使用模型。
获取东京气温数据
进行气温预测前,需要获取东京的历史气象数据。可以通过Open-Meteo的API获取指定时间段的每小时气温数据,代码如下:
import pandas as pd url = ( "https://archive-api.open-meteo.com/v1/archive" "?latitude=35.69&longitude=139.69&start_date=2024-10-01&end_date=2024-12-31" "&hourly=temperature_2m,relative_humidity_2m,surface_pressure,wind_speed_10m&format=csv" ) weather = pd.read_csv(url, skiprows=3) temperature = weather.iloc[:, 1].astype("float32")实现东京气温64小时预测
使用Time-Anchor ModernBERT 32M模型对东京气温进行64小时预测非常简单,只需调用predict_time_anchor函数并传入相应参数:
from time_anchor import predict_time_anchor result = predict_time_anchor( "K-Iwa/time-anchor-modernbert-32m", target_context=temperature[:1440], prediction_length=64, quantile_levels=(0.1, 0.5, 0.9), ) print(pd.DataFrame(result.forecast_rows))该模型在东京每小时气温64小时预测任务中表现出色,中位数MAE为0.57°C,且所有实际值都在q10–q90范围内。
分析气象变量影响
Time-Anchor ModernBERT 32M还能分析不同气象变量在不同时间步对气温预测的影响,帮助用户深入了解气温变化的驱动因素:
result = predict_time_anchor( "K-Iwa/time-anchor-modernbert-32m", target_context=temperature[:168], explanatory_contexts=[weather.iloc[:168, i].astype("float32") for i in (2, 3, 4)], gaf={"enabled": True, "topk_time_steps": 0}, ) print(pd.DataFrame(result.variable_impact_rows))通过分析可知,对于每小时的时间索引,所有变量的影响总和为1,便于直观比较各变量的重要性。
使用锚定预测提高精度
如果已知未来某些时间点的气温值,可以使用锚定预测功能来提高整体预测精度。例如在相关案例中,固定六个已知月份的数据,可将中位数预测MAE从46减少到10千名乘客(此处以航空乘客数据为例说明锚定预测效果)。以下是锚定预测的代码示例:
result = predict_time_anchor( "K-Iwa/time-anchor-modernbert-32m", target_context=passengers[:-24], # 此处passengers为相关时间序列数据,实际使用时替换为气温数据 prediction_length=24, anchor={ "mode": "observed", "positions": [4, 8, 12, 16, 20, 24], "values": [396, 559, 405, 461, 606, 432], # 实际使用时替换为已知的气温值 }, ) print(pd.DataFrame(result.forecast_rows))positions参数为基于1的预测范围步长,锚定预测仅使用目标历史数据,当传入explanatory_contexts时需禁用锚定。
模型详细参数
Time-Anchor ModernBERT 32M模型的主要参数如下:
- 骨干网络:ModernBERT编码器(10层,隐藏层384,6个头部)
- 参数总量:33.4M,其中ModernBERT骨干31.9M(名称中的“32M”),时间序列头1.5M(float32,safetensors)
- 最大上下文长度:3,463步
- 预测范围:64步
- 训练分位数:0.1 – 0.9(九个级别)
- 归因方法:Generalized Attention Flow (GAF) via barrier-method max-flow
- 锚定模式:
observed,auto,forward,inverted,sparse,hierarchical,hybrid
通过CLI进行预测
除了使用Python代码,还可以通过命令行界面(CLI)进行预测:
time-anchor-infer --checkpoint K-Iwa/time-anchor-modernbert-32m \ --input data.csv --target-column Target \ --exogenous-columns Feature1,Feature2,Feature3该命令会生成output/forecast.csv、output/variable_impact.csv和output/result.json文件。添加--no-impact可仅进行预测,或使用--anchor-mode observed --anchor-positions 12,24 --anchor-values 0.2,0.4进行锚定预测。
要使用该模型,可通过以下命令克隆仓库:
git clone https://gitcode.com/hf_mirrors/K-Iwa/time-anchor-modernbert-32mTime-Anchor ModernBERT 32M为时间序列预测提供了强大且易用的工具,无论是新手还是普通用户,都能快速上手并应用于东京气温等实际场景的预测任务中。
【免费下载链接】time-anchor-modernbert-32m项目地址: https://ai.gitcode.com/hf_mirrors/K-Iwa/time-anchor-modernbert-32m
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
