Transformer - 时间特征的处理

Transformer - 时间特征的处理

flyfish

ETTm1.csv有如下内容
在这里插入图片描述

假如有2016/7/1 0:45:00有这样的时间字符串,如何变成时间特征列表

from typing import Listimport numpy as np
import pandas as pd
from pandas.tseries import offsets
from pandas.tseries.frequencies import to_offsetclass TimeFeature:def __init__(self):passdef __call__(self, index: pd.DatetimeIndex) -> np.ndarray:passdef __repr__(self):return self.__class__.__name__ + "()"class SecondOfMinute(TimeFeature):"""Minute of hour encoded as value between [-0.5, 0.5]"""def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:return index.second / 59.0 - 0.5class MinuteOfHour(TimeFeature):"""Minute of hour encoded as value between [-0.5, 0.5]"""def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:return index.minute / 59.0 - 0.5class HourOfDay(TimeFeature):"""Hour of day encoded as value between [-0.5, 0.5]"""def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:return index.hour / 23.0 - 0.5class DayOfWeek(TimeFeature):"""Hour of day encoded as value between [-0.5, 0.5]"""def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:return index.dayofweek / 6.0 - 0.5class DayOfMonth(TimeFeature):"""Day of month encoded as value between [-0.5, 0.5]"""def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:return (index.day - 1) / 30.0 - 0.5class DayOfYear(TimeFeature):"""Day of year encoded as value between [-0.5, 0.5]"""def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:return (index.dayofyear - 1) / 365.0 - 0.5class MonthOfYear(TimeFeature):"""Month of year encoded as value between [-0.5, 0.5]"""def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:return (index.month - 1) / 11.0 - 0.5class WeekOfYear(TimeFeature):"""Week of year encoded as value between [-0.5, 0.5]"""def __call__(self, index: pd.DatetimeIndex) -> np.ndarray:return (index.isocalendar().week - 1) / 52.0 - 0.5def time_features_from_frequency_str(freq_str: str) -> List[TimeFeature]:"""Returns a list of time features that will be appropriate for the given frequency string.Parameters----------freq_strFrequency string of the form [multiple][granularity] such as "12H", "5min", "1D" etc."""features_by_offsets = {offsets.YearEnd: [],offsets.QuarterEnd: [MonthOfYear],offsets.MonthEnd: [MonthOfYear],offsets.Week: [DayOfMonth, WeekOfYear],offsets.Day: [DayOfWeek, DayOfMonth, DayOfYear],offsets.BusinessDay: [DayOfWeek, DayOfMonth, DayOfYear],offsets.Hour: [HourOfDay, DayOfWeek, DayOfMonth, DayOfYear],offsets.Minute: [MinuteOfHour,HourOfDay,DayOfWeek,DayOfMonth,DayOfYear,],offsets.Second: [SecondOfMinute,MinuteOfHour,HourOfDay,DayOfWeek,DayOfMonth,DayOfYear,],}offset = to_offset(freq_str)for offset_type, feature_classes in features_by_offsets.items():if isinstance(offset, offset_type):return [cls() for cls in feature_classes]supported_freq_msg = f"""Unsupported frequency {freq_str}The following frequencies are supported:Y   - yearlyalias: AM   - monthlyW   - weeklyD   - dailyB   - business daysH   - hourlyT   - minutelyalias: minS   - secondly"""raise RuntimeError(supported_freq_msg)
def printf_time_features():freq="h"dates=pd.to_datetime("2016/7/1 0:45:00")for feat in time_features_from_frequency_str(freq):print(feat,"\n")print(feat(dates))printf_time_features()
#返回适用于给定频率字符串的时间特征列表
# 频率字符串举例
# Y   - yearly
#     alias: A
# M   - monthly
# W   - weekly
# D   - daily
# B   - business days
# H   - hourly
# T   - minutely
#     alias: min
# S   - secondly

输出
输出4特征,时间字符串将编码为[-0.5,0.5]之间的值

