0

In program distance is Euclidean (which is by default). How to change it to Manhattan?

Code:

wdbc <- read.table(file.choose(), sep=",") 
View(wdbc) 
wdbc <- wdbc[, -1]
data_norm <- function(x) {((x - min(x))/ (max(x)- min(x)))} 
wdbc_norm <- as.data.frame(lapply(wdbc[, -1], data_norm))
summary(wdbc[,2:5]) 
summary(wdbc_norm[,1:4]) wdbc_train <- wdbc_norm[1:450,]
wdbc_test <- wdbc_norm[451:569,]
library(class)
wdbc_pred <- knn(wdbc_train, wdbc_test, wdbc[1:450, 1], k=21)
table(wdbc_pred, wdbc[451:569,1]) 
library(knnGarden)
wdbc_pred <- knn(wdbc_train, wdbc_test, wdbc[1:450, 1], k=21)
table(wdbc_pred, wdbc[451:569,1]) 
  • 2
    Since you used `library(knnGarden)` you are aware of the package. I have never used it, but the [documentation](https://cran.r-project.org/web/packages/knnGarden/knnGarden.pdf) shows the existence of a function `knnVCN` which allow for `method = "manhattan"` inside the function call. On the other hand, the documentation for `class` makes it fairly clear that its function `knn` is strictly for Euclidean distance. – John Coleman Oct 29 '18 at 09:38
  • 3
    https://www.rdocumentation.org/packages/cluster/versions/2.0.7-1/topics/daisy this function could generate manhattan distance. Get distance and then https://stackoverflow.com/questions/23449726/find-k-nearest-neighbors-starting-from-a-distance-matrix will help you. – abhiieor Oct 29 '18 at 10:23

0 Answers0