Chapter 3 k近邻法

对原来的k_neighbours算法做了一点改变

rm(list = ls())
require(magrittr)
## Loading required package: magrittr
k <- 6

if (!is.data.frame(iris)) stop("the input is not data.frame")
nr <- nrow(iris)
nc <- ncol(iris)

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
#创建找出距离每个特征的最近的k个“点”的矩阵
attr_k <- matrix(NA, k*nr, nc - 1)
for (i in 1:(nc - 1)) {
  # i <- 1
  for (ii in seq_len(nr)) {
    # ii <- 1
    min_order <- (iris[ii, i] - iris[-ii, i]) %>% abs() %>% order() ##最小值、第二小、第三小、、、的索引 ##最小值、第二小可能一样
    min_order <- min_order + 1  #之前除掉了"与自己的距离"
    end_1 <- (ii - 1) * k + 1
    end_2 <- ii * k
    # end_1:end_2, 1:6, 7:12, 13:18
    # 第1:6行针对第一个样本点, 第7:12行针对第二个样本点, 以此类推,,,
    attr_k[end_1:end_2, i] <- min_order[1:k]
  }
}

# attr_k[1:6, 1] 的含义是, 针对第一个样本点的第一个特征, 
# 距离该特征"最近"的6个其他的样本点的索引
iris[1,1]
## [1] 5.1
iris[attr_k[1:6, 1], 1]
## [1] 5.1 5.1 5.1 5.1 5.1 5.1
head(attr_k[1:6, ])
##      [,1] [,2] [,3] [,4]
## [1,]   18   18    2    2
## [2,]   20   28    5    3
## [3,]   22   37    7    4
## [4,]   24   41    9    5
## [5,]   40   44   13    8
## [6,]   45    5   18    9
all(attr_k[1, ] == attr_k[1, 1])
## [1] FALSE
TF <- matrix(NA, nrow(attr_k), 1)
for (i in seq_len(nrow(attr_k))) {
  TF[i, 1] <- all(attr_k[i, ] == attr_k[i, 1])
}
length(which(TF[, 1] == TRUE))
## [1] 0
  • 判断是否存在有样本点在每个维度上都是接近测试点的,
  • 先检查第一行,如果attr_k[1,] 都指向同一个样本点,则保存该样本点,
  • 继续检查第二行,如果attr_k[2,] 都指向同一个样本点,则保存该样本点,以此类推,
  • 而如果继续检查到某一行时,如attr_k[某一行,] 并不是都指向同一个样本点时,则停止,且attr_k[某一行,]不保存,只保存之前检查到的“都指向同一个样本点”的,
  • 而如果attr_k[1,] 并不是都指向同一个样本点,如(18 18 2 2)则保留这些样本点(18,2);不再继续往下检查,这样子其实每个维度都照顾到了,
save_sample_neighbours <- list(idx = list(), 
                               labels = list(), 
                               most_voted = list())
