【问题标题】:Outlier detection for multi column data frame in RR中多列数据框的异常值检测
【发布时间】: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


【解决方案1】:

首先,您需要花费更多时间预处理您的数据。 您的坐标轴具有完全不同的含义和规模。如果不小心,异常值检测结果将毫无意义,因为它们是基于无意义的距离。

例如produceCode。您确定这应该是您的相似性的一部分吗?

还请注意,我发现 R DMwR 包的 lofactor 实现非常慢。另外,它似乎与欧几里得距离硬连线!

相反,我建议使用 ELKI 进行异常值检测。首先,它提供了更广泛的算法选择,其次,它比 R 快得多,第三,它非常模块化和灵活。对于您的用例,您可能需要实现自定义距离函数,而不是使用欧几里得距离。

这是implementing a custom distance function 上 ELKI 教程的链接。

【讨论】:

    猜你喜欢
    • 2013-04-11
    • 2018-03-04
    • 2021-11-13
    • 2017-10-01
    • 2014-09-05
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多