# HourOfDay()# -0.5
# DayOfWeek()# 0.16666666666666663
# DayOfMonth()# -0.5
# DayOfYear()# -0.0013698630136986245
batch_x_mark: tensor([[[-0.5000,  0.1667, -0.5000, -0.0014],[-0.5000,  0.1667, -0.5000, -0.0014],[-0.5000,  0.1667, -0.5000, -0.0014],[-0.5000,  0.1667, -0.5000, -0.0014],[-0.4565,  0.1667, -0.5000, -0.0014],[-0.4565,  0.1667, -0.5000, -0.0014],[-0.4565,  0.1667, -0.5000, -0.0014],[-0.4565,  0.1667, -0.5000, -0.0014],[-0.4130,  0.1667, -0.5000, -0.0014],[-0.4130,  0.1667, -0.5000, -0.0014],[-0.4130,  0.1667, -0.5000, -0.0014],[-0.4130,  0.1667, -0.5000, -0.0014],[-0.3696,  0.1667, -0.5000, -0.0014],[-0.3696,  0.1667, -0.5000, -0.0014],[-0.3696,  0.1667, -0.5000, -0.0014],[-0.3696,  0.1667, -0.5000, -0.0014],[-0.3261,  0.1667, -0.5000, -0.0014],[-0.3261,  0.1667, -0.5000, -0.0014],[-0.3261,  0.1667, -0.5000, -0.0014],[-0.3261,  0.1667, -0.5000, -0.0014],[-0.2826,  0.1667, -0.5000, -0.0014],[-0.2826,  0.1667, -0.5000, -0.0014],[-0.2826,  0.1667, -0.5000, -0.0014],[-0.2826,  0.1667, -0.5000, -0.0014]]])
batch_y_mark: tensor([[[-0.3696,  0.1667, -0.5000, -0.0014],[-0.3696,  0.1667, -0.5000, -0.0014],[-0.3696,  0.1667, -0.5000, -0.0014],[-0.3696,  0.1667, -0.5000, -0.0014],[-0.3261,  0.1667, -0.5000, -0.0014],[-0.3261,  0.1667, -0.5000, -0.0014],[-0.3261,  0.1667, -0.5000, -0.0014],[-0.3261,  0.1667, -0.5000, -0.0014],[-0.2826,  0.1667, -0.5000, -0.0014],[-0.2826,  0.1667, -0.5000, -0.0014],[-0.2826,  0.1667, -0.5000, -0.0014],[-0.2826,  0.1667, -0.5000, -0.0014],[-0.2391,  0.1667, -0.5000, -0.0014],[-0.2391,  0.1667, -0.5000, -0.0014],[-0.2391,  0.1667, -0.5000, -0.0014],[-0.2391,  0.1667, -0.5000, -0.0014],[-0.1957,  0.1667, -0.5000, -0.0014],[-0.1957,  0.1667, -0.5000, -0.0014],[-0.1957,  0.1667, -0.5000, -0.0014],[-0.1957,  0.1667, -0.5000, -0.0014],[-0.1522,  0.1667, -0.5000, -0.0014],[-0.1522,  0.1667, -0.5000, -0.0014],[-0.1522,  0.1667, -0.5000, -0.0014],[-0.1522,  0.1667, -0.5000, -0.0014],[-0.1087,  0.1667, -0.5000, -0.0014],[-0.1087,  0.1667, -0.5000, -0.0014],[-0.1087,  0.1667, -0.5000, -0.0014],[-0.1087,  0.1667, -0.5000, -0.0014],[-0.0652,  0.1667, -0.5000, -0.0014],[-0.0652,  0.1667, -0.5000, -0.0014],[-0.0652,  0.1667, -0.5000, -0.0014],[-0.0652,  0.1667, -0.5000, -0.0014],[-0.0217,  0.1667, -0.5000, -0.0014],[-0.0217,  0.1667, -0.5000, -0.0014],[-0.0217,  0.1667, -0.5000, -0.0014],[-0.0217,  0.1667, -0.5000, -0.0014]]])

查看配置
打印属性值

 print('\n'.join(['%s:%s' % item for item in self.__dict__.items()]) )
seq_len:24
label_len:12
pred_len:24
set_type:0
features:M
target:OT
scale:True
timeenc:1
freq:h
root_path:./dataset/ETT-small/
data_path:ETTm1.csv
scaler:StandardScaler()

batch_x, batch_y, batch_x_mark, batch_y_mark各自的形状

 for i, (batch_x, , , ): torch.Size([1, 24, 7])for i, (, batch_y, , ): torch.Size([1, 36, 7])for i, (, , batch_x_mark, ): torch.Size([1, 24, 4])for i, (, , , batch_y_mark): torch.Size([1, 36, 4])

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/641218.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

携程 Java 暑期实习一面:HashMap 的 key 可以设置为 null 吗?那 ConcurrentHashMap 呢?

更多大厂面试内容可见 -> http://11come.cn 携程 Java 暑期实习一面:HashMap 的 key 可以设置为 null 吗?那 ConcurrentHashMap 呢? Java 基础 1、Java 中有哪些常见的数据结构? 图片来源于:JavaGuide Java 中常…

【数学归纳法 反证法】菲蜀定理

裴蜀定理(或贝祖定理,Bzout’s identity)得名于法国数学家艾蒂安裴蜀,说明了对任何整数a、b和它们的最大公约 数d,关于未知数x和y的线性不定方程(称为裴蜀等式):若a,b是整数,且&…

【分治算法】Hanoi塔问题Python实现

文章目录 [toc]问题描述Python实现 个人主页:丷从心 系列专栏:Python基础 学习指南:Python学习指南 问题描述 设 a a a、 b b b、 c c c是三个塔座,开始时,在塔座 a a a上有一叠共 n n n个圆盘,这些圆盘…

LabVIEW连接PostgreSql

一、安装ODBC 下载对应postgreSQL版本的ODBC 下载网址:http://ftp.postgresql.org/pub/odbc/versions/msi/ 下载好后默认安装就行,这样在ODBC数据源中才能找到。 二、配置系统DSN 实现要新建好要用的数据库,这里的用户名:postg…

近年数一,数二难度如何,听说24是像张宇那样的题?

直接上分数! “估分一百零几,平时李林130-140,张八110-125的样子,超越做的分数也是100出头。” 24学长说: “远离李林张八!张四没做不评价。” “李林张八暑假前做完当作打基础即可。超越才是真题难度”…

内存满了如何处理?

目录 虚拟内存 内存分配过程 直接内存回收和后台内存回收 回收内存的触发标准 那些内存被回收呢? 内存回收后,内存还是不够怎么办呢? 虚拟内存 介绍操作系统内存如何使用时,不可以避免的先认识到虚拟内存 首先我们通过虚拟内存的作用,来认识一下: 1.虚拟内存可以使得…

python 如何表示大写字母

upper() 方法将字符串中的小写字母转为大写字母。 语法 upper()方法语法: str.upper() 参数 NA。 返回值 返回小写字母转为大写字母的字符串。 实例 以下实例展示了 upper()函数的使用方法: #!/usr/bin/python str "this is string example…

就业班 第三阶段(负载均衡) 2401--4.18 day2 nginx2 LVS-DR模式

3、LVS/DR 模式 实验说明: 1.网络使用NAT模式 2.DR模式要求Director DIP 和 所有RealServer RIP必须在同一个网段及广播域 3.所有节点网关均指定真实网关 主机名ip系统用途client172.16.147.1mac客户端lvs-server172.16.147.154centos7.5分发器real-server1172.16.…

轻松搭建llama3Web 交互界面 - Ollama + Open WebUI

Ubuntu下安装:(官网:Download Ollama on Linux) curl -fsSL https://ollama.com/install.sh | sh 就运行起来ollama了,不放心可以用ollama serve查看一下 ollama run llama3 就可以跑起来了, 那么我们肯…

利用regress绘制拟合图,利用Slope的趋势分析(5)

code如下: % SCD,积雪日数 % SCS,积雪初日 % SCM,积雪终日% SCS SCD SCM clc; clear; % 读取 Excel 表 excel_file E:\work\2024年\April20\积雪作图\tif文件\excel表\SCS.xlsx; % Excel 文件路径 data readtable(excel_file);datavalue data{:,2:end}; year (…

基于vue+node+mysql的视频校对系统

一、登录注册:包括登录,注册,忘记密码,验证码等常用点。 二、用户管理:包括用户的增删改查 三、权限管理(请增加这个权限:任务分配——只有管理者才能发布和删除任务;管理员设置。 四…

ARTS Week 24

Algorithm 本周的算法题为 21. 合并两个有序链表 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例 1: img 输入:list1 [1,2,4], list2 [1,3,4]输出:[1,1,2,3,4,4] 实现代码如下&…