for (h in seq_len(nr)) {
  
  end__1 <- (h - 1) * k + 1
  end__2 <- h * k
  
  save_sample_c <- NULL
  save_sample_c1 <- NULL
  for (j in end__1:end__2) {      #以attr_k的第一至第k行(第一个样本点)为例,取其近邻点
    #j <- 1
    if (all(attr_k[j, ] == attr_k[j, 1])) {   # 此判断块将遍历完所有行(1:k)
      save_sample <- attr_k[j, 1]
      save_sample_c <- c(save_sample_c, save_sample)   #第一个元素不是NULL
    } else {
      print(paste(attr_k[j, ], "is not all the same!"))
      if (j == end__1) {
        print(j)
        save_sample_c1 <- unique(attr_k[j, ])
      } else {
      print(j)
      save_sample_c1 <- attr_k[end__1:(j - 1), 1]
      }
      break
    }
  }
  
  if (is.null(save_sample_c)) { ##注意,如果使用ifelse,save_sample_neighbours[[h]] <- save_sample_c1,只是返回save_sample_c1的第一个元素给save_sample_neighbours[[h]]
    save_sample_neighbours[["idx"]][[h]] <- save_sample_c1
  } else {save_sample_neighbours[["idx"]][[h]] <- save_sample_c}
  
}
## [1] "18 is not all the same!" "18 is not all the same!"
## [3] "2 is not all the same!"  "2 is not all the same!" 
## [1] 1
## [1] "10 is not all the same!" "13 is not all the same!"
## [3] "2 is not all the same!"  "2 is not all the same!" 
## [1] 7
## [1] "30 is not all the same!" "30 is not all the same!"
## [3] "17 is not all the same!" "2 is not all the same!" 
## [1] 13
## [1] "7 is not all the same!"  "10 is not all the same!"
## [3] "8 is not all the same!"  "2 is not all the same!" 
## [1] 19
## [1] "8 is not all the same!"  "23 is not all the same!"
## [3] "2 is not all the same!"  "2 is not all the same!" 
## [1] 25
## [1] "11 is not all the same!" "17 is not all the same!"
## [3] "19 is not all the same!" "16 is not all the same!"
## [1] 31
## [1] "5 is not all the same!"  "8 is not all the same!" 
## [3] "2 is not all the same!"  "18 is not all the same!"
## [1] 37
## [1] "6 is not all the same!" "8 is not all the same!"
## [3] "5 is not all the same!" "2 is not all the same!"
## [1] 43
## [1] "39 is not all the same!" "59 is not all the same!"
## [3] "2 is not all the same!"  "2 is not all the same!" 
## [1] 49
## [1] "3 is not all the same!"  "5 is not all the same!" 
## [3] "5 is not all the same!"  "13 is not all the same!"
## [1] 55
## [1] "7 is not all the same!"  "22 is not all the same!"
## [3] "5 is not all the same!"  "2 is not all the same!" 
## [1] 61
## [1] "13 is not all the same!" "8 is not all the same!" 
## [3] "26 is not all the same!" "2 is not all the same!" 
## [1] 67
## [1] "13 is not all the same!" "3 is not all the same!" 
## [3] "2 is not all the same!"  "11 is not all the same!"
## [1] 73
## [1] "10 is not all the same!" "3 is not all the same!" 
## [3] "15 is not all the same!" "11 is not all the same!"
## [1] 79
## [1] "68 is not all the same!" "33 is not all the same!"
## [3] "36 is not all the same!" "2 is not all the same!" 
## [1] 85
## [1] "19 is not all the same!" "34 is not all the same!"
## [3] "5 is not all the same!"  "7 is not all the same!" 
## [1] 91
## [1] "7 is not all the same!" "7 is not all the same!"
## [3] "4 is not all the same!" "7 is not all the same!"
## [1] 97
## [1] "2 is not all the same!" "2 is not all the same!"
## [3] "2 is not all the same!" "8 is not all the same!"
## [1] 103
## [1] "17 is not all the same!" "20 is not all the same!"
## [3] "7 is not all the same!"  "8 is not all the same!" 
## [1] 109
## [1] "2 is not all the same!"  "20 is not all the same!"
## [3] "5 is not all the same!"  "8 is not all the same!" 
## [1] 115
## [1] "7 is not all the same!" "8 is not all the same!"
## [3] "7 is not all the same!" "2 is not all the same!"
## [1] 121
## [1] "2 is not all the same!"  "12 is not all the same!"
## [3] "5 is not all the same!"  "7 is not all the same!" 
## [1] 127
## [1] "5 is not all the same!"  "6 is not all the same!" 
## [3] "15 is not all the same!" "2 is not all the same!" 
## [1] 133
## [1] "2 is not all the same!"  "50 is not all the same!"
## [3] "7 is not all the same!"  "7 is not all the same!" 
## [1] 139
## [1] "13 is not all the same!" "8 is not all the same!" 
## [3] "45 is not all the same!" "2 is not all the same!" 
## [1] 145
## [1] "6 is not all the same!"  "3 is not all the same!" 
## [3] "13 is not all the same!" "2 is not all the same!" 
## [1] 151
## [1] "6 is not all the same!"  "8 is not all the same!" 
## [3] "13 is not all the same!" "7 is not all the same!" 
## [1] 157
## [1] "29 is not all the same!" "2 is not all the same!" 
## [3] "5 is not all the same!"  "2 is not all the same!" 
## [1] 163
## [1] "29 is not all the same!" "8 is not all the same!" 
## [3] "2 is not all the same!"  "2 is not all the same!" 
## [1] 169
## [1] "4 is not all the same!"  "4 is not all the same!" 
## [3] "13 is not all the same!" "2 is not all the same!" 
## [1] 175
## [1] "13 is not all the same!" "5 is not all the same!" 
## [3] "13 is not all the same!" "2 is not all the same!" 
## [1] 181
## [1] "7 is not all the same!" "8 is not all the same!"
## [3] "5 is not all the same!" "7 is not all the same!"
## [1] 187
## [1] "29 is not all the same!" "16 is not all the same!"
## [3] "5 is not all the same!"  "11 is not all the same!"
## [1] 193
## [1] "37 is not all the same!" "34 is not all the same!"
## [3] "2 is not all the same!"  "2 is not all the same!" 
## [1] 199
## [1] "3 is not all the same!" "5 is not all the same!"
## [3] "5 is not all the same!" "2 is not all the same!"
## [1] 205
## [1] "6 is not all the same!"  "4 is not all the same!" 
## [3] "16 is not all the same!" "2 is not all the same!" 
## [1] 211
## [1] "35 is not all the same!" "2 is not all the same!" 
## [3] "4 is not all the same!"  "2 is not all the same!" 
## [1] 217
## [1] "3 is not all the same!"  "6 is not all the same!" 
## [3] "2 is not all the same!"  "11 is not all the same!"
## [1] 223
## [1] "10 is not all the same!" "3 is not all the same!" 
## [3] "4 is not all the same!"  "2 is not all the same!" 
## [1] 229
## [1] "2 is not all the same!" "8 is not all the same!"
## [3] "5 is not all the same!" "2 is not all the same!"
## [1] 235
## [1] "6 is not all the same!" "2 is not all the same!"
## [3] "4 is not all the same!" "8 is not all the same!"
## [1] 241
## [1] "5 is not all the same!"  "54 is not all the same!"
## [3] "4 is not all the same!"  "8 is not all the same!" 
## [1] 247
## [1] "10 is not all the same!" "4 is not all the same!" 
## [3] "4 is not all the same!"  "2 is not all the same!" 
## [1] 253
## [1] "6 is not all the same!"  "2 is not all the same!" 
## [3] "13 is not all the same!" "25 is not all the same!"
## [1] 259
## [1] "2 is not all the same!"  "20 is not all the same!"
## [3] "26 is not all the same!" "7 is not all the same!" 
## [1] 265
## [1] "13 is not all the same!" "3 is not all the same!" 
## [3] "2 is not all the same!"  "8 is not all the same!" 
## [1] 271
## [1] "2 is not all the same!"  "20 is not all the same!"
## [3] "13 is not all the same!" "2 is not all the same!" 
## [1] 277
## [1] "5 is not all the same!" "4 is not all the same!"
## [3] "2 is not all the same!" "2 is not all the same!"
## [1] 283
## [1] "29 is not all the same!" "12 is not all the same!"
## [3] "5 is not all the same!"  "2 is not all the same!" 
## [1] 289
## [1] "6 is not all the same!"  "25 is not all the same!"
## [3] "2 is not all the same!"  "2 is not all the same!" 
## [1] 295
## [1] "53 is not all the same!" "4 is not all the same!" 
## [3] "57 is not all the same!" "60 is not all the same!"
## [1] 301
## [1] "75 is not all the same!" "4 is not all the same!" 
## [3] "56 is not all the same!" "53 is not all the same!"
## [1] 307
## [1] "121 is not all the same!" "5 is not all the same!"  
## [3] "73 is not all the same!"  "53 is not all the same!" 
## [1] 313
## [1] "35 is not all the same!" "43 is not all the same!"
## [3] "63 is not all the same!" "56 is not all the same!"
## [1] 319
## [1] "105 is not all the same!" "56 is not all the same!" 
## [3] "59 is not all the same!"  "53 is not all the same!" 
## [1] 325
## [1] "17 is not all the same!" "56 is not all the same!"
## [3] "53 is not all the same!" "55 is not all the same!"
## [1] 331
## [1] "73 is not all the same!" "25 is not all the same!"
## [3] "52 is not all the same!" "84 is not all the same!"
## [1] 337
## [1] "3 is not all the same!"  "81 is not all the same!"
## [3] "94 is not all the same!" "61 is not all the same!"
## [1] 343
## [1] "76 is not all the same!" "10 is not all the same!"
## [3] "56 is not all the same!" "55 is not all the same!"
## [1] 349
## [1] "29 is not all the same!" "68 is not all the same!"
## [3] "70 is not all the same!" "52 is not all the same!"
## [1] 355
## [1] "6 is not all the same!"  "63 is not all the same!"
## [3] "80 is not all the same!" "59 is not all the same!"
## [1] 361
## [1] "71 is not all the same!" "3 is not all the same!" 
## [3] "95 is not all the same!" "53 is not all the same!"
## [1] 367
## [1] "79 is not all the same!" "69 is not all the same!"
## [3] "55 is not all the same!" "59 is not all the same!"
## [1] 373
## [1] "72 is not all the same!" "10 is not all the same!"
## [3] "52 is not all the same!" "52 is not all the same!"
## [1] 379
## [1] "67 is not all the same!" "10 is not all the same!"
## [3] "62 is not all the same!" "55 is not all the same!"
## [1] 385
## [1] "78 is not all the same!" "5 is not all the same!" 
## [3] "76 is not all the same!" "52 is not all the same!"
## [1] 391
## [1] "66 is not all the same!" "3 is not all the same!" 
## [3] "53 is not all the same!" "53 is not all the same!"
## [1] 397
## [1] "16 is not all the same!" "61 is not all the same!"
## [3] "89 is not all the same!" "59 is not all the same!"
## [1] 403
## [1] "98 is not all the same!" "64 is not all the same!"
## [3] "53 is not all the same!" "53 is not all the same!"
## [1] 409
## [1] "66 is not all the same!" "73 is not all the same!"
## [3] "61 is not all the same!" "81 is not all the same!"
## [1] 415
## [1] "63 is not all the same!"  "4 is not all the same!"  
## [3] "77 is not all the same!"  "104 is not all the same!"
## [1] 421
## [1] "65 is not all the same!" "56 is not all the same!"
## [3] "55 is not all the same!" "55 is not all the same!"
## [1] 427
## [1] "58 is not all the same!" "71 is not all the same!"
## [3] "54 is not all the same!" "53 is not all the same!"
## [1] 433
## [1] "65 is not all the same!" "56 is not all the same!"
## [3] "52 is not all the same!" "83 is not all the same!"
## [1] 439
## [1] "53 is not all the same!" "10 is not all the same!"
## [3] "98 is not all the same!" "55 is not all the same!"
## [1] 445
## [1] "60 is not all the same!" "3 is not all the same!" 
## [3] "67 is not all the same!" "52 is not all the same!"
## [1] 451
## [1] "113 is not all the same!" "56 is not all the same!" 
## [3] "72 is not all the same!"  "52 is not all the same!" 
## [1] 457
## [1] "67 is not all the same!"  "3 is not all the same!"  
## [3] "114 is not all the same!" "107 is not all the same!"
## [1] 463
## [1] "64 is not all the same!" "10 is not all the same!"
## [3] "53 is not all the same!" "53 is not all the same!"
## [1] 469
## [1] "17 is not all the same!" "91 is not all the same!"
## [3] "62 is not all the same!" "59 is not all the same!"
## [1] 475
## [1] "35 is not all the same!" "59 is not all the same!"
## [3] "82 is not all the same!" "71 is not all the same!"
## [1] 481
## [1] "35 is not all the same!" "59 is not all the same!"
## [3] "82 is not all the same!" "59 is not all the same!"
## [1] 487
## [1] "16 is not all the same!" "61 is not all the same!"
## [3] "61 is not all the same!" "75 is not all the same!"
## [1] 493
## [1] "64 is not all the same!"  "61 is not all the same!" 
## [3] "102 is not all the same!" "58 is not all the same!" 
## [1] 499
## [1] "7 is not all the same!"  "3 is not all the same!" 
## [3] "53 is not all the same!" "53 is not all the same!"
## [1] 505
## [1] "64 is not all the same!" "8 is not all the same!" 
## [3] "53 is not all the same!" "58 is not all the same!"
## [1] 511
## [1] "67 is not all the same!" "5 is not all the same!" 
## [3] "52 is not all the same!" "53 is not all the same!"
## [1] 517
## [1] "58 is not all the same!" "43 is not all the same!"
## [3] "67 is not all the same!" "55 is not all the same!"
## [1] 523
## [1] "66 is not all the same!" "3 is not all the same!" 
## [3] "69 is not all the same!" "55 is not all the same!"
## [1] 529
## [1] "35 is not all the same!" "71 is not all the same!"
## [3] "55 is not all the same!" "55 is not all the same!"
## [1] 535
## [1] "35 is not all the same!" "81 is not all the same!"
## [3] "67 is not all the same!" "75 is not all the same!"
## [1] 541
## [1] "65 is not all the same!" "3 is not all the same!" 
## [3] "56 is not all the same!" "52 is not all the same!"
## [1] 547
## [1] "16 is not all the same!" "81 is not all the same!"
## [3] "55 is not all the same!" "75 is not all the same!"
## [1] 553
## [1] "6 is not all the same!"  "43 is not all the same!"
## [3] "59 is not all the same!" "59 is not all the same!"
## [1] 559
## [1] "66 is not all the same!" "61 is not all the same!"
## [3] "63 is not all the same!" "55 is not all the same!"
## [1] 565
## [1] "17 is not all the same!" "3 is not all the same!" 
## [3] "63 is not all the same!" "75 is not all the same!"
## [1] 571
## [1] "17 is not all the same!" "10 is not all the same!"
## [3] "63 is not all the same!" "55 is not all the same!"
## [1] 577
## [1] "70 is not all the same!" "10 is not all the same!"
## [3] "76 is not all the same!" "55 is not all the same!"
## [1] 583
## [1] "2 is not all the same!"  "71 is not all the same!"
## [3] "59 is not all the same!" "71 is not all the same!"
## [1] 589
## [1] "17 is not all the same!" "56 is not all the same!"
## [3] "69 is not all the same!" "55 is not all the same!"
## [1] 595
## [1] "58 is not all the same!"  "25 is not all the same!" 
## [3] "126 is not all the same!" "110 is not all the same!"
## [1] 601
## [1] "16 is not all the same!"  "61 is not all the same!" 
## [3] "85 is not all the same!"  "112 is not all the same!"
## [1] 607
## [1] "52 is not all the same!"  "3 is not all the same!"  
## [3] "144 is not all the same!" "106 is not all the same!"
## [1] 613
## [1] "58 is not all the same!"  "10 is not all the same!" 
## [3] "129 is not all the same!" "72 is not all the same!" 
## [1] 619
## [1] "56 is not all the same!"  "3 is not all the same!"  
## [3] "109 is not all the same!" "118 is not all the same!"
## [1] 625
## [1] "118 is not all the same!" "3 is not all the same!"  
## [3] "118 is not all the same!" "104 is not all the same!"
## [1] 631
## [1] "3 is not all the same!"  "71 is not all the same!"
## [3] "53 is not all the same!" "79 is not all the same!"
## [1] 637
## [1] "110 is not all the same!" "10 is not all the same!" 
## [3] "132 is not all the same!" "72 is not all the same!" 
## [1] 643
## [1] "67 is not all the same!"  "71 is not all the same!" 
## [3] "106 is not all the same!" "72 is not all the same!" 
## [1] 649
## [1] "126 is not all the same!" "6 is not all the same!"  
## [3] "131 is not all the same!" "102 is not all the same!"
## [1] 655
## [1] "56 is not all the same!"  "4 is not all the same!"  
## [3] "85 is not all the same!"  "114 is not all the same!"
## [1] 661
## [1] "53 is not all the same!"  "61 is not all the same!" 
## [3] "116 is not all the same!" "103 is not all the same!"
## [1] 667
## [1] "78 is not all the same!"  "3 is not all the same!"  
## [3] "117 is not all the same!" "104 is not all the same!"
## [1] 673
## [1] "17 is not all the same!"  "71 is not all the same!" 
## [3] "79 is not all the same!"  "112 is not all the same!"
## [1] 679
## [1] "16 is not all the same!"  "56 is not all the same!" 
## [3] "85 is not all the same!"  "137 is not all the same!"
## [1] 685
## [1] "53 is not all the same!"  "4 is not all the same!"  
## [3] "113 is not all the same!" "119 is not all the same!"
## [1] 691
## [1] "56 is not all the same!"  "3 is not all the same!"  
## [3] "114 is not all the same!" "72 is not all the same!" 
## [1] 697
## [1] "119 is not all the same!" "20 is not all the same!" 
## [3] "123 is not all the same!" "106 is not all the same!"
## [1] 703
## [1] "119 is not all the same!" "81 is not all the same!" 
## [3] "119 is not all the same!" "117 is not all the same!"
## [1] 709
## [1] "64 is not all the same!" "64 is not all the same!"
## [3] "79 is not all the same!" "53 is not all the same!"
## [1] 715
## [1] "54 is not all the same!"  "4 is not all the same!"  
## [3] "125 is not all the same!" "117 is not all the same!"
## [1] 721
## [1] "66 is not all the same!"  "56 is not all the same!" 
## [3] "54 is not all the same!"  "112 is not all the same!"
## [1] 727
## [1] "119 is not all the same!" "56 is not all the same!" 
## [3] "119 is not all the same!" "112 is not all the same!"
## [1] 733
## [1] "58 is not all the same!" "61 is not all the same!"
## [3] "54 is not all the same!" "72 is not all the same!"
## [1] 739
## [1] "67 is not all the same!"  "25 is not all the same!" 
## [3] "122 is not all the same!" "104 is not all the same!"
## [1] 745
## [1] "111 is not all the same!" "4 is not all the same!"  
## [3] "102 is not all the same!" "72 is not all the same!" 
## [1] 751
## [1] "70 is not all the same!" "56 is not all the same!"
## [3] "72 is not all the same!" "72 is not all the same!"
## [1] 757
## [1] "65 is not all the same!" "3 is not all the same!" 
## [3] "54 is not all the same!" "72 is not all the same!"
## [1] 763
## [1] "53 is not all the same!"  "56 is not all the same!" 
## [3] "105 is not all the same!" "104 is not all the same!"
## [1] 769
## [1] "111 is not all the same!" "3 is not all the same!"  
## [3] "106 is not all the same!" "58 is not all the same!" 
## [1] 775
## [1] "109 is not all the same!" "56 is not all the same!" 
## [3] "111 is not all the same!" "103 is not all the same!"
## [1] 781
## [1] "119 is not all the same!" "20 is not all the same!" 
## [3] "109 is not all the same!" "112 is not all the same!"
## [1] 787
## [1] "53 is not all the same!"  "56 is not all the same!" 
## [3] "105 is not all the same!" "106 is not all the same!"
## [1] 793
## [1] "58 is not all the same!" "56 is not all the same!"
## [3] "85 is not all the same!" "53 is not all the same!"
## [1] 799
## [1] "65 is not all the same!"  "81 is not all the same!" 
## [3] "105 is not all the same!" "52 is not all the same!" 
## [1] 805
## [1] "119 is not all the same!" "3 is not all the same!"  
## [3] "111 is not all the same!" "117 is not all the same!"
## [1] 811
## [1] "58 is not all the same!"  "8 is not all the same!"  
## [3] "105 is not all the same!" "116 is not all the same!"
## [1] 817
## [1] "53 is not all the same!"  "5 is not all the same!"  
## [3] "114 is not all the same!" "72 is not all the same!" 
## [1] 823
## [1] "64 is not all the same!" "3 is not all the same!" 
## [3] "72 is not all the same!" "72 is not all the same!"
## [1] 829
## [1] "54 is not all the same!"  "5 is not all the same!"  
## [3] "149 is not all the same!" "104 is not all the same!"
## [1] 835
## [1] "67 is not all the same!"  "5 is not all the same!"  
## [3] "105 is not all the same!" "116 is not all the same!"
## [1] 841
## [1] "54 is not all the same!"  "5 is not all the same!"  
## [3] "85 is not all the same!"  "117 is not all the same!"
## [1] 847
## [1] "16 is not all the same!"  "61 is not all the same!" 
## [3] "85 is not all the same!"  "103 is not all the same!"
## [1] 853
## [1] "78 is not all the same!"  "4 is not all the same!"  
## [3] "104 is not all the same!" "117 is not all the same!"
## [1] 859
## [1] "67 is not all the same!"  "25 is not all the same!" 
## [3] "122 is not all the same!" "102 is not all the same!"
## [1] 865
## [1] "67 is not all the same!"  "3 is not all the same!"  
## [3] "148 is not all the same!" "117 is not all the same!"
## [1] 871
## [1] "58 is not all the same!"  "71 is not all the same!" 
## [3] "79 is not all the same!"  "103 is not all the same!"
## [1] 877
## [1] "56 is not all the same!"  "3 is not all the same!"  
## [3] "147 is not all the same!" "112 is not all the same!"
## [1] 883
## [1] "70 is not all the same!"  "8 is not all the same!"  
## [3] "141 is not all the same!" "117 is not all the same!"
## [1] 889
## [1] "63 is not all the same!" "3 is not all the same!" 
## [3] "85 is not all the same!" "72 is not all the same!"
## [1] 895
str(save_sample_neighbours)
## List of 3
##  $ idx       :List of 150
##   ..$ : num [1:2] 18 2
##   ..$ : num [1:3] 10 13 2
##   ..$ : num [1:3] 30 17 2
##   ..$ : num [1:4] 7 10 8 2
##   ..$ : num [1:3] 8 23 2
##   ..$ : num [1:4] 11 17 19 16
##   ..$ : num [1:4] 5 8 2 18
##   ..$ : num [1:4] 6 8 5 2
##   ..$ : num [1:3] 39 59 2
##   ..$ : num [1:3] 3 5 13
##   ..$ : num [1:4] 7 22 5 2
##   ..$ : num [1:4] 13 8 26 2
##   ..$ : num [1:4] 13 3 2 11
##   ..$ : num [1:4] 10 3 15 11
##   ..$ : num [1:4] 68 33 36 2
##   ..$ : num [1:4] 19 34 5 7
##   ..$ : num [1:2] 7 4
##   ..$ : num [1:2] 2 8
##   ..$ : num [1:4] 17 20 7 8
##   ..$ : num [1:4] 2 20 5 8
##   ..$ : num [1:3] 7 8 2
##   ..$ : num [1:4] 2 12 5 7
##   ..$ : num [1:4] 5 6 15 2
##   ..$ : num [1:3] 2 50 7
##   ..$ : num [1:4] 13 8 45 2
##   ..$ : num [1:4] 6 3 13 2
##   ..$ : num [1:4] 6 8 13 7
##   ..$ : num [1:3] 29 2 5
##   ..$ : num [1:3] 29 8 2
##   ..$ : num [1:3] 4 13 2
##   ..$ : num [1:3] 13 5 2
##   ..$ : num [1:3] 7 8 5
##   ..$ : num [1:4] 29 16 5 11
##   ..$ : num [1:3] 37 34 2
##   ..$ : num [1:3] 3 5 2
##   ..$ : num [1:4] 6 4 16 2
##   ..$ : num [1:3] 35 2 4
##   ..$ : num [1:4] 3 6 2 11
##   ..$ : num [1:4] 10 3 4 2
##   ..$ : num [1:3] 2 8 5
##   ..$ : num [1:4] 6 2 4 8
##   ..$ : num [1:4] 5 54 4 8
##   ..$ : num [1:3] 10 4 2
##   ..$ : num [1:4] 6 2 13 25
##   ..$ : num [1:4] 2 20 26 7
##   ..$ : num [1:4] 13 3 2 8
##   ..$ : num [1:3] 2 20 13
##   ..$ : num [1:3] 5 4 2
##   ..$ : num [1:4] 29 12 5 2
##   ..$ : num [1:3] 6 25 2
##   ..$ : num [1:4] 53 4 57 60
##   ..$ : num [1:4] 75 4 56 53
##   ..$ : num [1:4] 121 5 73 53
##   ..$ : num [1:4] 35 43 63 56
##   ..$ : num [1:4] 105 56 59 53
##   ..$ : num [1:4] 17 56 53 55
##   ..$ : num [1:4] 73 25 52 84
##   ..$ : num [1:4] 3 81 94 61
##   ..$ : num [1:4] 76 10 56 55
##   ..$ : num [1:4] 29 68 70 52
##   ..$ : num [1:4] 6 63 80 59
##   ..$ : num [1:4] 71 3 95 53
##   ..$ : num [1:4] 79 69 55 59
##   ..$ : num [1:3] 72 10 52
##   ..$ : num [1:4] 67 10 62 55
##   ..$ : num [1:4] 78 5 76 52
##   ..$ : num [1:3] 66 3 53
##   ..$ : num [1:4] 16 61 89 59
##   ..$ : num [1:3] 98 64 53
##   ..$ : num [1:4] 66 73 61 81
##   ..$ : num [1:4] 63 4 77 104
##   ..$ : num [1:3] 65 56 55
##   ..$ : num [1:4] 58 71 54 53
##   ..$ : num [1:4] 65 56 52 83
##   ..$ : num [1:4] 53 10 98 55
##   ..$ : num [1:4] 60 3 67 52
##   ..$ : num [1:4] 113 56 72 52
##   ..$ : num [1:4] 67 3 114 107
##   ..$ : num [1:3] 64 10 53
##   ..$ : num [1:4] 17 91 62 59
##   ..$ : num [1:4] 35 59 82 71
##   ..$ : num [1:3] 35 59 82
##   ..$ : num [1:3] 16 61 75
##   ..$ : num [1:4] 64 61 102 58
##   ..$ : num [1:3] 7 3 53
##   ..$ : num [1:4] 64 8 53 58
##   ..$ : num [1:4] 67 5 52 53
##   ..$ : num [1:4] 58 43 67 55
##   ..$ : num [1:4] 66 3 69 55
##   ..$ : num [1:3] 35 71 55
##   ..$ : num [1:4] 35 81 67 75
##   ..$ : num [1:4] 65 3 56 52
##   ..$ : num [1:4] 16 81 55 75
##   ..$ : num [1:3] 6 43 59
##   ..$ : num [1:4] 66 61 63 55
##   ..$ : num [1:4] 17 3 63 75
##   ..$ : num [1:4] 17 10 63 55
##   ..$ : num [1:4] 70 10 76 55
##   ..$ : num [1:3] 2 71 59
##   .. [list output truncated]
##  $ labels    : list()
##  $ most_voted: list()
str(save_sample_c)
##  NULL
str(save_sample_c1)
##  num [1:4] 63 3 85 72
#如果save_sample_c1非NULL,计算save_sample_c1中的点与测试点距离最近的点,取最近距离点
#如果save_sample_c非NULL,计算save_sample_c中的点与测试点距离,投票决定

