【问题标题】:scale in dataset在数据集中缩放
【发布时间】:2021-06-08 17:45:15
【问题描述】:

要在数据集中缩放哪个变量

【问题讨论】:

标签: r data-science data-mining


【解决方案1】:

我认为this 从理论上回答了你的问题。

请记住,如果您想构建一个统计模型,您可能希望将数据划分为训练集和测试集(可能还有验证集)。在这种情况下,您需要先独立缩放训练集,然后您可以根据训练集的均值和平均值来缩放测试集!这是为了避免将信息从测试集“泄露”到训练集。

从编码的角度来看[更适合 StackOverflow 的主题]:


# split 80-20 of training set and test set
p <- 0.8

# set seed for reproducibility
set.seed(1)
trn_rows <- sample(nrow(mtcars), nrow(mtcars) * p)

# training and test sets
trn <- mtcars[trn_rows, ]
tst <- mtcars[-trn_rows, ]

# calc mean and sd for each column of the training set
mean_trn <- apply(trn, 2, mean)
sd_trn   <- apply(trn, 2, sd)

# scale traing and test
trn_scaled <- scale(trn, center = mean_trn, scale = sd_trn)
tst_scaled <- scale(tst, center = mean_trn, scale = sd_trn)

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2018-04-13
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多