news 2026/8/7 19:35:31

Time-Anchor ModernBERT 32M实战教程:用Python预测东京气温的64小时变化

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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-anchor

predict_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.csvoutput/variable_impact.csvoutput/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-32m

Time-Anchor ModernBERT 32M为时间序列预测提供了强大且易用的工具,无论是新手还是普通用户,都能快速上手并应用于东京气温等实际场景的预测任务中。

【免费下载链接】time-anchor-modernbert-32m项目地址: https://ai.gitcode.com/hf_mirrors/K-Iwa/time-anchor-modernbert-32m

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/7 19:35:25

Delon性能优化指南:让你的Angular应用飞起来

Delon性能优化指南:让你的Angular应用飞起来 【免费下载链接】delon Delon is a set of essential modules for ng-alain. https://github.com/ng-alain/ng-alain/issues 项目地址: https://gitcode.com/gh_mirrors/de/delon Delon是ng-alain的核心模块集&am…

作者头像 李华
网站建设 2026/8/7 19:35:10

CMake目标链接高级用法:example_exec与JSONUtils库的无缝集成

CMake目标链接高级用法:example_exec与JSONUtils库的无缝集成 【免费下载链接】modern-cmake-sample Example library that shows best practices and proper usage of CMake by using targets 项目地址: https://gitcode.com/gh_mirrors/mo/modern-cmake-sample …

作者头像 李华
网站建设 2026/8/7 19:33:53

Pylogix路由功能详解:跨ControlLogix底盘通信配置实例

Pylogix路由功能详解:跨ControlLogix底盘通信配置实例 【免费下载链接】pylogix Read/Write data from Allen Bradley Compact/Control Logix PLCs 项目地址: https://gitcode.com/gh_mirrors/py/pylogix Pylogix是一款强大的Python库,专为与Alle…

作者头像 李华
网站建设 2026/8/7 19:33:28

如何快速获取最新纯真IP数据库?qqwry项目使用指南

如何快速获取最新纯真IP数据库?qqwry项目使用指南 【免费下载链接】qqwry 纯真IP数据库,每天从官方授权方式自动抓取最新文件 项目地址: https://gitcode.com/gh_mirrors/qq/qqwry 纯真IP数据库(qqwry.dat)是网络开发中常用…

作者头像 李华
网站建设 2026/8/7 19:31:44

数据分析转大模型:用一次交付过程做复盘

聊《大模型岗位变了,数据分析工程师该补的还是算法吗?》之前,先说一句实在的:别急着背概念,先看它在真实项目里到底解决什么问题。摘要从报表开发转向智能分析 Agent,很多数据分析同学卡在 Demo 能跑但生产…

作者头像 李华