save_sample_neighbours_1 <- save_sample_neighbours
str(save_sample_neighbours_1)
## List of 3
##  $ idx       :List of 150
##   ..$ : num [1:2] 18 2
##   ..$ : num [1:3] 10 13 2
##   ..$ : num [1:3] 30 17 2
##   ..$ : num [1:4] 7 10 8 2
##   ..$ : num [1:3] 8 23 2
##   ..$ : num [1:4] 11 17 19 16
##   ..$ : num [1:4] 5 8 2 18
##   ..$ : num [1:4] 6 8 5 2
##   ..$ : num [1:3] 39 59 2
##   ..$ : num [1:3] 3 5 13
##   ..$ : num [1:4] 7 22 5 2
##   ..$ : num [1:4] 13 8 26 2
##   ..$ : num [1:4] 13 3 2 11
##   ..$ : num [1:4] 10 3 15 11
##   ..$ : num [1:4] 68 33 36 2
##   ..$ : num [1:4] 19 34 5 7
##   ..$ : num [1:2] 7 4
##   ..$ : num [1:2] 2 8
##   ..$ : num [1:4] 17 20 7 8
##   ..$ : num [1:4] 2 20 5 8
##   ..$ : num [1:3] 7 8 2
##   ..$ : num [1:4] 2 12 5 7
##   ..$ : num [1:4] 5 6 15 2
##   ..$ : num [1:3] 2 50 7
##   ..$ : num [1:4] 13 8 45 2
##   ..$ : num [1:4] 6 3 13 2
##   ..$ : num [1:4] 6 8 13 7
##   ..$ : num [1:3] 29 2 5
##   ..$ : num [1:3] 29 8 2
##   ..$ : num [1:3] 4 13 2
##   ..$ : num [1:3] 13 5 2
##   ..$ : num [1:3] 7 8 5
##   ..$ : num [1:4] 29 16 5 11
##   ..$ : num [1:3] 37 34 2
##   ..$ : num [1:3] 3 5 2
##   ..$ : num [1:4] 6 4 16 2
##   ..$ : num [1:3] 35 2 4
##   ..$ : num [1:4] 3 6 2 11
##   ..$ : num [1:4] 10 3 4 2
##   ..$ : num [1:3] 2 8 5
##   ..$ : num [1:4] 6 2 4 8
##   ..$ : num [1:4] 5 54 4 8
##   ..$ : num [1:3] 10 4 2
##   ..$ : num [1:4] 6 2 13 25
##   ..$ : num [1:4] 2 20 26 7
##   ..$ : num [1:4] 13 3 2 8
##   ..$ : num [1:3] 2 20 13
##   ..$ : num [1:3] 5 4 2
##   ..$ : num [1:4] 29 12 5 2
##   ..$ : num [1:3] 6 25 2
##   ..$ : num [1:4] 53 4 57 60
##   ..$ : num [1:4] 75 4 56 53
##   ..$ : num [1:4] 121 5 73 53
##   ..$ : num [1:4] 35 43 63 56
##   ..$ : num [1:4] 105 56 59 53
##   ..$ : num [1:4] 17 56 53 55
##   ..$ : num [1:4] 73 25 52 84
##   ..$ : num [1:4] 3 81 94 61
##   ..$ : num [1:4] 76 10 56 55
##   ..$ : num [1:4] 29 68 70 52
##   ..$ : num [1:4] 6 63 80 59
##   ..$ : num [1:4] 71 3 95 53
##   ..$ : num [1:4] 79 69 55 59
##   ..$ : num [1:3] 72 10 52
##   ..$ : num [1:4] 67 10 62 55
##   ..$ : num [1:4] 78 5 76 52
##   ..$ : num [1:3] 66 3 53
##   ..$ : num [1:4] 16 61 89 59
##   ..$ : num [1:3] 98 64 53
##   ..$ : num [1:4] 66 73 61 81
##   ..$ : num [1:4] 63 4 77 104
##   ..$ : num [1:3] 65 56 55
##   ..$ : num [1:4] 58 71 54 53
##   ..$ : num [1:4] 65 56 52 83
##   ..$ : num [1:4] 53 10 98 55
##   ..$ : num [1:4] 60 3 67 52
##   ..$ : num [1:4] 113 56 72 52
##   ..$ : num [1:4] 67 3 114 107
##   ..$ : num [1:3] 64 10 53
##   ..$ : num [1:4] 17 91 62 59
##   ..$ : num [1:4] 35 59 82 71
##   ..$ : num [1:3] 35 59 82
##   ..$ : num [1:3] 16 61 75
##   ..$ : num [1:4] 64 61 102 58
##   ..$ : num [1:3] 7 3 53
##   ..$ : num [1:4] 64 8 53 58
##   ..$ : num [1:4] 67 5 52 53
##   ..$ : num [1:4] 58 43 67 55
##   ..$ : num [1:4] 66 3 69 55
##   ..$ : num [1:3] 35 71 55
##   ..$ : num [1:4] 35 81 67 75
##   ..$ : num [1:4] 65 3 56 52
##   ..$ : num [1:4] 16 81 55 75
##   ..$ : num [1:3] 6 43 59
##   ..$ : num [1:4] 66 61 63 55
##   ..$ : num [1:4] 17 3 63 75
##   ..$ : num [1:4] 17 10 63 55
##   ..$ : num [1:4] 70 10 76 55
##   ..$ : num [1:3] 2 71 59
##   .. [list output truncated]
##  $ labels    : list()
##  $ most_voted: list()
#对应标签
len_idx <- length(save_sample_neighbours_1[["idx"]])
for (i in seq_len(len_idx)) {
  indx <- save_sample_neighbours_1[["idx"]][[i]]
  save_sample_neighbours_1[["labels"]][[i]] <- as.character(iris[indx, ncol(iris)])
}


