R中的KNN离群值检测

时间:2019-10-28 13:34:42

标签: r outliers unsupervised-learning

我正在尝试运行一个脚本,该脚本使用加权KNN离群值来执行离群值检测,但始终出现以下错误:

  

apply错误(kNNdist(x = dat,k = k),1,平均值):
    dim(X)必须为正长度

我尝试运行的脚本如下。这是一个脚本块,但是我在导致错误的脚本部分的正上方添加了一个注释,即函数:

WKNN_Outlier <- apply(kNNdist(x=dat, k = k), 1, mean)

如果有人对无监督的异常值检测有任何更好或更简单的主意,那么我真是无所不能(可以说...)

    library(dbscan)
    library(ggplot2)

    set.seed(0)

    x11 <- rnorm(n = 100, mean = 10, sd = 1) # Cluster 1 (x1 coordinate)
    x21 <- rnorm(n = 100, mean = 10, sd = 1) # Cluster 1 (x2 coordinate)
    x12 <- rnorm(n = 100, mean = 20, sd = 1) # Cluster 2 (x1 coordinate)
    x22 <- rnorm(n = 100, mean = 10, sd = 1) # Cluster 2 (x2 coordinate)
    x13 <- rnorm(n = 100, mean = 15, sd = 3) # Cluster 3 (x1 coordinate)
    x23 <- rnorm(n = 100, mean = 25, sd = 3) # Cluster 3 (x2 coordinate)
    x14 <- rnorm(n = 50, mean = 25, sd = 1)  # Cluster 4 (x1 coordinate)
    x24 <- rnorm(n = 50, mean = 25, sd = 1)  # Cluster 4 (x2 coordinate)

    dat <- data.frame(x1 = c(x11,x12,x13,x14), x2 = c(x21,x22,x23,x24))

    ( g0a <- ggplot() + geom_point(data=dat, mapping=aes(x=x1, y=x2), shape = 19) )

    k <- 4 # KNN parameter
    top_n <- 20 # No. of top outliers to be displayed

    KNN_Outlier <- kNNdist(x=dat, k = k)
    rank_KNN_Outlier <- order(x=KNN_Outlier, decreasing = TRUE)    # Sorting (descending)
    KNN_Result <- data.frame(ID = rank_KNN_Outlier, score = KNN_Outlier[rank_KNN_Outlier])

    head(KNN_Result, top_n)

    graph <- g0a +
      geom_point(data=dat[rank_KNN_Outlier[1:top_n],], mapping=aes(x=x1,y=x2), shape=19, 
      color="red", size=2) +
      geom_text(data=dat[rank_KNN_Outlier[1:top_n],],
      mapping=aes(x=(x1-0.5), y=x2, label=rank_KNN_Outlier[1:top_n]), size=2.5)

    graph

    ## Use KNNdist() to calculate the weighted KNN outlier score

    k <- 4       # KNN parameter
    top_n <- 20  # No. of top outliers to be displayed

下面的WKNN_Outler函数是导致错误的原因。据我所知,apply函数应该没有任何问题,因为数据(dat)被转换为data.frame,这应该可以防止错误,但不会。

    WKNN_Outlier <- apply(kNNdist(x=dat, k = k), 1, mean)  # Weighted KNN outlier score (mean)


    rank_WKNN_Outlier <- order(x=WKNN_Outlier, decreasing = TRUE) 
    WKNN_Result <- data.frame(ID = rank_WKNN_Outlier, score = WKNN_Outlier[rank_WKNN_Outlier])

    head(WKNN_Result, top_n)

    ge1 <- g0a +
      geom_point(data=dat[rank_WKNN_Outlier[1:top_n],], mapping=aes(x=x1,y=x2), shape=19, 
      color="red", size=2) +
      geom_text(data=dat[rank_WKNN_Outlier[1:top_n],],
      mapping=aes(x=(x1-0.5), y=x2, label=rank_WKNN_Outlier[1:top_n]), size=2.5)

    ge1

1 个答案:

答案 0 :(得分:1)

函数kNNdist(x=dat, k = k)产生的向量不是矩阵,这就是为什么当您尝试执行apply函数时会告诉您dim(X) must have a positive length(向量具有NULL暗角的原因)。

尝试:

WKNN_Outlier <- apply(kNNdist(x=dat, k = k, all=T), 1, mean)