【问题标题】:Scale Function Returns: Error in FUN(x, aperm(array(STATS, dims[perm]), order(perm)), ...)缩放函数返回:FUN(x, aperm(array(STATS, dims[perm]), order(perm)), ...) 中的错误
【发布时间】:2019-11-07 06:34:47
【问题描述】:

尝试训练 NeuralNet,但我无法标准化我的数据。

为缩放定义 Max 和 Mins 可以正常工作。

 maxs <- apply(tour_weahter_data, 2, max) 
 mins <- apply(tour_weahter_data, 2, min)

这是我要扩展的数据:

head(tour_weahter_data)
Start Time Starting          Station ID Duration  Distance Temperatur Humidity
    1 2016-07-07 13:00:00                3063       12  578.7915         18       72
    2 2016-07-07 13:00:00                3040       10 1262.4654         18       72
    3 2016-07-07 13:00:00                3063       19 1660.0441         18       72
    4 2016-07-07 13:00:00                3018       10  907.1427         18       72
    5 2016-07-07 13:00:00                3076       10 1004.5161         18       72
    6 2016-07-07 13:00:00                3034        4  448.0982         18       72

这是对函数的调用:

 scaled <- as.data.frame(scale(tour_weahter_data, center = mins, scale = maxs - mins))

这是我收到的错误消息:

FUN(x, aperm(array(STATS, dims[perm]), order(perm)), ...) 中的错误: 二元运算符的非数字参数
另外:警告信息:
在 scale.default(tour_weahter_data, center = mins, scale = maxs - : 强制引入的 NAs

我的数据有问题还是我使用的功能不正确?

【问题讨论】:

    标签: r dataframe normalization


    【解决方案1】:

    您应该将scalenumeric 变量一起使用,因此您只能将它与数字变量一起使用。

    这是一种使用dplyr的方法。

    library(dplyr)
    
    vars_scale <- tour_weahter_data %>% 
      select_if(is.numeric) %>% 
      colnames()
    
    
    scale_min_max <- function(x) scale(x, center = min(x), scale = max(x) - min(x))
    
    
    tour_weahter_data %>% 
      mutate_at(vars_scale, scale_min_max)
    
    ## A tibble: 6 x 7
    #  Start[,1] Time_Starting       Station_ID[,1] Duration[,1]
    #      <dbl> <dttm>                       <dbl>        <dbl>
    #1       0   2016-07-07 13:00:00          0.776        0.533
    #2       0.2 2016-07-07 13:00:00          0.379        0.4  
    #3       0.4 2016-07-07 13:00:00          0.776        1    
    #4       0.6 2016-07-07 13:00:00          0            0.4  
    #5       0.8 2016-07-07 13:00:00          1            0.4  
    #6       1   2016-07-07 13:00:00          0.276        0    
    ## ... with 3 more variables: Distance[,1] <dbl>,
    ##   Temperatur[,1] <dbl>, Humidity[,1] <dbl>
    

    【讨论】:

    • 是的,日期时间是问题所在。谢谢我只是这样做了,不包括前两个cols。现在它起作用了。 scaled
    • 您可以使用cbind 绑定它们。例如cbind(tour_weahter_data[,1:2], scaled)
    • 好的,但我可以确定我将数据正确地绑定在一起,再次将相同的行放在一起吗?
    • @JensKrüger 由于您使用的函数是矢量化的,它们不会改变行顺序,所以不用担心。但是你可以设置rownames并查看。
    猜你喜欢
    • 2013-05-15
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 2013-09-07
    • 2016-09-10
    相关资源
    最近更新 更多