应用归一化和结构更改后的数据值 NaN

Data values NaN after normalization is applied and structure changes

我试图在使用神经网络函数之前对我的值进行归一化,但是,当对我的值进行归一化时,它们变成了 NaN,我从 dataDelay 变量中的值变成了具有 88 个变量的单个观察值,而不是原金额。

library(neuralnet)
library(grid)
library(MASS)
library(ggplot2)
library(reshape2)
library(gridExtra)
library(neuralnet)

normalize <- function(x){
  return ((x - min(x)) / (max(x) - min (x)))
}


data <-
  structure(
    list(
      `USD/EUR` = c(
        1.373,
        1.386,
        1.3768,
        1.3718,
        1.3774,
        1.3672,
        1.3872,
        1.3932,
        1.3911,
        1.3838,
        1.4171,
        1.4164,
        1.3947,
        1.3675,
        1.3801,
        1.3744,
        1.3759,
        1.3743,
        1.3787,
        1.3595,
        1.3599,
        1.3624,
        1.3523,
        1.3506,
        1.3521
      )
    ),
    row.names = c(NA,-25L),
    class = c("tbl_df",
              "tbl", "data.frame")
  )


#time series delay
dataDelay <- embed(data[[1]], 4)[, 4:1]
#normalizing values
currencyNorm <- as.data.frame(lapply(dataDelay, normalize))

Input <- subset(dataDelay, select = c(dataDelay[1], dataDelay[2], dataDelay[3]))
Output <- subset(dataDelay, select = c(dataDelay[4]))

##NN model
currency_model <- neuralnet(Output~Input, hidden = 1, data = dataDelay)

数据延迟输出:

      [,1]   [,2]   [,3]   [,4]
[1,] 1.3730 1.3860 1.3768 1.3718
[2,] 1.3860 1.3768 1.3718 1.3774
[3,] 1.3768 1.3718 1.3774 1.3672
[4,] 1.3718 1.3774 1.3672 1.3872
[5,] 1.3774 1.3672 1.3872 1.3932
[6,] 1.3672 1.3872 1.3932 1.3911

标准化后:

NaN. NaN..1 NaN..2 NaN..3 NaN..4 NaN..5 NaN..6 NaN..7 NaN..8 NaN..9 NaN..10 NaN..11 NaN..12
1  NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN    NaN     NaN     NaN     NaN...

应用于完整数据集:

normalize <- function(x){
  return ((x - min(x)) / (max(x) - min (x)))
}


exchangeData <- read.csv("ExchangeUSDcsv.csv")
data <- exchangeData[,3]
data <- as.data.frame(data)

currencyNorm <- embed(normalize(data[[1]]), 4)[, 4:1]
head(currencyNorm)

currencyNorm <- as.data.frame(currencyNorm)

[在完整数据集上应用解决方案代码后出现问题]

完整数据集:https://www.dropbox.com/s/17exy1968lsidsc/ExchangeUSDcsv.csv?dl=0

应用于完整数据集时的输出:

     [,1] [,2] [,3] [,4]
[1,]   NA   NA   NA   NA
[2,]   NA   NA   NA   NA
[3,]   NA   NA   NA   NA
[4,]   NA   NA   NA   NA
[5,]   NA   NA   NA   NA
[6,]   NA   NA   NA   NA

如果你想对矩阵进行按列归一化 apply :

currencyNorm <- data.frame(apply(dataDelay, 2, normalize))

将完整数据归一化为整体:

currencyNorm <- normalize(dataDelay)

我认为最简单的方法是从原来的data归一化。

currencyNorm <- embed(normalize(data[[1]]), 4)[, 4:1]

但如果这是一个 XY Problem 那么也许下面的代码更切题。
它从 currencyNorm 构建一个带有一个隐藏层的神经网络。要提取不需要的子集 InputOutput,公式 V4 ~ . 对所有其他列的第 4 列 V4 建模。

library(neuralnet)

currencyNorm <- embed(normalize(data[[1]]), 4)[, 4:1]
currencyNorm <- as.data.frame(currencyNorm)
##NN model
currency_model <- neuralnet(V4 ~ ., hidden = 1, data = currencyNorm)

为了使用模型进行预测,您必须有 3 个值,每个值对应 V1V2V3

set.seed(2021)   # make the results reproducible
new <- data.frame(V1 = runif(1), V2 = runif(1), V3 = runif(1))

predict(currency_model, newdata = new)
#          [,1]
#[1,] 0.6168927

或者一个包含很多行的新数据集。

new2 <- data.frame(V1 = runif(5), V2 = runif(5), V3 = runif(5))
predict(currency_model, newdata = new2)