0

I'm trying to learn how to use kknn and am working through the iris example in the documentation. Meanwhile, my homework requires me to apply kknn to a dataset and iteratively supply k values and test to find the best one.
While supplying a value for k is an option for the function, the iris example produces a solution without a k value being supplied:

iris.kknn <- kknn(Species~., iris.learn, iris.valid, distance = 1,
                  kernel = "triangular")

How do we determine from the resulting kknn object what k value was used to produce the results? Does it use an arbitrary default value, or does it somehow optimise to pick a best value for k?

neilfws
  • 32,751
  • 5
  • 50
  • 63
James
  • 673
  • 6
  • 19

1 Answers1

0

The default value of k = 7. You can see that in the Usage section of the documentation to which you linked, or by typing ?kknn.

You can determine what k was used from the number of columns in iris.kknn$C (or iris.kknn$W, iris.kknn$D or iris.kknn$CL).

ncol(iris.kknn$C)
[1] 7
neilfws
  • 32,751
  • 5
  • 50
  • 63
  • I hadn't realised the values supplied in the Usage section were defaults, I guess duh, what else would they be !? Thanks. – James May 25 '17 at 05:25
  • I guess it is not immediately obvious when you're starting out. If no parameter is supplied and the function works, then there has to be a default and it should be documented. You can also see the defaults using `args(kknn)`. – neilfws May 25 '17 at 05:31