【发布时间】:2013-10-09 00:46:41
【问题描述】:
我有一个包含 18 列和大约 12000 行的数据框。我想找出前 17 列的异常值,并将结果与第 18 列进行比较。第 18 列是一个因子,包含可用作异常值指标的数据。
我的数据框是 ufo,我删除了第 18 列,如下所示:
ufo2 <- ufo[,1:17]
然后将 3 个非数字列转换为数值:
ufo2$Weight <- as.numeric(ufo2$Weight)
ufo2$InvoiceValue <- as.numeric(ufo2$InvoiceValue)
ufo2$Score <- as.numeric(ufo2$Score)
然后使用以下命令进行异常值检测:
outlier.scores <- lofactor(ufo2, k=5)
但是outlier.scores的所有元素都是NA!!!
我在这段代码中有什么错误吗?
还有其他方法可以找到此类数据框的异常值吗?
我所有的代码:
setwd(datadirectory)
library(doMC)
registerDoMC(cores=8)
library(DMwR)
# load data
load("data_9802-f2.RData")
ufo2 <- ufo[,2:17]
ufo2$Weight <- as.numeric(ufo2$Weight)
ufo2$InvoiceValue <- as.numeric(ufo2$InvoiceValue)
ufo2$Score <- as.numeric(ufo2$Score)
outlier.scores <- lofactor(ufo2, k=5)
dput(head(ufo2))的输出是:
structure(list(Origin = c(2L, 2L, 2L, 2L, 2L, 2L), IO = c(2L,
2L, 2L, 2L, 2L, 2L), Lot = c(1003L, 1003L, 1003L, 1012L, 1012L,
1013L), DocNumber = c(10069L, 10069L, 10087L, 10355L, 10355L,
10382L), OperatorID = c(5698L, 5698L, 2015L, 246L, 246L, 4135L
), Month = c(1L, 1L, 1L, 1L, 1L, 1L), LineNo = c(1L, 2L, 1L,
1L, 2L, 1L), Country = c(1L, 1L, 1L, 1L, 11L, 1L), ProduceCode = c(63456227L,
63455714L, 33687427L, 32686627L, 32686627L, 791614L), Weight = c(900,
850, 483, 110000, 5900, 1000), InvoiceValue = c(637, 775, 2896,
48812, 1459, 77), InvoiceValueWeight = c(707L, 912L, 5995L, 444L,
247L, 77L), AvgWeightMonth = c(1194.53, 1175.53, 7607.17, 311.667,
311.667, 363.526), SDWeightMonth = c(864.931, 780.247, 3442.93,
93.5818, 93.5818, 326.238), Score = c(0.56366535234262, 0.33775439984787,
0.46825476121676, 1.414092583904, 0.69101737288291, 0.87827342721894
), TransactionNo = c(47L, 47L, 6L, 3L, 3L, 57L)), .Names = c("Origin",
"IO", "Lot", "DocNumber", "OperatorID", "Month", "LineNo", "Country",
"ProduceCode", "Weight", "InvoiceValue", "InvoiceValueWeight",
"AvgWeightMonth", "SDWeightMonth", "Score", "TransactionNo"), row.names = c(NA,
6L), class = "data.frame")
【问题讨论】:
-
您好,欢迎来到 stackoverflow!如果您提供minimal, reproducible data set 以及您尝试过的代码,您更有可能收到答案。谢谢!
-
谢谢!数据集很大,但我的代码如上,您还需要回答我的问题吗?
-
您的数据样本(使用
dput(head(ufo2))提供)以及您已加载的包。只是猜测:您在使用as.numeric后查看过您的数据吗? -
您不需要发布整个数据集。请阅读我给你的链接,你会找到各种方法来发布一个小样本数据集。干杯。
-
添加到问题中的 dput 命令的输出。对于as.numeric之后的数据,数据一切正常。
标签: r data-mining outliers