#投票
#names(which.max(table(c("a","b","a","b")))) #[1] "a"
for (i in seq_len(len_idx)) {
  save_sample_neighbours_1[["most_voted"]][[i]] <- names(which.max(table(save_sample_neighbours_1[["labels"]][[i]])))
}
str(save_sample_neighbours_1)
## List of 3
##  $ idx       :List of 150
##   ..$ : num [1:2] 18 2
##   ..$ : num [1:3] 10 13 2
##   ..$ : num [1:3] 30 17 2
##   ..$ : num [1:4] 7 10 8 2
##   ..$ : num [1:3] 8 23 2
##   ..$ : num [1:4] 11 17 19 16
##   ..$ : num [1:4] 5 8 2 18
##   ..$ : num [1:4] 6 8 5 2
##   ..$ : num [1:3] 39 59 2
##   ..$ : num [1:3] 3 5 13
##   ..$ : num [1:4] 7 22 5 2
##   ..$ : num [1:4] 13 8 26 2
##   ..$ : num [1:4] 13 3 2 11
##   ..$ : num [1:4] 10 3 15 11
##   ..$ : num [1:4] 68 33 36 2
##   ..$ : num [1:4] 19 34 5 7
##   ..$ : num [1:2] 7 4
##   ..$ : num [1:2] 2 8
##   ..$ : num [1:4] 17 20 7 8
##   ..$ : num [1:4] 2 20 5 8
##   ..$ : num [1:3] 7 8 2
##   ..$ : num [1:4] 2 12 5 7
##   ..$ : num [1:4] 5 6 15 2
##   ..$ : num [1:3] 2 50 7
##   ..$ : num [1:4] 13 8 45 2
##   ..$ : num [1:4] 6 3 13 2
##   ..$ : num [1:4] 6 8 13 7
##   ..$ : num [1:3] 29 2 5
##   ..$ : num [1:3] 29 8 2
##   ..$ : num [1:3] 4 13 2
##   ..$ : num [1:3] 13 5 2
##   ..$ : num [1:3] 7 8 5
##   ..$ : num [1:4] 29 16 5 11
##   ..$ : num [1:3] 37 34 2
##   ..$ : num [1:3] 3 5 2
##   ..$ : num [1:4] 6 4 16 2
##   ..$ : num [1:3] 35 2 4
##   ..$ : num [1:4] 3 6 2 11
##   ..$ : num [1:4] 10 3 4 2
##   ..$ : num [1:3] 2 8 5
##   ..$ : num [1:4] 6 2 4 8
##   ..$ : num [1:4] 5 54 4 8
##   ..$ : num [1:3] 10 4 2
##   ..$ : num [1:4] 6 2 13 25
##   ..$ : num [1:4] 2 20 26 7
##   ..$ : num [1:4] 13 3 2 8
##   ..$ : num [1:3] 2 20 13
##   ..$ : num [1:3] 5 4 2
##   ..$ : num [1:4] 29 12 5 2
##   ..$ : num [1:3] 6 25 2
##   ..$ : num [1:4] 53 4 57 60
##   ..$ : num [1:4] 75 4 56 53
##   ..$ : num [1:4] 121 5 73 53
##   ..$ : num [1:4] 35 43 63 56
##   ..$ : num [1:4] 105 56 59 53
##   ..$ : num [1:4] 17 56 53 55
##   ..$ : num [1:4] 73 25 52 84
##   ..$ : num [1:4] 3 81 94 61
##   ..$ : num [1:4] 76 10 56 55
##   ..$ : num [1:4] 29 68 70 52
##   ..$ : num [1:4] 6 63 80 59
##   ..$ : num [1:4] 71 3 95 53
##   ..$ : num [1:4] 79 69 55 59
##   ..$ : num [1:3] 72 10 52
##   ..$ : num [1:4] 67 10 62 55
##   ..$ : num [1:4] 78 5 76 52
##   ..$ : num [1:3] 66 3 53
##   ..$ : num [1:4] 16 61 89 59
##   ..$ : num [1:3] 98 64 53
##   ..$ : num [1:4] 66 73 61 81
##   ..$ : num [1:4] 63 4 77 104
##   ..$ : num [1:3] 65 56 55
##   ..$ : num [1:4] 58 71 54 53
##   ..$ : num [1:4] 65 56 52 83
##   ..$ : num [1:4] 53 10 98 55
##   ..$ : num [1:4] 60 3 67 52
##   ..$ : num [1:4] 113 56 72 52
##   ..$ : num [1:4] 67 3 114 107
##   ..$ : num [1:3] 64 10 53
##   ..$ : num [1:4] 17 91 62 59
##   ..$ : num [1:4] 35 59 82 71
##   ..$ : num [1:3] 35 59 82
##   ..$ : num [1:3] 16 61 75
##   ..$ : num [1:4] 64 61 102 58
##   ..$ : num [1:3] 7 3 53
##   ..$ : num [1:4] 64 8 53 58
##   ..$ : num [1:4] 67 5 52 53
##   ..$ : num [1:4] 58 43 67 55
##   ..$ : num [1:4] 66 3 69 55
##   ..$ : num [1:3] 35 71 55
##   ..$ : num [1:4] 35 81 67 75
##   ..$ : num [1:4] 65 3 56 52
##   ..$ : num [1:4] 16 81 55 75
##   ..$ : num [1:3] 6 43 59
##   ..$ : num [1:4] 66 61 63 55
##   ..$ : num [1:4] 17 3 63 75
##   ..$ : num [1:4] 17 10 63 55
##   ..$ : num [1:4] 70 10 76 55
##   ..$ : num [1:3] 2 71 59
##   .. [list output truncated]
##  $ labels    :List of 150
##   ..$ : chr [1:2] "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "versicolor" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "versicolor" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:2] "setosa" "setosa"
##   ..$ : chr [1:2] "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "versicolor" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "setosa" "setosa" "setosa" "setosa"
##   ..$ : chr [1:3] "setosa" "setosa" "setosa"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "virginica" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "virginica" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:3] "versicolor" "setosa" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:3] "versicolor" "setosa" "versicolor"
##   ..$ : chr [1:4] "setosa" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:3] "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "virginica"
##   ..$ : chr [1:3] "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "virginica" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "virginica" "virginica"
##   ..$ : chr [1:3] "versicolor" "setosa" "versicolor"
##   ..$ : chr [1:4] "setosa" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:3] "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:3] "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "versicolor" "virginica" "versicolor"
##   ..$ : chr [1:3] "setosa" "setosa" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:3] "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:3] "setosa" "setosa" "versicolor"
##   ..$ : chr [1:4] "versicolor" "versicolor" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "setosa" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:4] "versicolor" "setosa" "versicolor" "versicolor"
##   ..$ : chr [1:3] "setosa" "versicolor" "versicolor"
##   .. [list output truncated]
##  $ most_voted:List of 150
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "setosa"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "virginica"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "setosa"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   ..$ : chr "setosa"
##   ..$ : chr "versicolor"
##   ..$ : chr "setosa"
##   ..$ : chr "setosa"
##   ..$ : chr "versicolor"
##   ..$ : chr "versicolor"
##   .. [list output truncated]
#结果
vec_most_voted <- purrr::as_vector(save_sample_neighbours_1[["most_voted"]])
vec_iris_label <- purrr::as_vector(iris[ncol(iris)])
table(vec_iris_label, vec_most_voted)
##               vec_most_voted
## vec_iris_label setosa versicolor virginica
##     setosa         50          0         0
##     versicolor      5         44         1
##     virginica       0         24        26

