是否有用于编码时间序列的R函数作为图像,其中包括Gramian-angular-Fields-hethod(GAF/GA



我想将时间序列与Gramian-angular-Fields-Method(GAF(编码为图像,目的是应用卷积神经网络(CNN(。到目前为止,我还没有找到R函数。

对于Python,我找到了以下内容:https://github.com/pecu/series2gaf

我也尝试自己编写一个函数,但我不确定它是否正常工作。这是该文章,提出了GAF方法:https://www.google.com/url?sa = t& amp; rct = j&p =&q =&eSrc = s&eSrc; source = web&cd = b&cd = 6&aaai.org%2focs%2findex.php%2FWS%2faaaiw15%2fpaper%2fiewfile%2F10179%2F10251& usg = aovvaw0ufl_9zulwcr_9zulwcr_bouoxnv_bouoxnv_bouoxnv

这是我的代码,其中包含x: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软件包网站启用来运行python代码。

相关内容

最新更新