0

Using the iris dataset in R I have come up with two different subsets.

test <- iris[seq(1, nrow(iris), by = 5),]
training <- iris[-seq(1, nrow(iris), by = 5),]

I'm now looking for the k nearest neighbor using k=1. Here's my attempt and output.

knn(test, k = 1, prob=TRUE)

**Error in knn(test, k = 1, prob = TRUE) : 
  argument "test" is missing, with no default**

Why is this telling me that "test" is missing? Thanks

Tomas
  • 115
  • 1
  • 2
  • 12
  • You forget to include training data and also cl. Type `?knn `in your R console to find the exact syntax. – Ravi Jun 28 '16 at 17:17
  • It's saying train and class have different lengths? – Tomas Jun 28 '16 at 17:24
  • What did you take class as? – Ravi Jun 28 '16 at 17:29
  • See these threads, it might become clearer. http://stackoverflow.com/questions/29654540/what-does-cl-parameter-in-knn-function-in-r-mean and http://stackoverflow.com/questions/16276388/knn-in-r-train-and-class-have-different-lengths – Ravi Jun 28 '16 at 17:31

1 Answers1

0

Try this:

test <- iris[seq(1, nrow(iris), by = 5),-5] #hide the species from knn
training <- iris[-seq(1, nrow(iris), by = 5),-5]

cl <-iris[-seq(1, nrow(iris), by = 5),]$Species #this is where you tell
# it which species is which.

knn(train = training, test = test, cl = cl, k = 1, prob=TRUE)

[1] setosa     setosa     setosa     setosa     setosa     setosa     setosa    
[8] setosa     setosa     setosa     versicolor versicolor versicolor versicolor
[15] virginica  versicolor versicolor versicolor versicolor versicolor virginica 
[22] virginica  virginica  virginica  virginica  virginica  virginica  virginica 
[29] virginica  virginica 
attr(,"prob")
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Levels: setosa versicolor virginica
Bryan Goggin
  • 2,449
  • 15
  • 17