距离计算函数

#计算距离

#定义距离计算公式

dist_MINko <- function(dataframe, q) {
  nc <- ncol(dataframe)
  nr <- nrow(dataframe)
  distance <- matrix(NA, nr, nr)
  for (j in 1:nr) {
    for (i in 1:nr) {
      suma <- sum(abs(dataframe[j, -nc] - dataframe[i, -nc])^q)
      distance[j, i] <- round(suma^(1/q), 4)
    }
  }
  return(distance)
}

distance_3 <- dist_MINko(iris, 3)   #当q趋于无穷大时,切比雪夫距离
distance_3[1, 3]
## [1] 0.4514
distance_2 <- dist_MINko(iris, 2)  #欧氏距离
distance_2[1, 3]
## [1] 0.5099
distance_1 <- dist_MINko(iris, 1)   #绝对值距离
distance_1[1, 3]
## [1] 0.8
dist_MAX <- function(dataframe) {     #切比雪夫距离
  nc <- ncol(dataframe)
  nr <- nrow(dataframe)
  distance <- matrix(NA, nr, nr)
  for (j in 1:nr) {
    for (i in 1:nr) {
      distance[j, i] <- max(abs(dataframe[j, -nc] - dataframe[i, -nc]))
    }
  }
  return(distance)
}

