Skip to content

Instantly share code, notes, and snippets.

@erickguan
Created February 2, 2017 15:42
Show Gist options
  • Save erickguan/d1134f974d4624c119e9d32af21ba83a to your computer and use it in GitHub Desktop.
Save erickguan/d1134f974d4624c119e9d32af21ba83a to your computer and use it in GitHub Desktop.
library("class")
iris <- read.csv("/Users/fantasticfears/Downloads/iris.csv", header=FALSE)
irisShuffled <- iris[sample(nrow(iris)),]
irisShuffledRaw <- irisShuffled[,1:4]
train <- irisShuffledRaw[1:100,]
test <- irisShuffledRaw[101:nrow(iris),]
cl <- factor(as.array(irisShuffled[1:100,5]))
predicted <- knn(train, test, cl, prob=TRUE)
trueTarget <- irisShuffled[101:150,5]
table(predicted == trueTarget)
table(predicted, trueTarget)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment