gpt4 book ai didi

r - 是否有一个 R 函数可以使用 gramian-angular-fields-method (gaf/gasf) 将时间序列编码为图像?

转载 作者:行者123 更新时间:2023-12-05 07:23:13 26 4
gpt4 key购买 nike

我想使用 gramian-angular-fields-method (GAF) 将时间序列编码为图像,目的是应用卷积神经网络 (CNN)。到目前为止,我还没有找到实现此功能的 R 函数。

对于 Python,我发现了以下内容: https://github.com/pecu/Series2GAF

我也试过自己写一个函数,但我不确定它是否能正常工作。这是文章,提出了 GAF 方法: https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=6&ved=2ahUKEwjS5a2PxJjiAhUPIlAKHVj4BBMQFjAFegQIABAC&url=https%3A%2F%2Fwww.aaai.org%2Focs%2Findex.php%2FWS%2FAAAIW15%2Fpaper%2FviewFile%2F10179%2F10251&usg=AOvVaw0uFl_9ZuLwCRvF_BouOxnV

这是我的代码,其中包含存储在 x 中的时间序列值的示例:x 只是一个向量,其测量值按日期排序,例如

library('tidyverse')
library('reshape2')
library('ggplot2')

x <- tibble(values = sin(-10:6))

复制本文方程1、2、3的函数:

gramian_angular_field_custom <- function(x){
# Delete column names
names(x) <- NULL

# Convert to matrix
x <- x %>% as.matrix()

# Normalize
x <- (x - max(x) + (x - min(x))) / (max(x) -min(x))
x <- x %>% as.matrix()

# Calculate phi for polar coordinates
x <- x %>%
as_tibble() %>%
mutate(V1 = acos(V1)) %>%
as.matrix() %>%
t()

# Create matrix by column repeat
x <- x %>%
t() %>%
as_tibble() %>%
replicate(n = length(x), .) %>%
bind_cols()

# Calculate sum of phi
x <- x + as_tibble(t(x))
x <- x %>% as_tibble()

# Calculate cosinus
x <- x %>% cos()

x <- x %>% as.matrix()
colnames(x) <- NULL

# convert matrix to tibble in long format
x <- x %>%
melt() %>%
as_tibble()

return(x)
}

然后应用此函数并绘制生成的图像:

x %>% 
gramian_angular_field_custom() %>%
ggplot(aes(Var2, Var1)) +
geom_raster(aes(fill = value)) +
scale_fill_gradientn(colours = c('blue', 'green', 'yellow', 'red')) +
theme(legend.position = "none",
axis.title.x = element_blank(),
axis.title.y = element_blank())

生成的图像与对角线不是轴对称的,它应该是,或者我错了?对数学的任何建议都与实现此功能的 R 函数的提示一样受到赞赏。

非常感谢

最佳答案

您可以使用 python 模块 pyts和 R 包 reticulate从 R 运行 python 代码。

关于r - 是否有一个 R 函数可以使用 gramian-angular-fields-method (gaf/gasf) 将时间序列编码为图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56113791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com