dist_MAX(iris)[1:6, 1:6]
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]  0.0  0.5  0.4  0.5  0.1  0.4
## [2,]  0.5  0.0  0.2  0.3  0.6  0.9
## [3,]  0.4  0.2  0.0  0.2  0.4  0.7
## [4,]  0.5  0.3  0.2  0.0  0.5  0.8
## [5,]  0.1  0.6  0.4  0.5  0.0  0.4
## [6,]  0.4  0.9  0.7  0.8  0.4  0.0
max(abs(iris[66, -5] - iris[150, -nc]))
## [1] 0.8

马氏距离

dist_MAHALA <- function(dataframe) {   #马氏距离
  nc <- ncol(dataframe)
  nr <- nrow(dataframe)
  covar <- cov(dataframe[, -nc])  #最后一列为标签值
  distance <- matrix(NA, nr, nr)
  for (j in 1:nr) {
    for (i in 1:nr) {
      #i <- 2
      mat_left <- as.matrix(dataframe[j, -nc] - dataframe[i, -nc])
      mat_right <- t(as.matrix(dataframe[j, -nc] - dataframe[i, -nc]))
      distance[j, i] <- round(mat_left %*% covar %*% mat_right, 4)
    }
  }
  return(distance)
}

dist_MAHALA(iris)[1:6, 1:6]
##        [,1]   [,2]   [,3]   [,4]   [,5]   [,6]
## [1,] 0.0000 0.0664 0.2300 0.1150 0.0096 0.7339
## [2,] 0.0664 0.0000 0.1337 0.0143 0.0702 1.0100
## [3,] 0.2300 0.1337 0.0000 0.0948 0.1632 1.7561
## [4,] 0.1150 0.0143 0.0948 0.0000 0.1024 1.1862
## [5,] 0.0096 0.0702 0.1632 0.1024 0.0000 0.8903
## [6,] 0.7339 1.0100 1.7561 1.1862 0.8903 0.0000

