我正在使用来自 UCI 机器学习存储库的 optdigits 数据集,并希望创建一个带有噪声的新训练数据集。如何随机向R中的向量添加噪声?说损坏了向量值的 10%。
向一列数据添加噪声
机器算法验证
r
机器学习
2022-04-08 08:02:39
1个回答
这取决于您要添加的噪音类型。
这是一个例子:
x <- runif(100,100,150) # this is our original vector, which I'm just making up
corrupt <- rbinom(length(x),1,0.1) # choose an average of 10% to corrupt at random
corrupt <- as.logical(corrupt)
noise <- rnorm(sum(corrupt),1000,200) # generate the noise to add
x[corrupt] <- x[corrupt] + noise # about 10% of x has been corrupted
这是一个损坏值与原始值的对比图:

您应该替换您自己的向量和噪声类型。
编辑:我刚刚看到你对我的问题的回答。要获得从 0 到 9 的随机值,您可以sample这样使用:
noise <- sample(0:9,sum(corrupt),replace=TRUE)
并且因为您要替换而不是添加,所以您可以这样做:
x[corrupt] <- noise
在与以前相同的数据上给出这个,并替换相同的值:

如果您想替换精确数量的值(例如 1000 个值中的 100 个,而不是平均 100 个),您可以sample从一组指示要替换的值的索引中进行选择。
其它你可能感兴趣的问题