兰氏距离

dist_LANBERRA_1 <- function(dataframe) {     # 兰氏距离
  nc <- ncol(dataframe)
  nr <- nrow(dataframe)
  distance <- matrix(NA, nr, nr)
  p <- nc - 1   #维度个数
  for (j in 1:nr) {
    for (i in 1:nr) {
      d1 <- 0
      for (k in 1:(nc - 1)) {
        d <- abs(iris[j, k] - iris[i, k]) / (iris[j, k] + iris[i, k])
        d1 <- d + d1
      }
      distance[j, i] <- round(d1 / p, 4)
    }
  }
  return(distance)
}

dist_LANBERRA_1(iris)[1:6, 1:6]
##        [,1]   [,2]   [,3]   [,4]   [,5]   [,6]
## [1,] 0.0000 0.0242 0.0307 0.0367 0.0060 0.1282
## [2,] 0.0242 0.0000 0.0225 0.0206 0.0253 0.1523
## [3,] 0.0307 0.0225 0.0000 0.0245 0.0317 0.1586
## [4,] 0.0367 0.0206 0.0245 0.0000 0.0377 0.1475
## [5,] 0.0060 0.0253 0.0317 0.0377 0.0000 0.1271
## [6,] 0.1282 0.1523 0.1586 0.1475 0.1271 0.0000